Rightmost Digit

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

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
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的最右边一位。

题解:普通方法进行幂运算再进行取余会超时,需要用到快速幂

 #include<bits/stdc++.h>
using namespace std;
int mod_exp(int a, int b, int c) //快速幂取余a^b%c
{
int res, t;
res = % c;
t = a % c;
while (b)
{
if (b & )
{
res = res * t % c;
}
t = t * t % c;
b >>= ;
}
return res;
}
int main() {
int t;
while(~scanf("%d",&t))
{
while(t--)
{
int n;
scanf("%d",&n);
printf("%d\n",mod_exp(n,n,)%);
}
}
return ;
}

最新文章

  1. .NET使用ZXing.NET生成中间带图片的二维码
  2. 自定义Listview
  3. select_tag in rails about selected not change and onchange()
  4. 不复杂的Autofac注入
  5. Ubuntu14.04安装配置ndnSIM
  6. CSS样式display:none和visibility:hidden的区别
  7. Swift 中的getting和setter的使用
  8. 学渣也要搞 laravel(4)—— 服务 加解密篇
  9. 【转】打包AAC码流到FLV文件
  10. 深度剖析Spark分布式执行原理
  11. 《Tornado介绍》—— 读后总结
  12. I/O系统(一)
  13. xcode项目打不开:incompatible project version问题
  14. vue中使用BetterScroll
  15. 简单的user-based协同过滤算法示例代码
  16. [Robot Framework] 搭建Robot Framework和RIDE(Robot Framework GUI) 的环境
  17. Object [object Object] has no method 'live'
  18. HTML 5 &lt;script&gt; async 属性简单设置代码异步执行
  19. ZH奶酪:如何在Ubuntu上安装Java/管理多个JAVA/设置JAVA_HOME
  20. troubleshooting-When importing query results in parallel, you must specify --split-by.

热门文章

  1. Linux 进程与信号的概念和操作 linux process and signals
  2. springmvc进阶(5):mvc:default-servlet-handler详解
  3. 两种方式(xml+代码)构建SqlSessionFactory+完整实现
  4. unittest单元测试框架之测试结果输出到外部文件(四)
  5. 关于ORA-00257: archiver error. Connect internal only, until freed 错误的处理方法
  6. oracle查询相关注意点
  7. Oracle子查询之简单子查询
  8. phpStudy环境安装SSL证书教程(apache)
  9. linux安装完jenkins无法访问的问题
  10. SQL Server 2012 - 动态SQL查询