UVa 11029 Leading and Trailing (如何计算n^k的开头三位和末尾三位?)
2013-11-04 09:03
447 查看
11029 - Leading and Trailing
Time limit: 3.000 secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=1970
Apart from the novice programmers, all others know that you can’t exactly represent numbers raised to some high power. For example, the C functionpow(125456, 455) can be represented in double data type format, but you won’t get all the digits
of the result. However we can get at least some satisfaction if we could know few of the leading and trailing digits. This is the requirement of this problem.
Input
The first line of input will be an integer T<1001, where
T represents the number of test cases. Each of the next T lines contains two positive integers,n andk.
n will fit in 32 bit integer andk will be less than 10000001.
Output
For each line of input there will be one line of output. It will be of the format LLL…TTT, where LLL represents the first three digits ofn^k and TTT represents the last three digits ofn^k. You are assured thatn^k
will contain at least 6 digits.
Sample Input | Output for Sample Input |
2 123456 1 123456 2 | 123...456 152...936 |
那前三位怎么求呢?——对数
令x=lg(n^k)的整数部分,y=lg(n^k)的小数部分,则n^k由y决定——因为10^x是1000...0。所以10^y再乘上100取整就是前三位。
完整代码:
/*0.015s*/ #include<cstdio> #include<cmath> #define sf scanf #define pf printf typedef long long ll; const int mod = 1000; ll pow_mod(int n, int k) { if (k == 0) return 1; ll temp = pow_mod(n, k >> 1); temp = temp * temp % mod; if (k & 1) temp = temp * n % mod;///注意这里中间运算结果会超int return temp; } int main() { int t, n, k; double intpart; sf("%d", &t); while (t--) { sf("%d%d", &n, &k); pf("%d...%03lld\n", (int)pow(10.0, 2.0 + modf((double)k * log10(n), &intpart)), pow_mod(n, k)); } return 0; }
相关文章推荐
- RAID 介绍
- Container With Most Water 求容器里能放最多的水 @LeetCode
- Daily Scrum 11.2
- 如何获得一个月以前的日期,需要兼容AIX SUSE HP-UX
- 拆解iPad Air,内部构造大曝光
- Daily Scrum 11.1
- 11.3Daily Scrum
- Daily Scrum5
- Daily Scrum 11.3
- LINK : error LNK2001 无法解析的外部符号_mainCRTStartup
- HDFS-HA的配置-----自动Failover(ZKFC)
- 人机接口介绍(Brain-computer interface)
- Copy/Retain/Assign
- main(int argc,char **argv)
- UVA 253 Cube painting
- Daily Scrum 10.31
- Vmware10下安装Mac OS X Mountain Lion10.8.5
- 第一款基于Samurai引擎的WP游戏《MarioJump》成功登陆应用商店~
- 【KMP】hdu 1867 A + B for you again(外:hdu 2594 Simpsons’ Hidden Talents)
- 收集的图像处理网站http://blog.csdn.net/chief1985/article/details/1898358