Leading and Trailing LightOJ - 1282(数论)

思路

取后3位相当于%1000,直接快速幂取模.
前3位需要一点技巧,nk=10k*log10(n).而考虑到 klog10(n)为小数且值可能非常大我们还要把它对1取模,这是因为klog10(n)的整数部分其实只表示nk的位数,取模用fmod()函数,最后将答案乘100.

代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#include<iostream>
#include<cmath>
#include<string>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include <iomanip>
#include<sstream>
#define INF 0x3f3f3f3f
#define mod 1000
#define MAXN 1001000
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
/*----------------------------------------*/
int n,k;
int pow_mod(int n,int k)
{
    
    
    if(k==1)
        return n%mod;
    int t=pow_mod(n,k/2)%mod;
    int ans=(t*t)%mod;
    if(k%2)ans*=n%mod;
    return ans%mod;
}
int main()
{
    
    
//    ios::sync_with_stdio(false);
    int t;
    cin>>t;
    for(int cas=1; cas<=t; cas++)
    {
    
    
        cin>>n>>k;
        double x=pow(10.0,fmod(k*log10(1.0*n),1));
        cout<<"Case "<<cas<<": ";
        printf("%03d %03d\n",(int)(x*100),pow_mod(n,k));
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43638337/article/details/103914877