uva11029 - Leading and Trailing n^k保留前三位
来源:互联网 发布:二手工作站笔记本 知乎 编辑:程序博客网 时间:2024/06/07 15:09
Problem C Leading and Trailing Time limit: 2 seconds
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
这道题后三位快速幂取模就行了,但是前三位怎么做是个难点。
n^k=10^(logn^k),设x是logn^k的整数部分,y是logn^k的小数部分,那么n^k=10^(x+y)=10^x * 10^y。由于10^x只改变小数点的位数,对数的值是不影响的。0<=y<1,所以1<=10^y<10,要保留3位,所以最后再乘以100就行了。
这里有个新函数咯,fmod(a,b),就是a对b取模余下的包括小数部分,a,b都是浮点型,所以fmod(k*log10(n),1)就是y。
#include<cstring>#include<cstdio>#include<iostream>#include<cmath>#include<algorithm>#include<queue>#define INF 0x3f3f3f3fusing namespace std;long long bigpow(long long x,long long n,int M){ long long ret=1,t=x%M; while(n){ if(n&1) ret=ret*t%M; t=t*t%M; n>>=1; } return ret;}int main(){ freopen("in.txt","r",stdin); int T; scanf("%d",&T); while(T--){ int N,K,left,right; scanf("%d%d",&N,&K); left=100*pow(10,fmod(K*log10(N),1)); right=bigpow(N,K,1000); printf("%d...%03d\n",left,right); } return 0;}
- uva11029 - Leading and Trailing n^k保留前三位
- LightOJ 1282 - Leading and Trailing (求n^k的前三位和后三位)
- lightoj 1282 Leading and Trailing(求n^k的前三位后三位)
- uva11029 - Leading and Trailing
- LightOJ 1282 Leading and Trailing(n阶的前三位,后三位)
- UVa 11029 Leading and Trailing (如何计算n^k的开头三位和末尾三位?)
- UVa 11029 Leading and Trailing (如何计算n^k的开头三位和末尾三位?)
- LightOJ 1282 Leading and Trailing(n^k的高三位和低三位)
- uva11029 - Leading and Trailing(头和尾)
- LightOJ 1282 Leading and Trailing(取n^k的最高三位数字)
- UVa 11029 - Leading and Trailing 求n^k的前3位
- UVA - 11029Leading and Trailing(快速幂取模取后三位 + log10()取前三位)
- UVa 11029 - Leading and Trailing 数学题(求n^k的前N位和后N位)
- LightOJ1282 Leading and Trailing(截取前n位输出)
- 【Loj 1282 】 Leading and Trailing 【a^b 的前3位and后三位】
- Leading and Trailing (快速取模和数字取前三位)
- UVA 11029 Leading and Trailing(大数n^k的前x位高精度问题)(好题)
- Lightoj1282——Leading and Trailing(幂取模求前三位)
- Android中如何显示本地HTML
- Linux防火墙的配置
- 如何让自己桌面文件默认路径在其它盘呢?
- 编写设计一个People(人)类。该类的数据成员有年龄(age)、身高(height)、体重(weight)和人数(num),其中人数为静态数据成员,成员函数有构造函数(People)、进食(Eatt
- coco2d-x版的植物大战僵尸
- uva11029 - Leading and Trailing n^k保留前三位
- 软件工程视频(前四章小结-图)
- 文件输入输出操作
- 【Cocos2d-x】Win7+ vs2012,2010 + Cocos2d-x2.2 配置Cocos2d-X项目-环境
- 文件
- 定义Person(人)类,由Person分别派生出Teacher(教师)类和Cadre(干部)类,再由Teacher(教师)类和Cadre(干部)类采用多重继承方式派生出新类TeacherCadre(
- COCOS2D-X 2.1.3没有CCListView,被CCTableView代替了
- linux下SVN服务搭建:apache+svn+usvn+php+mysql
- HDU 2021 - 发工资咯:)(贪心)