题目链接:https://vjudge.net/problem/LightOJ-1282

1282 - Leading and Trailing
Time Limit: 2 second(s) Memory Limit: 32 MB

You are given two integers: n and k, your task is to find the most significant three digits, and least significant three digits of nk.

Input

Input starts with an integer T (≤ 1000), denoting the number of test cases.

Each case starts with a line containing two integers: n (2 ≤ n < 231) and k (1 ≤ k ≤ 107).

Output

For each case, print the case number and the three leading digits (most significant) and three trailing digits (least significant). You can assume that the input is given such that nk contains at least six digits.

Sample Input

Output for Sample Input

5

123456 1

123456 2

2 31

2 32

29 8751919

Case 1: 123 456

Case 2: 152 936

Case 3: 214 648

Case 4: 429 296

Case 5: 665 669

题意:

求 n^k 的前三位和后三位。2 ≤ n < 231,1≤ k ≤ 107

题解:

1.后三位快速幂求模即可。

2.对于前三位,可知任何一个正整数可以表示为 10^(x+y),为何能表示正整数?联想一下指数函数的曲线。规定x是整数,y是小于1的浮点数。因为 10^(x+y) = 10^x*10^y,所以,10^x决定了这个数的最高位为第x位,10^y决定了这个数的数值。类似于科学计数法 a*10^b,其中 10^y对应a, 10^y对应10^b。

3.有了上述结论,那么就可以: n^k = 10^(x+y)。两边取对数,得k*log10(n) = x+y。由于我们需要的是数值,而不是位数,所以只取对数值有用的信息,即y,自带函数fmod()能实现这个功能。得到y之后,10^y就是数值了,由于要前三位,所以直接乘以100即可。

4.很多时候,指数都不太好计算,一般都转化为对数进行操作(高数经常有这样的转化)。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int MAXM = 1e5+;
const int MAXN = 5e5+; LL qpow(LL x, int y)
{
LL s = ;
while(y)
{
if(y&) s = (1LL*s*x)%;
x = (1LL*x*x)%;
y >>= ;
}
return s;
} int main()
{
int T, kase = ;
scanf("%d", &T);
while(T--)
{
LL n, k;
scanf("%lld%lld", &n,&k);
int most = *pow(, fmod(k*log10(n),));
int least = qpow(n, k);
printf("Case %d: %03d %03d\n", ++kase, most, least);
}
}

最新文章

  1. PHP过滤外部链接及外部图片 添加rel=&quot;nofollow&quot;属性
  2. Xcode配置.pch文件
  3. ERROR: The partition with /var/lib/mysql is too full! failed!
  4. homework-04 抓瞎
  5. 146. LRU Cache
  6. 【网络流#1】hdu 3549 - 最大流模板题
  7. HDU 3401 Trade(单调队列优化)
  8. JDNI
  9. 程序媛也话Android 之 自定义控件(垂直方向滑动条)
  10. ubuntu更换开机动画
  11. 新手立体四子棋AI教程(1)——基础扫盲
  12. LeetCode &amp; Q119-Pascal&#39;s Triangle II-Easy
  13. MyEclipse代码提示设置
  14. 如何伪造IP(转)
  15. Commons Daemon procrun stdout initialized
  16. Python中数字之间的进制转换
  17. 手机app数据的爬取之mitmproxy安装教程
  18. .Net 中读写Oracle数据库常用两种方式
  19. exception is java.lang.IllegalArgumentException: No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make su re that file is correct.
  20. c#传统SqlTransaction事务和TransactionScope事务

热门文章

  1. oceanbase 分布式数据库
  2. Android 中的Canvas画图
  3. alibaba fastjson常见问题FAQ
  4. pycharm的todo和fixme标记,标志为今后再做和bug点
  5. python 工具 图片批量合并
  6. 设计模式——介绍与工厂模式(扁平管理模式VS职业经理人模式)
  7. Cocos2d-X中的粒子
  8. 使用JDBC连接SQL Server
  9. Android对apk源代码的改动--反编译+源代码改动+又一次打包+签名【附HelloWorld的改动实例】
  10. Canvas学习笔记——缓动