LightOJ - 1282

题目:click
题意:求n的k次幂的前三位后三位。

后三位直接取模,千万注意。。。可能有003这种情况保留三位输出。
前三位 n k = x . y 1 0 l e n n^k=x.y*10^{len} 取对数, k l o g 10 ( n ) = l e n + l o g 10 ( x . y ) k*log_{10}(n)=len+log_{10}(x.y) ,可得要知道x.y*100,要知道结果的len(具体位数减1),可推出答案。

#include<cmath>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<istream>
#include<vector>
#include<stack>
#include<set>
#include<map>
#include<algorithm>
#include<queue>
#define inf 0x3f3f3f3f
#define llinf 0x3f3f3f3f3f3f3f3f
#define MAX_len 200005*4
using namespace std;
typedef long long ll;
typedef pair<int,int> PP;
ll qpow(ll a,ll n,ll p)
{
    ll res=1;
    while(n)
    {
        if(n&1)
            res=(res*a)%p;
        n>>=1;
        a=((a%p)*(a%p))%p;
    }
    return res;
}
int main()
{
    cout<<log10(100)<<endl;
    int T;
    scanf("%d",&T);
    int cases=1;
    while(T--)
    {
        ll n,k,i,j;
        scanf("%lld %lld",&n,&k);
        ll t1=qpow(n,k,1000);
        ll len=k*log10(n);
        double temp=k*1.0*log10(n)-len*1.0;
        double tmp=pow((double)10,temp);
        ll ans=tmp*100;
        printf("Case %d: %lld %03lld\n",cases++,ans,t1);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43958964/article/details/106466642