LightOJ 1282 (快速幂+截取前n位输出)

1282 - Leading and Trailing
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

 


Problem Setter: Shamim Hafiz
Special Thanks: Jane Alam Jan (Solution, Dataset)


题意:输入两个数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);



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值