admin管理员组文章数量:1530517
Description
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 n^k contains at least six digits.
Sample Input
5
123456 1
123456 2
2 31
2 32
29 8751919
Sample Output
Case 1: 123 456
Case 2: 152 936
Case 3: 214 648
Case 4: 429 296
Case 5: 665 669
解析
求n的k次方,取前三位和后三位。
后三位比较简单,就是一个快速幂取模1000的。
但是前三位比较陌生,之前都没有遇见过。看了别人的解释理解了蛮久。
n^k=10^x
x=log10(n^k)
x=k*long10(n)
x是一个带有小数的东西
10^整数部分=它的位数-1
10^小数部分=各个位数
举例来理解:
123=10^(2+0.0899051114394)=10^2 * 10^(0.0899051114394)
10^2=100
10^(0.0899051114394)=1.23
最后发现就是科学记数法!啊哈
所以我们要通过 x=k*long10(n)来算到它的小数部分再*100就得到前三位了
代码
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <cmath>
using namespace std;
long long quickmod(long long a,long long b,long long mod)
{
long long ans=1;
if(!b) return 1;
ans=quickmod(a*a%mod,b>>1,mod);
if(b&1) ans=ans*a%mod;
return ans;
}
int main()
{
int t,cnt=0;
scanf("%d",&t);
while(t--)
{
long long n,k;
scanf("%lld%lld",&n,&k);
int start=(int)pow(10.0,2.0+fmod(k*log10(n*1.0),1.0));
//前三位,fmod得到的是小数部分,pow作用等价于*100
int end=(int)quickmod(n,k,1000);//后三位
printf("Case %d: %d %03d\n",++cnt,start,end);
}
return 0;
}
版权声明:本文标题:[LightOJ - 1282] Leading and Trailing 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/xitong/1725721909a1038479.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论