[LightOJ - 1282] Leading and Trailing

编程入门 行业动态 更新时间:2024-10-26 22:21:18

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

本文发布于:2023-06-13 09:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/678895.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:LightOJ   Trailing   Leading

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!