LightOJ1282 Leading and Trailing —— 指数转对数
题目链接:https://vjudge.net/problem/LightOJ-1282
| 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);
}
}
LightOJ1282 Leading and Trailing —— 指数转对数的更多相关文章
- LightOJ-1282 Leading and Trailing 模算数 快速幂 对数的用法
题目链接:https://cn.vjudge.net/problem/LightOJ-1282 题意 给出两个正整数n(2 ≤ n < 231), k(1 ≤ k ≤ 1e7) 计算n^k的前三 ...
- LightOJ1282 Leading and Trailing
题面 给定两个数n,k 求n^k的前三位和最后三位 Input Input starts with an integer T (≤ 1000), denoting the number of test ...
- 【LightOJ1282】Leading and Trailing(数论)
[LightOJ1282]Leading and Trailing(数论) 题面 Vjudge 给定两个数n,k 求n^k的前三位和最后三位 题解 这题..真的就是搞笑的 第二问,直接输出快速幂\(m ...
- Leading and Trailing(数论/n^k的前三位)题解
Leading and Trailing You are given two integers: n and k, your task is to find the most significant ...
- E - Leading and Trailing 求n^k得前三位数字以及后三位数字,保证一定至少存在六位。
/** 题目:E - Leading and Trailing 链接:https://vjudge.net/contest/154246#problem/E 题意:求n^k得前三位数字以及后三位数字, ...
- Leading and Trailing LightOJ - 1282 题解
LightOJ - 1282 Leading and Trailing 题解 纵有疾风起 题目大意 题意:给你一个数n,让你求这个数的k次方的前三位和最后三位. \(2<=n<2^{31} ...
- LightOJ 1282 Leading and Trailing (快数幂 + 数学)
http://lightoj.com/volume_showproblem.php?problem=1282 Leading and Trailing Time Limit:2000MS Me ...
- Leading and Trailing (数论)
Leading and Trailing https://vjudge.net/contest/288520#problem/E You are given two integers: n and k ...
- UVA-11029 Leading and Trailing
Apart from the novice programmers, all others know that you can’t exactly represent numbers raised t ...
随机推荐
- java 8 LocalDateTime 20 例
http://www.importnew.com/15637.html 伴随lambda表达式.streams以及一系列小优化,Java 8 推出了全新的日期时间API,在教程中我们将通过一些简单的实 ...
- Windows下Python的虚拟环境
前言 在开发python的应用程序的时候,有时候会遇到依赖包的版本问题,比如之前开发tensorflow应用的时候需要用到python3.5以下的版本(还是python3),但是日常做其它应用的时候用 ...
- GCD CoreData 简化CoreData操作(转)
来自会员带睡帽的青蛙的分享: 短话长说,开始写这个小工具到现在有两个月了,虽然东西少,但是很精练,改了又改,期间有不少问题 在坛子里获得了不少帮助 谢谢各位大大. 就是两个文件一个类 CoreData ...
- Objective-C 的 API 设计(转)
英文原文:API Design 转自oschina 参与翻译(14人): 李远超, 魏涛, showme, weizhe72, 周荣冰, crAzyli0n, WangWenjing, throwab ...
- webpack+vue起步
本文基于vue1.x 基于vue2.x&webpack2.x请移步至 Vue2.x踩坑与总结Webpack2.x踩坑与总结 记得第一次知道Vue.js是在勾三股四大大的微博,那时候他开始翻译v ...
- 3D空间中射线与轴向包围盒AABB的交叉检测算法 【转】
http://blog.csdn.net/i_dovelemon/article/details/38342739 引言 在上一节中,我讲述了如何实现射线与三角形的交叉检测算法. 但是,我们应该知道, ...
- python得到最长递增子串长度及起始位置【转】
def incSeq(seq): start = 0 for i in xrange(1, len(seq)): if seq[i] < seq[i-1]: yield start, i - s ...
- QlikView显示所选时间前一年的数据
客户常常提出这种需求,当用户选择某一时间时.图表中显示所选时间之前一年的数据.以下是我的方法.如有不当,请多不吝赐教: 数据准备例如以下所看到的: SalesData: LOAD Num(ID) as ...
- php自己编译安装后,再给这个编译安装的php版本添加拓展模块的处理办法。
原文: https://www.cnblogs.com/zongyl/p/5924627.html 说明,给编译安装之后的php 添加pgsql 拓展成功. --------------------- ...
- UVA 12338 - Anti-Rhyme Pairs(后缀数组+RMQ)
UVA 12338 - Anti-Rhyme Pairs 题目链接 题意:给定一些字符串,每次询问求出两个字符串的最长公共前缀的长度 思路:把字符串排序,就能求出height和rank数组,然后利用R ...