LightOj 1282 Leading and Trailing
求n^k的前三位数字和后三位数字。
范围: n (2 ≤ n < 231) and k (1 ≤ k ≤ 107).
前三位: 设 n^k = x
---> lg(n^k)=lg(x)
---> klg(n)=lg(x)
---> x=10^(klgn).
因为求前三位,klgn大于2的整数部分可以舍弃。bit=floor(klgn-2), x=10^(klgn-bit)。
后三位:快速幂模1000即可。
代码:
#include <iostream>
#include <cstdio>
#include <cmath> using namespace std;
typedef long long ll; //n (2 ≤ n < 2^31) and k (1 ≤ k ≤ 10^7). ll mod_pow(ll a, ll b, ll mod)
{
ll res = 1;
while (b > 0) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
} int calc(int n, int k)
{
int bit = floor(k * log10(n) - 2);
return floor(pow(10, k * log10(n) - bit));
} int main()
{
int n, k;
int t;
scanf("%d", &t);
for (int cas = 1; cas <= t; ++cas) {
scanf("%d%d", &n, &k);
int first = calc(n, k);
int last = mod_pow(n, k, 1000);
printf("Case %d: %3d %03d\n", cas, first, last);
}
return 0;
}
LightOj 1282 Leading and Trailing的更多相关文章
- LightOJ 1282 Leading and Trailing (快数幂 + 数学)
http://lightoj.com/volume_showproblem.php?problem=1282 Leading and Trailing Time Limit:2000MS Me ...
- UVA 11029 || Lightoj 1282 Leading and Trailing 数学
Leading and Trailing You are given two integers: n and k, your task is to find the most significant ...
- LightOJ 1282 Leading and Trailing 数论
题目大意:求n^k的前三位数 和 后三位数. 题目思路:后三位数直接用快速幂取模就行了,前三位则有些小技巧: 对任意正数都有n=10^T(T可为小数),设T=x+y,则n=10^(x+y)=10^x* ...
- LightOJ 1282 Leading and Trailing (数学)
题意:求 n^k 的前三位和后三位. 析:后三位,很简单就是快速幂,然后取模1000,注意要补0不全的话,对于前三位,先取10的对数,然后整数部分就是10000....,不用要,只要小数部分就好,然后 ...
- LightOJ - 1282 - Leading and Trailing(数学技巧,快速幂取余)
链接: https://vjudge.net/problem/LightOJ-1282 题意: You are given two integers: n and k, your task is to ...
- LightOJ - 1282 Leading and Trailing (数论)
题意:求nk的前三位和后三位. 分析: 1.后三位快速幂取模,注意不足三位补前导零. 补前导零:假如nk为1234005,快速幂取模后,得到的数是5,因此输出要补前导零. 2.前三位: 令n=10a, ...
- 1282 - Leading and Trailing 求n^k的前三位和后三位。
1282 - Leading and Trailing You are given two integers: n and k, your task is to find the most signi ...
- 1282 - Leading and Trailing ---LightOj1282(快速幂 + 数学)
http://lightoj.com/volume_showproblem.php?problem=1282 题目大意: 求n的k次方的前三位和后三位数然后输出 后三位是用快速幂做的,我刚开始还是不会 ...
- light OJ 1282 - Leading and Trailing 数学 || double技巧
http://lightoj.com/volume_showproblem.php?problem=1282 #include <cstdio> #include <cstdlib& ...
随机推荐
- PL/SQL — 变长数组
PL/SQL变长数组是PL/SQL集合数据类型中的一种,其使用方法与PL/SQL嵌套表大同小异,唯一的区别则是变长数组的元素的最大个数是有限制的.也即是说变长数组的下标固定下限等于1,上限可以扩展.下 ...
- NSAssert使用摘抄
#define NSAssert(condition, desc, ...) 只有条件condition满足,才会执行下一个语句,否则输出断言错误. 例如: NSAssert(1 != 2, @&qu ...
- Docker容器中运行ASP.NET Core
在Linux和Windows的Docker容器中运行ASP.NET Core 译者序:其实过去这周我都在研究这方面的内容,结果周末有事没有来得及总结为文章,Scott Hanselman就捷足先登了. ...
- Linux下相关查找文件命令(find locate which whereis type)
以下内容摘自:http://blog.csdn.net/jessica1201/article/details/8139249 标注的内容为自己的补充: 我们经常需要在系统中查找一个文件,那么在lin ...
- 随机产生字母a--z, A-Z 的任意组合
VERSION 1.0 引自: http://www.coderanch.com/t/134491/Security/generating-secure-tokens package demo; ...
- 让Eclipse使用新版本的JRE
更新到新的 Mac OS X 再打开Eclipse 编译程序会报错, Exception in thread "main" java.lang.UnsupportedClassVe ...
- 设置Tomcat的UTF-8编码
利用request.setCharacterEncoding("UTF-8");来设置Tomcat接收请求的编码格式,只对POST方式提交的数据有效,对GET方式提交的数据无效! ...
- SPRING IN ACTION 第4版笔记-第五章BUILDING SPRING WEB APPLICATIONS-004-以query parameters的形式给action传参数(@RequestParam、defaultValue)
一. 1.Spring MVC provides several ways that a client can pass data into a controller’s handler method ...
- SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-011-注入AspectJ Aspect
一. 1. package concert; public interface CriticismEngine { public String getCriticism(); } 2. package ...
- Android 带着用户名的SharedPreferences
/** * 设置当前用户的签到信息 * account&info;account&info * * @param context * @param sign * @author jrj ...