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& ...
随机推荐
- MJRefreshFooterView
实例化header和footer _header = [MJRefreshHeaderView header]; _header.scrollView = _tableView; 设置header和f ...
- 关于 js 2个数组取差集怎么取
关于 js 2个数组取差集怎么取? 例如求var arr1 = [1]; var arr2 = [1,2];的差集方法一: Array.prototype.diff = function(a) { r ...
- linux删除某类型文件的命令
使用linux命令行,删除某目录下某类型的文件,如:删除.rar结尾的所有文件. 命令如下: find . -name "*.rar" -type f -print -exec r ...
- res\menu\main.xml:6: error: No resource identifier found for attribute 'showAsAction' in package 'com.xxx.xxxx'
res\menu\main.xml:6: error: No resource identifier found for attribute 'showAsAction' in package 'co ...
- php smarty 配置四个存放目录
require("Smarty.class.php"); $smarty = new Smarty(); $smarty -> template_dir = "./ ...
- [杂题]CSUOJ1276 Counting Route Sequence
题目链接 题意:从1号点走到n号点(每条边只能走一次, 两结点间的边数必定为奇数) 问 经过结点不同顺序的方式有多少种(如1->2->3->4和1->3->2->4 ...
- UVALive 3211 Now or later
每架飞机有早晚起降两种方式,给定n架飞机两种方式的起落时间,为每架飞机安排起落时间(早或晚),使得所有飞机起降时间按照早到晚的顺序之间的间隔时间最小值尽量大. 分析: 最小时间尽量大应该采用二分的方法 ...
- Java中的比较
Java SE.Java EE.Java ME区别 是什么: Java SE=Java Standard Edition=J2SE= Java标准版 Java EE=Java Enterprise E ...
- poj2749
万变不离其宗 只要搞清楚题目的基本模型 搞清楚边是一种推导出的关系 搞清楚里面的逻辑关系 那就没什么难的了…… 二分+sat,没什么好说的 ; type node=record point,next: ...
- Rabin-Miller算法
首先附上matrix67大神的讲解: --------------------------------------------------------------------------------- ...