博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVa 11029 Leading and Trailing (如何计算n^k的开头三位和末尾三位?)
阅读量:3607 次
发布时间:2019-05-21

本文共 1668 字,大约阅读时间需要 5 分钟。

 

分类:  
 
430人阅读 
(1) 
 

11029 - Leading and Trailing

Time limit: 3.000 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 nextT lines contains two positive integers,n andkn 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

思路:后三位好求,mod=1000的快速幂就是。

那前三位怎么求呢?——对数

令x=lg(n^k)的整数部分,y=lg(n^k)的小数部分,则n^k由y决定——因为10^x是1000...0。所以10^y再乘上100取整就是前三位。

完整代码:

[cpp] 
  1. /*0.015s*/  
  2.   
  3. #include<cstdio>  
  4. #include<cmath>  
  5. #define sf scanf  
  6. #define pf printf  
  7. typedef long long ll;  
  8. const int mod = 1000;  
  9.   
  10. ll pow_mod(int n, int k)  
  11. {  
  12.     if (k == 0) return 1;  
  13.     ll temp = pow_mod(n, k >> 1);  
  14.     temp = temp * temp % mod;  
  15.     if (k & 1) temp = temp * n % mod;///注意这里中间运算结果会超int  
  16.     return temp;  
  17. }  
  18.   
  19. int main()  
  20. {  
  21.     int t, n, k;  
  22.     double intpart;  
  23.     sf("%d", &t);  
  24.     while (t--)  
  25.     {  
  26.         sf("%d%d", &n, &k);  
  27.         pf("%d...%03lld\n", (int)pow(10.0, 2.0 + modf((double)k * log10(n), &intpart)), pow_mod(n, k));  
  28.     }  
  29.     return 0;  
  30. }  

转载地址:http://ystzn.baihongyu.com/

你可能感兴趣的文章
ProFTPD:Limit配置
查看>>
IDEA恢复布局
查看>>
重定向和请求转发的区别
查看>>
Map、Set、List集合区别(看完秒懂)
查看>>
普通用户使用docker命令遇到提示需要提升权限时的解决方法
查看>>
webpack打包技术
查看>>
Leecode 面试题09用两个栈实现队列
查看>>
fastdfs连接超时报错解决方案
查看>>
Leecode202. 快乐数
查看>>
windows10解决80端口被占用的问题
查看>>
ElasticSearch快速入门之创建索引库、创建映射、创建文档、搜索文档
查看>>
用故事巧妙帮助理解公钥和私钥的区别和联系
查看>>
application.properties 文件和 application.yml 文件区别以及加载顺序
查看>>
阿里云服务器安装docker,拉取常用的mysql,redis,nginx等镜像
查看>>
为什么timestamp到2038年就截止了?
查看>>
设计模式之适配器模式
查看>>
设计模式之工厂模式
查看>>
设计模式之原型模式
查看>>
设计模式之对象池模式
查看>>
设计模式之责任链模式 Java实例代码 + Tomcat责任链模式应用+安卓责任链模式应用
查看>>