Uva 11029 Leading and Trailing (求n^k前3位和后3位)

题意:给你 n 和 k ,让你求 n^k 的前三位和后三位

思路:后三位很简单,直接快速幂就好,重点在于如何求前三位,注意前导0

资料:求n^k的前m位 博客连接地址

代码:

#include <iostream>
#include <cmath>
#include <cstdio>
#include <algorithm>
#define ll long long
using namespace std;

ll qmod(ll a,ll b,ll mod)
{
    ll ans=1;
    while(b)
    {
        if(b%2)
        {
            ans=(ans*a)%mod;
        }
        b=b/2;
        a=(a*a)%mod;
    }
    return ans;
}

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
       ll n,k;
       scanf("%lld %lld",&n,&k);
       double a=k*log10((double)n);
       a=a-floor(a);
       double ans1 = pow(10,a)*100;
       ll ans2 = qmod(n,k,1000);
       cout<<(ll)ans1<<"...";
       printf("%03lld\n",ans2);
    }
    return 0;
}
时间: 2024-10-28 22:11:13

Uva 11029 Leading and Trailing (求n^k前3位和后3位)的相关文章

UVa 11029 Leading and Trailing

题目要求输出N的K次方的前三位和后三位.后三位的解法不用多说了,用二分法快速去模即可.关键是前三位怎么求?题目中说N能用32位带符号整数表示,K最大是10的六次方.因此N^K的解ans最多不过10^(9*10^6),因此我们完全可以用以十为底的对数x+y表示,其中x表示对数的整数部分,y表示对数的小数部分.显然,ans的具体数字是由10^y来表示的,而x只是用来将小数以为成整数而已.并且可以确定的是,10^y必小于10且大于1,因为大于10的话,y就大于1了,而小于1的话,y就小于0了显然不可能

UVA 11029 Leading and Trailing(大数n^k的前x位高精度问题)(好题)

Problem C Leading and Trailing Time limit: 2 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

UVA - 11029 - Leading and Trailing (快速幂+公式变形)

题目传送:UVA - 11029 思路:后三位可以直接快速幂取模,然后前三位可以有两种做法,一个是利用double,一个是利用公式法,具体看代码吧 注意,后三位不足三位要补0,即用%03d AC代码①: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #incl

LightOJ 1282 - Leading and Trailing (求n的k次方的前三位数字 后三位)

题意:http://www.lightoj.com/volume_showproblem.php?problem=1282 n^k = a.bc * 10.0^m:等式两边同时加上log10k*log10(n) = log10(a.bc) + m;m为k * log10(n)的整数部分,log10(a.bc)为k * lg(n)的小数部分;x = log10(a.bc) = k*log10(n) - m = k*log10(n) - (int)k*log10(n);x = pow(10.0, x

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

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

UVA - 11029Leading and Trailing(快速幂取模取后三位 + log10()取前三位)

题目: UVA - 11029Leading and Trailing(快速幂取模取后三位 + log10()取前三位) 题目大意:给你N的k次方,然后要求你求出这个数的前三位和后三位. 解题思路:因为n和k都很大,这个数求出来是大数,所以可以用快速幂取模求后三位,因为后面的三位和前面的位数的没有关系.前面的三位比较难办.设x = log (n^k)  = k * log10(n),那么10^x = k * log10(n).将X = a(整数) + b(小数),整数部分10^a只是移动小数点,

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

LightOJ - 1282 Leading and Trailing (数论)

题意:求nk的前三位和后三位. 分析: 1.后三位快速幂取模,注意不足三位补前导零. 补前导零:假如nk为1234005,快速幂取模后,得到的数是5,因此输出要补前导零. 2.前三位: 令n=10a,则nk=10ak=10x+y,x为ak的整数部分,y为ak的小数部分. eg:n=19,k=4,则nk=130321, a=log10(n)=1.2787536009528289615363334757569 ak=5.1150144038113158461453339030277, 因此,x=5,