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 ...
随机推荐
- [原创][Verilog]个人.v文件书写规范
1> 文件名命名,小些单词为主且从命名中可以大致读出其功能,例如:fpga_power.v,can_ctrl.v等命名. 2> 顶层top文件的输入输出IO信号使用大些表示,且加上”i/o ...
- Kafka应用实践与生态集成
1.前言 Apache Kafka发展至今,已经是一个很成熟的消息队列组件了,也是大数据生态圈中不可或缺的一员.Apache Kafka社区非常的活跃,通过社区成员不断的贡献代码和迭代项目,使得Apa ...
- Java---详解方法传值问题
过程解析: 1.首先执行int[] arr={3,5,6,1,7,9,0},遇到数组先执行等式右边的,{3,5,6,1,7,9,0}会在堆内存中开辟一块空间,分成7小块,下标分别从0~6,先进行系统初 ...
- @RequestParam,@PathVariable,@ResponseBody,@RequestBody,@ModelAttribute学习
1.@RequestParam使用于参数上,用于将请求参数映射到指定参数变量上 例如: @RequestMapping(value="/hello",method=RequestM ...
- 1.【nuxt起步】-nuxt是什么?
百度了解下,简单说就是vue的seo化,因为vue是spa,不支持seo,从本地运行的源码可以看出来,html没有tkd和相关文字,导致百度收录困难,所以nuxt可以很好的解决这个问题, 举个例子:纯 ...
- [反汇编练习] 160个CrackMe之032
[反汇编练习] 160个CrackMe之032. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...
- 几种支持动作模型格式的比较(MD2,MD5,sea3d) 【转】
最近使用了几种不同的模型格式做人物动作的表现,记录一下优缺点 1) MD2 数据内容: 记录了所有动作顶点数据 数据格式: 二进制 动作文件: 动作文件合并在一个模型文件 文件大小: 动作多时很大 ...
- hdu1034 简单模拟
这里开一个二维数组.num[105][2]; 我也不知道N有多少,随便开的, 那么这里num[i][0] 表示当前 第 i 个人拥有的糖果数,num[i][1]表示他上面一个人分给他的糖果数.详 ...
- Android开发——进程间通信之AIDL(二)
0. 前言 不论是Android还是其它操作系统.都会有自己的IPC机制.所谓IPC(Inter-Process Communication)即进程间通信.首先线程和进程是非常不同的概念,线程是CP ...
- was系统错误日志大量出现标识符缺失
原创作品.出自 "深蓝的blog" 博客,深蓝的blog:http://blog.csdn.net/huangyanlong/article/details/46909941 近日 ...