UVA11029 Leading and Trailing【快速模幂+数学】

Apart from the novice programmers, all others know that you can’t exactly represent numbers raised to some high power. For example, the C function pow(125456, 455) can be represented in double data type format, but you won’t get all the digits of the result. However we can get at least some satisfaction if we could know few of the leading and trailing digits. This is the requirement of this problem.
Input
The first line of input will be an integer T < 1001, where T represents the number of test cases. Each of the next T lines contains two positive integers, n and k. n will fit in 32 bit integer and k will be less than 10000001.
Output
For each line of input there will be one line of output. It will be of the format LLL . . . T T T, where LLL represents the first three digits of n^k and T T T represents the last three digits of n^k. You are assured that n k will contain at least 6 digits.
Sample Input
2
123456 1
123456 2
Sample Output
123...456
152...936

问题链接UVA11029 Leading and Trailing
问题简述:(略)
问题分析
????计算n^k的前3位和后3位。
????后3位用快速模幂来计算。
????前3位则根据数学公式,按浮点数进行计算。假设n^K= X 10 ^(len-1) ,其中X是规范化表示的小数点前唯一的1位10进制数字,len是10进制数的长度,那么两边取对数后得Klog10(n)=(len-1)+log10(X);令T=Klog10(n),那么根据取对数后的等式,去除整数部分,tmp的小数部分=log(X),故T=10^X,即X=pow(10,T)。而T值用程序语言的表达式来表示的话,T=Klog10(n)-(long long)K*log10(n),整数用long long类型。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++程序如下:

/* UVA11029 Leading and Trailing */

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
const int MOD = 1000;

// 快速模幂
LL powmod(LL x, LL n, LL m)
{
    LL result = 1;
    for(; n; n >>= 1) {
        if(n & 1) {
            result *= x;
            result %= m;
        }
        x *= x;
        x %= m;
    }

    return result;
}

int main()
{
    int t;
    LL n, k, ans1, ans2;
    scanf("%d",&t);
    while(t--) {
        scanf("%lld%lld", &n, &k);

        ans2 = powmod(n, k, MOD);
        double tmp = k * log10(n);
        tmp=pow(10.0, tmp - (long long)tmp);
        ans1 = (LL)(tmp * 100);

        printf("%lld...%03lld\n", ans1, ans2);
    }

    return 0;
}

原文地址:https://www.cnblogs.com/tigerisland45/p/10469421.html

时间: 2024-08-28 07:13:31

UVA11029 Leading and Trailing【快速模幂+数学】的相关文章

LightOJ 1282 Leading and Trailing (快数幂 + 数学)

http://lightoj.com/volume_showproblem.php?problem=1282 Leading and Trailing Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1282 Description You are given two integers: n and k, your task is t

[题解]UVA11029 Leading and Trailing

链接:http://vjudge.net/problem/viewProblem.action?id=19597 描述:求n^k的前三位数字和后三位数字 思路:题目要解决两个问题.后三位数字可以一边求高次幂一边取模,下面给出求前三位数字的方法. n^k = (10^lg n)^k = 10^(k*lg n) 为了描述方便,令X=n^k .则 lg X 的整数部分表示X有多少位.设整数部分为zs,小数部分为xs,则X=(10^zs)*(10^xs) . (10^zs)的形式就是100……,跟X的位

【分治】快速模幂

问题 R: [分治]快速模幂 时间限制: 1 Sec  内存限制: 64 MB提交: 8  解决: 7[提交][状态][讨论版] 题目描述 试求ab%n的值,其中a.b.n均为整数范围内的数. 输入 三个整数a.b和n 输出 ab%n的值 样例输入 1 1 1 样例输出 0解题思路:快速模幂比快速幂多了取余,和另一个快速取余是一样的.有公式.代码: #include <iostream> #include <cstdio> using namespace std; //a*b %

快速模幂

问题 H: [分治]快速模幂 时间限制: 1 Sec  内存限制: 64 MB提交: 79  解决: 64[提交] [状态] [讨论版] [命题人:admin] 题目描述 试求ab%n的值,其中a.b.n均为整数范围内的数. 输入 三个整数a.b和n 输出 ab%n的值 样例输入 1 1 1 样例输出 0 1 #include <iostream> 2 3 using namespace std; 4 typedef long long ll; 5 ll fun(ll a, ll b, ll

poj1845 逆元,快速模幂

题目大意: 给定两个正整数和,求的所有因子和对9901取余后的值. 分析: 很容易知道,先把分解得到,那么得到,那么 的所有因子和的表达式如下 因为要取模且存在除法,所以要用到逆元. 对于正整数和,如果有,那么把这个同余方程中的最小正整数解叫做模的逆元. 逆元一般用扩展欧几里得算法来求得,如果为素数,那么还可以根据费马小定理得到逆元为. 推导过程如下 求现在来看一个逆元最常见问题,求如下表达式的值(已知) 当然这个经典的问题有很多方法,最常见的就是扩展欧几里得,如果是素数,还可以用费马小定理.

uva11029 - Leading and Trailing

题目: 求n的k次方,然后将答案用前三位和最后三位表示. Sample Input 2 123456 1 123456 2 Sample Output 123...456 152...936 分析: 题目中其实有提示,用double来表示n的k次方,double神奇的地方在于能转化为string类型的字符串.用到了sprintf这个函数.代码: #include <cstdio> #include <iostream> #include <cstring> using

UVA Leading and Trailing 11029【数学+快速幂】

11029 - Leading and Trailing Time limit: 3.000 seconds Apart from the novice programmers, all others know that you can't exactly represent numbers raised to some high power. For example, the C function pow(125456, 455) can be represented in double da

快速幂 E - Leading and Trailing LightOJ - 1282

E - Leading and Trailing LightOJ - 1282 快速幂主要是把n拆成2进制位,如果这一位有那么就乘,没有就不乘,而计数器也就是x是不断推进的,从x->x^2->x^4直到n的最高位精髓在于取模,后一步的要求结果只与前一步的模后数据有关 . 对于后三个数用了log10.log函数对求n^k这种问题还是很有用的.没想出来. 1 #include <iostream> 2 #include <cstring> 3 #include <st

UVA 11029 || Lightoj 1282 Leading and Trailing 数学

Leading and Trailing You are given two integers: n and k, your task is to find the most significant three digits, and least significant three digits of nk. Input Input starts with an integer T (≤ 1000), denoting the number of test cases. Each case st