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 kn 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);    }}



0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 喝母乳的宝宝不喝奶粉怎么办 我宝宝的牙齿长的不整齐怎么办? 宝宝5个月了母乳不够吃怎么办 4个月宝宝母乳不够吃怎么办 11个月宝宝只吃母乳怎么办 10个月宝宝突然不吃母乳怎么办 2个月宝宝咳嗽怎么办是吃母乳的 九个月宝宝不喜欢吃粥和米糊怎么办 9个月宝宝突然不吃辅食怎么办 宝贝8个月不吃辅食怎么办 11个月宝贝不吃辅食怎么办 十一个月宝宝长牙不吃辅食怎么办 八个多月的宝宝不爱吃辅食怎么办 7个月的宝宝不爱吃辅食怎么办 宝宝只吃母乳不吃辅食怎么办 10个月的宝宝不肯吃勺子怎么办 十一个月大宝宝不吃奶吃饭怎么办 十一个月宝宝光吃奶不吃饭怎么办 20个月宝宝爱吃奶不爱吃饭怎么办 宝宝8个多月不肯米糊跟粥怎么办 8个月宝宝突然不肯吃米糊了怎么办 八个月宝宝突然不吃辅食怎么办 八个月宝宝拉屎费劲还干怎么办 两个月的小孩不喜欢吃奶粉怎么办 三个多月的宝宝不爱吃奶怎么办 4个月的宝宝不吃奶怎么办 九个月宝宝一直高烧不退怎么办 8个月宝宝不吃辅食怎么办 孩子七个月了母乳不够了怎么办 11个月宝宝吃的太多怎么办 宝宝十一个月了上火眼死太多怎么办 9个月孩子不吃辅食怎么办 y一夜没有睡好现在母乳不够怎么办 喂母乳的婴儿总要吃奶怎么办 宝宝四个月了奶水不够吃怎么办 宝宝出生十天了奶水不够吃怎么办 宝宝晚上不吃奶了奶水多怎么办 我家小孩六个月了奶不够吃怎么办 六个月的宝宝奶不够吃怎么办 宝贝六个月了奶不够吃怎么办 奶不够吃宝宝又不吃奶粉怎么办