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;
}

 

posted @ 2017-04-04 21:03  simpleknight  阅读(301)  评论(0编辑  收藏  举报