Leading and Trailing LightOJ - 1282 (quick power)

Leading and Trailing LightOJ - 1282 (quick power)

Topic Link

Meaning of the questions:
namely: a very significant rapid power, seeking the final results of the first three and last three

Solution:
Because here is the large number of related issues, the first thought is certainly a Java BigInteger, but tle the ...
tragedy
after some struggle, or did not want to come out and looked really find solution to a problem or too much food, before power operation on request there are three fixed formula:

int ans1=(int)pow(10.0,2+fmod(k*log10(n),1));

Then, for the three, only in the fast power, a modulo operation can be performed, so the result has been pretty

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<string>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<map>
#define lowbit(x) x&(-x)
#define ll long long
#define inf 0x3f3f3f3f
#define mod 1000
using namespace std;
const double pi=acos(-1.0);
const int maxn=1000010;
const double eps=1e-8;
int t;
int n,k;
int modPow(int a,int b){
    int r=1;
    a%=1000;
    while(b){
        if(b&1){
            r=r*a%mod;
        }
        b>>=1;
        a=a*a%mod;
    }
    return r;
}
int main(){
    scanf("%d",&t);
    int cnt=1;
    while(t--){
        scanf("%d%d",&n,&k);
        int ans1=(int)pow(10.0,2+fmod(k*log10(n),1));
        while(ans1>1000){
            ans1/=10;
        }
        int ans2=modPow(n,k);
        ans2%=1000;         //最终结果可能大于1000,需要再次取模,踩过坑
        printf("Case %d: %d %03d\n",cnt++,ans1,ans2);
    }
    return 0;
}
Published 127 original articles · won praise 32 · views 10000 +

Guess you like

Origin blog.csdn.net/boliu147258/article/details/104270797