题目链接: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);
}
}

LightOJ1282 Leading and Trailing —— 指数转对数的更多相关文章

  1. LightOJ-1282 Leading and Trailing 模算数 快速幂 对数的用法

    题目链接:https://cn.vjudge.net/problem/LightOJ-1282 题意 给出两个正整数n(2 ≤ n < 231), k(1 ≤ k ≤ 1e7) 计算n^k的前三 ...

  2. LightOJ1282 Leading and Trailing

    题面 给定两个数n,k 求n^k的前三位和最后三位 Input Input starts with an integer T (≤ 1000), denoting the number of test ...

  3. 【LightOJ1282】Leading and Trailing(数论)

    [LightOJ1282]Leading and Trailing(数论) 题面 Vjudge 给定两个数n,k 求n^k的前三位和最后三位 题解 这题..真的就是搞笑的 第二问,直接输出快速幂\(m ...

  4. Leading and Trailing(数论/n^k的前三位)题解

    Leading and Trailing You are given two integers: n and k, your task is to find the most significant ...

  5. E - Leading and Trailing 求n^k得前三位数字以及后三位数字,保证一定至少存在六位。

    /** 题目:E - Leading and Trailing 链接:https://vjudge.net/contest/154246#problem/E 题意:求n^k得前三位数字以及后三位数字, ...

  6. Leading and Trailing LightOJ - 1282 题解

    LightOJ - 1282 Leading and Trailing 题解 纵有疾风起 题目大意 题意:给你一个数n,让你求这个数的k次方的前三位和最后三位. \(2<=n<2^{31} ...

  7. LightOJ 1282 Leading and Trailing (快数幂 + 数学)

    http://lightoj.com/volume_showproblem.php?problem=1282 Leading and Trailing Time Limit:2000MS     Me ...

  8. Leading and Trailing (数论)

    Leading and Trailing https://vjudge.net/contest/288520#problem/E You are given two integers: n and k ...

  9. UVA-11029 Leading and Trailing

    Apart from the novice programmers, all others know that you can’t exactly represent numbers raised t ...

随机推荐

  1. SQLite的sqlite_master表

    SQLite的sqlite_master表   sqlite_master表是SQLite的系统表.该表记录该数据库中保存的表.索引.视图.和触发器信息.每一行记录一个项目.在创建一个SQLIte数据 ...

  2. xcopy中文文件名,中文件目录乱码问题

    1.保存成bat脚本文件 2.且该bat文件不能使用utf-8格式,使用ANSI即正常

  3. 投资人王刚口述:滴滴如何用八十万成为百亿美金公司? zz

    作者|李好福布斯杂志中文版采编 阿里巴巴前高管.滴滴打车天使投资人王刚近日在杭州接受了<福布斯>独家专访,讲述了集齐“阿里的人.百度的技术.腾讯的钱”的滴滴如何从八十万启动资金,在三年内成 ...

  4. nodeJS一些事儿

    node-webkit:开发桌面+WEB混合型应用的神器[大漠穷秋] 展望未来 其实这条路老早就有人在走 网上有很多人在争论,未来究竟是原生的应用会胜出,还是WEB APP会胜出,实际上这两者并不是你 ...

  5. 【IE】IE对line-height 失效的的解决方案

    微软的IE9 + Extjs3.1 确实头疼.在使用了line-height:20px 的Tree的样式,可是一直没有生效, 以下给出3中解决方式: 方案1.加padding-top: <div ...

  6. Solidworks输出Autocad的DWG格式乱码怎么办

    Solidworks输出DWG会有很多问题,如果没必要就别这么做,比如你只是想要打印图纸,Solidworks也可以直接打印,而且很方便,不需要转成DWG再打印,如果对方确实需要DWG格式的图纸,你只 ...

  7. css3: background-image使用多个背景图像

    CSS3 允许元素使用多个背景图像. background-image: url(img/ic_ms.png),url(img/icon_dutyfree_invite.png); <!DOCT ...

  8. 通俗易懂,什么是.NET?什么是.NET Framework?什么是.NET Core? .Net Web开发技术栈

    通俗易懂,什么是.NET?什么是.NET Framework?什么是.NET Core?   什么是.NET?什么是.NET Framework?本文将从上往下,循序渐进的介绍一系列相关.NET的概念 ...

  9. 三行代码实现.NET MVC统计显示页面的执行时间 超简单的实现方法 分析页面执行效率

    三行代码实现.NET MVC统计显示页面的执行时间 超简单的实现方法 分析页面执行效率    博客页脚处添加了页面执行时间统计显示,如下图所示,也可以直接查看网页页脚处. 实现方法非常简单,只需三行代 ...

  10. C99_变长结构体实现

    /************************************************************************* > File Name: C99_lengt ...