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 . . . T T T, where LLL represents the first three digits of n^k and T T T represents the last three digits of n^k. You are assured that n k will contain at least 6 digits.
Sample Input
2
123456 1
123456 2
Sample Output
123…456
152…936

问题链接:UVA11029 Leading and Trailing
问题简述:(略)
问题分析
    计算n^k的前3位和后3位。
    后3位用快速模幂来计算。
    前3位则根据数学公式,按浮点数进行计算。假设n^K= X 10 ^(len-1) ,其中X是规范化表示的小数点前唯一的1位10进制数字,len是10进制数的长度,那么两边取对数后得Klog10(n)=(len-1)+log10(X);令T=Klog10(n),那么根据取对数后的等式,去除整数部分,tmp的小数部分=log(X),故T=10^X,即X=pow(10,T)。而T值用程序语言的表达式来表示的话,T=Klog10(n)-(long long)K*log10(n),整数用long long类型。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++程序如下:

/* UVA11029 Leading and Trailing */#include <bits/stdc++.h>using namespace std;typedef long long LL;
const int MOD = 1000;// 快速模幂
LL powmod(LL x, LL n, LL m)
{LL result = 1;for(; n; n >>= 1) {if(n & 1) {result *= x;result %= m;}x *= x;x %= m;}return result;
}int main()
{int t;LL n, k, ans1, ans2;scanf("%d",&t);while(t--) {scanf("%lld%lld", &n, &k);ans2 = powmod(n, k, MOD);double tmp = k * log10(n);tmp=pow(10.0, tmp - (long long)tmp);ans1 = (LL)(tmp * 100);printf("%lld...%03lld\n", ans1, ans2);}return 0;
}

UVA11029 Leading and Trailing【快速模幂+数学】相关推荐

  1. 51Nod-1046 A^B Mod C【快速模幂】

    1046 A^B Mod C 基准时间限制:1秒 空间限制:131072KB 分值:0难度:基础题 给出3个正整数A B C,求A^B Mod C. 例如,3 5 8,3^5 Mod 8 = 3. I ...

  2. HDU1163 Eddy's digital Roots(解法二)【快速模幂+九余数定理】

    问题链接:HDU1163 Eddy's digital Roots. 问题简述:参见上述链接. 问题分析:计算n^n的数根,一要快,二要简单.使用快速模幂计算,加上数论中的九余数定理就完美了. 程序说 ...

  3. CodeForces - 1514B AND 0, Sum Big【快速模幂】

    B. AND 0, Sum Big time limit per test2 seconds memory limit per test256 megabytes inputstandard inpu ...

  4. HDU1420 Prepared for New Acmer【快速模幂】

    Prepared for New Acmer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...

  5. UVA11582 Colossal Fibonacci Numbers!【快速模幂+数列模除】

    The i'th Fibonacci number f(i) is recursively defined in the following way: • f(0) = 0 and f(1) = 1 ...

  6. HDU1163 Eddy's digital Roots(解法二)【快速模幂+九余数定理】(废除!!!)

    本文废除,参见下述链接. 参考链接:HDU1163 Eddy's digital Roots[快速模幂+九余数定理+水题] 问题链接:HDU1163 Eddy's digital Roots. 问题简 ...

  7. POJ1845 Sumdiv【快速模幂+素因子分解+等比数列+二分法】

    问题链接:POJ1845 Sumdiv. 问题简述:参见上述链接. 问题分析:计算a^b的因子数,首先要对a进行因子分解,然后再进行计算. 程序说明:计算过程中用到了快速模幂函数. 题记:(略) AC ...

  8. 51Nod-1013 3的幂的和【快速模幂+逆元】

    1013 3的幂的和 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 求:3^0 + 3^1 +...+ 3^(N) mod 1000000007 Input 输入 ...

  9. HDU1061 Rightmost Digit【快速模幂】

    问题链接:HDU1061 Rightmost Digit. 问题简述:参见上述链接. 问题分析:一个简单的快速模幂计算. 程序说明:快速模幂计算被封装到一个函数中,直接调用即可. 题记:(略) AC的 ...

最新文章

  1. python反射实例化_Python类反射机制使用实例解析
  2. 认识哈希函数(散列函数)
  3. python数据结构与算法(13)
  4. ZooKeeper入门之数据模型和常用命令介绍
  5. JVM 的内存结构和内存分配
  6. linux共享内存的定义,共享内存是什么意思 Linux系统如何共享内存
  7. 打印设置自定义表尾_如何自定义凭证汇总表打印页面设置
  8. 【java】java中文件监控WatchService使用
  9. 保持皮肤水嫩有光泽,让皮肤保湿的土办法 - 生活至上,美容至尚!
  10. python中的[1:]、[::-1]、X[:,m:n]和X[1,:]
  11. 新版IAR软件打开旧版本创建的项目时出现的三个错误及其解决方案 is too long for segment definition
  12. Radius认证协议(六)报文属性
  13. Apple苹果iOS数据丢失如何恢复?
  14. 常见知识库对比:DBpedia/Freebase/OpenCyc/Wikidata/YAGO3
  15. 计算机组成原理学习笔记一
  16. 中继链路trunk详解
  17. 基于MATLAB的电弧仿真模型(Mayr/Cassie 电弧模型)
  18. Xcode直接安装ipa
  19. Eclipse集成Mybatis Generator及应用
  20. 头歌--C++之if-else基本性质

热门文章

  1. 树莓派Linux内核编译选项如何开启TPM 2.0
  2. dojo——AMD(二、AMD中class内部成员函数相互调用实现)
  3. sass-loader高版本的坑
  4. 5gh掌上云计算认证不通过_5GH币项目介绍及评级分析
  5. git忽略文件或者文件夹
  6. vertica数据库将一个字段用逗号分割与拼接
  7. anaconda3环境整体打包放在Spark集群上运行
  8. 完解决MySQL57版登录报ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)的问题
  9. android期末大作业_关于大学期末的一点碎碎念
  10. oracle 数字处理函数,ORACLE 数字型函数