Time Limit: 2 second(s) | Memory Limit: 32 MB |
You are given two integers: n and k, your taskis to find the most significant three digits, and least significant threedigits 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 leadingdigits (most significant) and three trailing digits (least significant). Youcan assume that the input is given such that nk contains atleast 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,让你求n^k的前三位数和后三位数
这个题目不仅要求ab的后3位,还有就是前3位,后3位就是刚刚的快速幂取模运算,把mod写成1000,即可。前三位,需要用到一个知识点,那就是给定一个数n,这个数可以表示成10a,这个a一般是小数,那么nk就等于10ak,这里把ak分为两部分,整数和小数部分,即x和y,那么nk = 10x * 10y,由于x是整数,那么很明显他是用来指定位数的,因为10x肯定是一个10, 100, 10000...之类的数字,也就是说10y才是这个数字的有效部分,我们只要求出10y,然后乘上100最终结果就是我们想要的。因为
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#define mod 1000
#define LL long long
using namespace std;
LL quick(LL n,LL k)
{
LL ans=1,base=n;
while(k)
{
if(k%2!=0)
ans=((ans%mod)*(base%mod)%mod);
base=((base%mod)*(base%mod)%mod);
k/=2;
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
int flag=1;
while(t--)
{
LL n,k;
scanf("%lld%lld",&n,&k);
double ans=log10(n)*((double)k);
ans=ans-(int)ans;
ans=pow(10,ans);
while(ans<100)
ans*=10;
LL sum=quick(n,k);
printf("Case %d: ",flag++);
printf("%d ",(int)ans);
if(sum>100)
printf("%lld\n",sum);
else if(sum>10)
printf("0%lld\n",sum);
else printf("00%lld\n",sum);
}
return 0;
}
n = 10 a 所以 a = log10(n),10 y 就是小数部分,我们用 函数fmod(x, 1),返回x的小数部分 ,然后乘上100即可 fmod返回的是y的值,所以必须计算10 y 才是真实值,所以直接使用10 2 * 10 y 即pow(10, 2 + y);