Rightmost Digit

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

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.

 
Author
Ignatius.L
 
 
 #include <stdio.h>
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,i,m;
int t,sum=;
scanf("%d",&n);
t=n%;
m=n%;
if(m==)
sum=t*t*t*t%;
else
{
for(i=;i<m;i++)
{
sum *= t;
}
}
printf("%d\n",sum%);
}
return ;
}
//找规律题,找到循环周期
 
/*
对于数字0~9, 它们的N次方,我们只看最后一个数字:
0^1=0; 0^2=0; 循环周期T=1;
1^1=1; 1^2=1;
循环周期T=1;
2^1=2; 2^2=4; 2^3=8; 2^4=6 2^5=2; 循环周期T=4;
3^1=3; 3^2=9; 3^3=7;
3^4=1;3^5=3; 循环周期T=4;
4^1=4; 4^2=6; 4^3=4;
循环周期T=2;

后面的大家可以自己算,会得出一个规律:
他们的循环周期只有1,2,4这三种,所以对于源代码里的b,我们可以只算b%4次就可以了,大大减少了循环的次数

*/

hdu_1061_Rightmost Digit_201311071851的更多相关文章

随机推荐

  1. 48.EXt.Data.JsonReader()

    转自:https://blog.csdn.net/huoyanxueren/article/details/2662915?utm_source=blogxgwz6 extJs 2.1学习笔记(Ext ...

  2. 47. Ext.form.Field.prototype.msgTarget

    转自:https://blog.csdn.net/a1542aa/article/details/24295791 ExtJS.form中msgTarget Ext表单提示方式:msgTarget:有 ...

  3. hibernate基础学习---hierbnate2级缓存

    1:开启二级缓存sessionFactory需要安装jar包 2:在实体类配置文件添加(配置二级缓存).我的配置文件是Account.hbm.xml <?xml version="1. ...

  4. P2251 质量检测(ST表)

    P2251 质量检测 题目描述 为了检测生产流水线上总共N件产品的质量,我们首先给每一件产品打一个分数A表示其品质,然后统计前M件产品中质量最差的产品的分值Q[m] = min{A1, A2, ... ...

  5. sql 循环分割字符

    DECLARE @Items VARCHAR(1000)='148' --待处理拼接字符串 --开始处理SET @Items=@Items+',' --必须追加“,”否则最后一个无法输出DECLARE ...

  6. Java系列学习(零)-写在前面的话

    1.为什么写这套笔记 理由一:因为需求,所以学习,然后就要记笔记 理由二:同时学几种相似的语言,怕搞混,所以写 2.笔记修改日志

  7. hibernate annotation 之 一对多、多对一双向外键关联

    假设,一个农场产出多种植物,具体的某一植物产于某一农场. 3 import java.io.Serializable; 4 import java.util.Set; 5 import javax.p ...

  8. 【Spring】AOP

    AOP 编程允许你把遍布应用各处的功能分离出来形成可重用的组件,将安全.事务和日志关注点与你的核心业务逻辑相分离. 面向切面编程往往被定义为促使应用程序分离关注点的一项技术.系统由许多不同组件组成,每 ...

  9. 第一次android混淆实战

    第一次混淆,主要是因为引用本地第三方jar包的问题.虽然说本地第三方jar包自动避免混淆,但一些本地第三方jar包下的一些包要避免混淆.比如: 文中的com.org 这些包名都要避免混淆. 下面是我用 ...

  10. RTL Compiler之Technology Library

    1 Target Library Design Compiler uses the target library to build a circuit. During mapping, Design ...