您的位置:首页 > 大数据 > 人工智能

LightOJ - 1282E - Leading and Trailing(数论)

2017-11-08 16:30 351 查看
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

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

LightOJ1282 Leading and Trailing

http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1282

数论 fmod

fmod题。

求n^k的前三位

n可以写成10^a(a为小数)的形式。

因此原式=10^(ak).

而ak可以写成x+y,其中x为ak的整数部分,y为ak的小数部分

所以x决定了位数,y决定了值

因此求出y即可。

而n=10^a => a=log10(n)

fmod(x,1)可以求出x的小数部分

因此用fmod(ak,1)即可求出y

#include <cstdio>
#include <cmath>
const int Nmax=1e6+7;
const long long mod=1000;
long long qpow(long long base,long long n)
{
base%=mod;
long long ans=1LL;
while(n>0)
{
if(n&1)
ans=(ans*base)%mod;
base=(base*base)%mod;
n>>=1;
}
ans=ans%mod;
return ans;
}
int main()
{

int t;
scanf("%d",&t);
t=0;
long long n,k;
while(scanf("%lld%lld",&n,&k)==2)
{
t++;
double x=pow(10.0,fmod(k*log10(1.0*n),1));
x=x*100.0;
printf("Case %d: %d %03lld\n",t,(int)x,qpow(n,k));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: