Leading and Trailing - UVa 11029
来源:互联网 发布:python try sys.exit 编辑:程序博客网 时间:2024/05/16 05:04
Apart from the novice programmers, all others know that you can’t exactly represent numbers raised to some high power. For example, the C function pow(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 and k. n will fit in 32 bit integer and k 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 of n^k. You are assured that n^k will contain at least 6 digits.
Sample Input
Output for Sample Input
2
123456 1
123456 2
123...456
152...936
题意:求出n^k的前三位和后三位。
思路:后三位用快速幂求,前三位n^k=pow(10,k*log(n)),再保留三位取整。
AC代码如下:
#include<cstdio>#include<cstring>#include<cmath>using namespace std;int MOD=1000;int main(){ int T,t,n,m,i,j,k,f,ret,p,q,h; scanf("%d",&T); for(t=1;t<=T;t++) { scanf("%d%d",&n,&m); ret=1;f=n%MOD; k=m; while(k) { if(k&1) ret=ret*f%MOD; f=f*f%MOD; k/=2; } q=floor(pow(10,2+fmod(m*log10(n),1) )); printf("%d...%03d\n",q,ret); }}
- UVA 11029 Leading and Trailing
- UVA 11029 - Leading and Trailing
- uva 11029 - Leading and Trailing
- UVA - 11029 Leading and Trailing
- UVA 11029 Leading and Trailing
- UVa:11029 Leading and Trailing
- UVA 11029 - Leading and Trailing
- uva 11029 Leading and Trailing
- UVA 11029 Leading and Trailing
- Leading and Trailing - UVa 11029
- UVa 11029 Leading and Trailing
- 【uva】11029 Leading and Trailing
- UVA - 11029 Leading and Trailing
- UVA 11029 Leading and Trailing
- UVA 11029 Leading and Trailing
- uva 11029 Leading and Trailing
- UVA 11029 Leading and Trailing
- UVA 11029 Leading and Trailing [数学]
- Hadoop YARN配置参数剖析(1)—RM与NM相关参数
- VC中动态添加控件
- tachyon的编译
- 如何精心设计CDN架构?
- Mac OS X 配置 Apache+Mysql+PHP 详细教程
- Leading and Trailing - UVa 11029
- Binder初尝
- FAQ8.15 HashMap和Hashtable有什么区别?
- 责任链设计模式(过滤器、拦截器)
- 日志打包压缩
- Android 自定义可移动土司及多击事件
- 滑轮控件研究四、VelocityTracker的简单研究
- Quartz 2D绘图简介
- 题目1512:用两个栈实现队列