LightOJ 1282 Leading and Trailing(截取前n位输出)

LightOJ 1282 Leading and Trailing

题意:

输入n^k,输出n^k的前3位与后3位.

思路:

之前做快速幂专题的时候见过这类题,这类题只要记得前m位用log10来求,后m位用mod 10^m来求就基本没问题啦~

f(n)=nktmp=log10f(n)=klog10ntmp=tmptmptmp=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;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值