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

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.

 
 
思路:一道简单的快速幂,刚开始看错了题目,一直没有思路。
若把a^b%c看做b个a相乘并分布求余,b太大显然会超时,快速幂的原理是:降低 b 的次数,把a变大(涉及求余,对a不影响);

#include<cstdio>
int powermod(int a,int b,int c)
{
int ans=;
a=a%c;
if(a==)
return ;
while(b>)
{
if(b%==)
ans=ans*a%c;
b/=;
a=a*a%c;
}
return ans;
}
int main()
{
int T;
int n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
printf("%d\n",powermod(n,n,));
}
return ;
}

Rightmost Digit的更多相关文章

  1. HDU 1061 Rightmost Digit --- 快速幂取模

    HDU 1061 题目大意:给定数字n(1<=n<=1,000,000,000),求n^n%10的结果 解题思路:首先n可以很大,直接累积n^n再求模肯定是不可取的, 因为会超出数据范围, ...

  2. hdoj 1061 Rightmost Digit【快速幂求模】

    Rightmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  3. HDOJ 1061 Rightmost Digit(循环问题)

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

  4. Rightmost Digit(快速幂+数学知识OR位运算) 分类: 数学 2015-07-03 14:56 4人阅读 评论(0) 收藏

    C - Rightmost Digit Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...

  5. Rightmost Digit(快速幂)

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

  6. <hdu - 1600 - 1601> Leftmost Digit && Rightmost Digit 数学方法求取大位数单位数字

    1060 - Leftmost Digit 1601 - Rightmost Digit 1060题意很简单,求n的n次方的值的最高位数,我们首先设一个数为a,则可以建立一个等式为n^n = a * ...

  7. 杭电 1061 Rightmost Digit计算N^N次方的最后一位

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

  8. HDOJ 1061 Rightmost Digit

    找出数学规律 原题: Rightmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

  9. Rightmost Digit(最后一位数字)

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

随机推荐

  1. 用 Java 实现断点续传 (HTTP)

    断点续传的原理 其实断点续传的原理很简单,就是在 Http 的请求上和一般的下载有所不同而已. 打个比方,浏览器请求服务器上的一个文时,所发出的请求如下: 假设服务器域名为 wwww.sjtu.edu ...

  2. java笔记--异常详解与处理

    一.异常概念 Throwable类是Java中所有错误或异常的超类. 1.只有当对象是此类(或其子类)的实例时,才能通过Java虚拟机或着Java throw语句抛出.     2.只有此类或其子类才 ...

  3. [Effective JavaScript 笔记]第17条:间接调用eval函数优于直接调用

    eval函数不仅仅是一个函数.大多数函数只访问定义它们所在的作用域,而不能访问除此之外的作用域(词法作用域).eval函数具有访问调用它时的整个作用域的能力.编译器编写者首次设法优化js时,eval函 ...

  4. unity缓存和浏览器缓存

    原地址:http://www.cnblogs.com/hisiqi/p/3203553.html <蒸汽之城>游戏中,为什么会黑屏?或者无法正常进入游戏? 我们在进行多项测试中发现少数用户 ...

  5. 将服务器上的myql数据库导入本地数据库

    文章是从我的个人博客上粘贴过来的, 大家也可以访问 www.iwangzheng.com 首先登录到服务器上,进入文件夹,我们这边的是m-cms $mysqldump -uroot mos > ...

  6. ImageMagick资料

    ImageMagick资料 ---------------------------------------------------------------------------- ImageMagi ...

  7. 【GoLang】GoLang fmt 占位符详解

    golang 的fmt 包实现了格式化I/O函数,类似于C的 printf 和 scanf. # 定义示例类型和变量 type Human struct { Name string } var peo ...

  8. python提取百万数据到csv文件

    转自:http://www.2cto.com/kf/201311/258112.html 今天有需求,需要把系统所有用户注册的id和邮箱等信息导出来提供给他们,在mysql里面count了下,大概有3 ...

  9. Android的Observable和iOS的NotificationCenter

    使用起来很类似,参看以下网址http://stackoverflow.com/questions/10327200/equivalent-of-ios-nsnotificationcenter-in- ...

  10. Android 阅读Tasks and Back Stack文章后的重点摘抄

    这篇文章是做android的必读篇目,要仔细阅读,原文连接http://developer.android.com/guide/components/tasks-and-back-stack.html ...