LightOJ - 1282

LightOJ - 1282

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 < 2^31)and k (1 ≤ k ≤ 10^7).
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.

题目大意
求 n 的 k 次方结果的前三位数 和 后三位数

快速幂的话可以单独去学一下,我记得当时我是通过位运算的思想去理解的
Sample Input

5
123456 1
123456 2
2 31
2 32
29 8751919

Sample Output

5
123456 1
123456 2
2 31
2 32
29 8751919
#include<stdio.h>
#include<math.h>
#include<string.h>
int main()
{
	int t,z=0,s;
	scanf("%d",&t);
	long long n,k;
	while(t--)
	{
		s=1;
		z++;
		scanf("%lld %lld",&n,&k);
		long long k1=k,m=n;
		while(k1)//快速幂
		{
			if(k1&1)// 判断奇偶 //  3 0 0 1 1
								//& 1 0 0 0 1
								//		  0 1		
			s=s*m%1000;
			m=m*m%1000;
			k1/=2;
		}
		// 转化成类似科学计数法
		//123456 = 1.23456 x 10^5 = 10^n x 10^5 (n为1.23456的常用对数)
		
		double p=k*log10(n);
		long long g=p;   //把 p 的整数位给 g
		int h=pow(10,p-g)*100;  //取小数位
		printf("Case %d: ",z);
		printf("%d %03d\n",h,s);
	}
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值