LightOJ 1282 Leading and Trailing(截取前n位输出)
2015-09-19 00:13
369 查看
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=10tmpf(n)=n^k\\
tmp=\log_{10}f(n)=k\log_{10}n\\
tmp=tmp-\lfloor tmp \rfloor\\
tmp=10^{tmp}
然后把tmp放大到所需位数就行啦~
代码:
[code]/* * @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; }
相关文章推荐
- codeforces #320 div 2A - Raising Bacteria (位运算)
- zoj1163The Staircases
- 【CodeForces】80D Time to Raid Cowavans 分块
- 关于retain循环引用对象解决图解
- golang学习笔记之---waitgoup
- LEETCODE-Climbing Stairs
- 解决Command /usr/bin/codesign failed with exit code 1.rtfd
- LeetCode -- Contains Duplicate II
- Thumbnails
- wait()和sleep()的区别
- RAID技术介绍
- UVa 10780 - Again Prime? No Time.
- failed to install sourceTree in win7
- leetcode: (70) Climbing Stairs
- LeetCode -- Contains Duplicate
- WebSessionStore: Could not obtain reference to HttpContext
- LeetCode---Submission Details
- 设计模式之职责链模式(Chain Of Responsibility)
- Conversion to Dalvik format failed:Unable toexecute dex: method ID not in [0, 0xffff]: 65536
- nonatomic, retain,weak,strong用法详解