Rightmost Digit (求n^n最后一位)
2024-08-29 14:45:06
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).
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
2
3
4
3
4
Sample Output
7
6
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.
n^n最后一位,等价于(n%100)^(n%100)的最后一位。
#include<cstdio>
int main()
{
__int64 t,l,b;
scanf("%I64d",&t);
while(t--)
{
__int64 n;
scanf("%I64d",&n);
l=n%;
b=l;
for(__int64 i=;i<l;i++)
{
b=b*l;
b%=;
}
b%=;
printf("%I64d\n",b);
}
}
快速幂
快速幂求n^n;
1 int f1(int a,int b)
2 {
3 int t=1;
4 while(b)
5 {
6 if(b % 2 != 0)
7 {
8 t*=a;
9 b--;
10 }
11 a*=a;
12 b/=2;
13 }
14 return t;
15 }
快速幂求n^n后y位;
1 int f2(int a,int b)
2 {
3 int t=1;
4 while(b)
5 {
6 if(b % 2 != 0)
7 {
8 t=(t*a)%x; //x控制要求的位数
9 b--;
10 }
11 a=(a*a)%x;
12 b/=2;
13 }
14 return t;
15 }
代码
#include<cstdio>
__int64 f(__int64 a)
{
__int64 b=a;
__int64 t=;
while(b)
{
if(b%!=)
{
t=(t*a)%;
b--;
}
a=a*a%;
b/=;
}
return t;
}
int main()
{
__int64 t,a;
scanf("%I64d",&t);
while(t--)
{
scanf("%I64d",&a);
printf("%I64d\n",f(a));
}
}
最新文章
- svn版本控制方案:多分支并行开发,多环境自动部署
- NOIP200003方格取数
- 用BenchmarkDotNet给C#程序做性能测试
- (DT系列一)DTS结构及其编译方法
- DOM方法
- python成长笔记
- 分享几个社交类网站常用并且也是最好用的jquery类库
- SpringMVC+Spring+Hibernate的小样例
- ant安装和验证
- 使用Ueditor编辑器上传图片总结;
- 完整工程,deeplab v3+(tensorflow)代码全理解及其运行过程,长期更新
- Mysql数据库左外连接,右外连接,模糊查询
- .gitinore配置失效问题
- [PHP]算法-拼接最小字典序的实现
- 3.4Python数据处理篇之Numpy系列(四)---ndarray 数组的运算
- JQ 弹出层全屏
- MySQL用sql复制表数据到新表的方法
- iOS如何在一个包上切换正式环境和测试环境
- SparseArray代替HashMap
- Codeforces Round #431