LightOJ – 1282 Leading and Trailing

题目链接

要取出前三位数,可使n^k=a.bc*10^m, 则有klg(n)=m+lg(a.bc),接下来就简单了…

计算用log()涉及到换底,交上去过不了,应该是有误差.但用log10()可以,也是才知道还有log10()这个函数…

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
#define maxn 0x3f3f3f3f
#define ll long long
using namespace std;

int quickpow(int n,int m,int mod){
  int res=1;
  n%=1000;
  while(m){
    if(m&1) res=res*n%mod;
    n=(ll)n*n%mod;
    m>>=1;
  }
  return res;
}

int main() {
  //freopen("in.txt","r",stdin);
  //freopen("out.txt","w",stdout);
  int T,kase=0;
  cin>>T;
  while(T--){  
    int n,k;
    cin>>n>>k;
    int a=quickpow(n,k,1000);
    double t=k*log10(n)-(int)(k*log10(n));
    int b=(int) (pow(10,t)*100);
    printf("Case %d: %d %03d\n",++kase,b,a);
  }
  return 0;
}