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

2
3
4
 

Sample Output

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.

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));
}
}

最新文章

  1. svn版本控制方案:多分支并行开发,多环境自动部署
  2. NOIP200003方格取数
  3. 用BenchmarkDotNet给C#程序做性能测试
  4. (DT系列一)DTS结构及其编译方法
  5. DOM方法
  6. python成长笔记
  7. 分享几个社交类网站常用并且也是最好用的jquery类库
  8. SpringMVC+Spring+Hibernate的小样例
  9. ant安装和验证
  10. 使用Ueditor编辑器上传图片总结;
  11. 完整工程,deeplab v3+(tensorflow)代码全理解及其运行过程,长期更新
  12. Mysql数据库左外连接,右外连接,模糊查询
  13. .gitinore配置失效问题
  14. [PHP]算法-拼接最小字典序的实现
  15. 3.4Python数据处理篇之Numpy系列(四)---ndarray 数组的运算
  16. JQ 弹出层全屏
  17. MySQL用sql复制表数据到新表的方法
  18. iOS如何在一个包上切换正式环境和测试环境
  19. SparseArray代替HashMap
  20. Codeforces Round #431

热门文章

  1. centos7开启路由转发
  2. poj 2299 Ultra-QuickSort 归并排序求逆序数对
  3. CentOS系统里如何正确取消或者延长屏幕保护自动锁屏功能(图文详解)
  4. 转】在Ubuntu中安装Redis
  5. MyEclipse开启Jquery智能提示
  6. Java BigDecimal类的使用和注意事项
  7. Android与H5互调(通过实例来了解Hybrid App)
  8. qt5.5版本的creator构建套件自动检测为警告
  9. rem手机端页面自适应完美解决方案(最新)
  10. iTOP-4418/6818开发板支持锂电池供电方案