首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【LightOJ】1282 - Leading and Trailing(快速幂 & 数论)

【LightOJ】1282 - Leading and Trailing(快速幂 & 数论)

作者头像
FishWang
发布2025-08-27 11:32:38
发布2025-08-27 11:32:38
13800
代码可运行
举报
运行总次数:0
代码可运行

点击打开题目

1282 - Leading and Trailing

PDF (English)

Statistics

Forum

Time Limit: 2 second(s)

Memory Limit: 32 MB

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

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次幂的前三位和后三位。

前三位用log的方法求,后三位跑一次快速幂就行。

注意后三位的输出,不够要补齐前导0。

代码如下:

代码语言:javascript
代码运行次数:0
运行
复制
#include <stdio.h>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;
#define PI acos(-1)
#define INF 0x3f3f3f3f
#define CLR(a,b) memset(a,b,sizeof(a))
#define LL long long
LL quick(LL a,LL b)
{
	a %= 1000;
	LL ans = 1;
	while (b)
	{
		if (b & 1)
			ans = ans * a % 1000;
		a = a * a % 1000;
		b >>= 1;
	}
	return ans;
}
int main()
{
	LL n,k;
	int u;
	int Case = 1;
	scanf ("%d",&u);
	while (u--)
	{
		scanf ("%lld %lld",&n,&k);
		printf ("Case %d: ",Case++);
		double t = (double)k * (log10(n));
		t -= (LL)t;
		t = pow(10,t);
		t = (LL)(t*100);
		LL ans1 = t;
		LL ans2 = quick(n,k);
		printf ("%lld ",ans1);
		/*if (ans2 < 10)
			printf ("00%lld\n",ans2);
		else if (ans2 < 100)
			printf ("0%lld\n",ans2);
		else
			printf ("%lld\n",ans2);*/
		printf ("%03lld\n",ans2);	//这样输出更方便
	}
	return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Input
  • Output
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档