LightOJ 1282 Leading and Trailing
题意:
输入n^k,输出n^k的前3位与后3位.
思路:
之前做快速幂专题的时候见过这类题,这类题只要记得前m位用log10来求,后m位用mod 10^m来求就基本没问题啦~
f(n)=nktmp=log10f(n)=klog10ntmp=tmp−⌊tmp⌋tmp=10tmp
然后把tmp放大到所需位数就行啦~
代码:
/*
* @author FreeWifi_novicer
* language : C++/C
*/
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<queue>
using namespace std;
#define clr( x , y ) memset(x,y,sizeof(x))
#define cls( x ) memset(x,0,sizeof(x))
#define pr( x ) cout << #x << " = " << x << endl
#define pri( x ) cout << #x << " = " << x << " "
#define mp make_pair
#define pb push_back
typedef long long lint;
typedef long long ll;
typedef long long LL;
const int p = 1e3 ;
lint fast_pow( lint x , lint n ){
x %= p ;
lint res = 1 ;
while( n ){
if( n & 1 )
res = res * x % p ;
n >>= 1 ;
x = x * x % p ;
}
return res ;
}
int pre_ans( lint n , lint k ){
double tmp = k * log10( n * 1.0 ) ;
tmp -= floor( tmp ) ;
tmp = pow( 10 , tmp ) ;
while( tmp < 100 ) tmp *= 10 ;
return int( tmp ) ;
}
int last_ans( lint n , lint k ){
return fast_pow( n , k ) ;
}
int main(){
//freopen("input.txt","r",stdin);
int t ; cin >> t ; int kase = 1 ;
while( t-- ){
lint n , k ;
cin >> n >> k ;
printf( "Case %d: %.3d %.3d\n" , kase++ , pre_ans( n , k ) , last_ans( n , k ) ) ;
}
return 0;
}