首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【HDU】1061 - Rightmost Digit(快速幂)

【HDU】1061 - Rightmost Digit(快速幂)

作者头像
FishWang
发布2025-08-27 08:58:46
发布2025-08-27 08:58:46
12100
代码可运行
举报
运行总次数:0
代码可运行

点击打开题目

Rightmost Digit

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 47404 Accepted Submission(s): 17900

Problem Description

Given a positive integer N, you should output the most right digit of N^N.

Input

The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow. Each test case contains a single positive integer N(1<=N<=1,000,000,000).

Output

For each test case, you should output the rightmost digit of N^N.

Sample Input

代码语言:javascript
代码运行次数:0
运行
复制
   2
3
4

Sample Output

代码语言:javascript
代码运行次数:0
运行
复制
   7
6


   
    
     Hint
    
In the first case, 3 * 3 * 3 = 27, so the rightmost digit is 7.
In the second case, 4 * 4 * 4 * 4 = 256, so the rightmost digit is 6.

Author

Ignatius.L

以前用观察找规律然后打表的方法做的,其实用快速幂也可以的,时间上也亏不了多少,代码相对就短很多了。

打表的代码:点击打开链接

代码如下:

代码语言:javascript
代码运行次数:0
运行
复制
#include <cstdio>
int cal(int n,int m)
{
	int ans = 1;
	n %= 10;
	while (m)
	{
		if (m & 1)
			ans = (ans * n) % 10;
		n = (n * n) % 10;
		m >>= 1;
	}
	return ans % 10;
}
int main()
{
	int n;
	int u;
	scanf ("%d",&u);
	while (u--)
	{
		scanf ("%d",&n);
		printf ("%d\n",cal(n,n));
	}
	return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

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