找出数学规律

原题:

Rightmost Digit

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

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.

讲解:(摘自网上)

  对于数字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这三种,所以对于源代码里的a,我们可以只算a%4次就可以了,大大减少了循环的次数

源代码:

 #include <iostream>
using namespace std; int zhou[]; int main() {
int N; cin >> N;
while (N--) {
long long int num;
cin >> num;
int a = num % ;
int b = (a * a) % ;
int c = (b * a) % ;
int d = (c * a) % ;
int m = num % ;
switch(m) {
case : cout << d << endl; break;
case : cout << a << endl; break;
case : cout << b << endl; break;
case : cout << c << endl; break;
}
}
return ;
}

HDOJ 1061 Rightmost Digit的更多相关文章

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

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

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

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

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

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

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

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

  5. 题解报告:hdu 1061 Rightmost Digit(快速幂取模)

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

  6. 快速幂 HDU 1061 Rightmost Digit *

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

  7. 【HDOJ】1061 Rightmost Digit

    这道题目可以手工打表,也可以机器打表,千万不能暴力解,会TLE. #include <stdio.h> #define MAXNUM 1000000001 ][]; int main() ...

  8. HDU 1061 Rightmost Digit解决问题的方法

    求大量N^N的值最右边的数字,即最低位. 它将能够解决一个简单二分法. 只是要注意溢出,只要把N % 10之后.我不会溢出,代替使用的long long. #include <stdio.h&g ...

  9. hdu 1061 Rightmost Digit

    解决本题使用数学中的快速幂取余: 该方法总结挺好的:具体参考http://www.cnblogs.com/PegasusWang/archive/2013/03/13/2958150.html #in ...

随机推荐

  1. ActivityGroup实现tab功能

    android.app包中含有一个ActivityGroup类,该类是Activity的容器,可以包含多个嵌套进来的 Activitys,这篇文章就是借助ActivityGroup可以嵌套Activi ...

  2. 3.2 x86体系结构

    计算机组成 3 指令系统体系结构 3.2 x86体系结构 X86是商业上最为成功,影响力最大的一种体系结构.但从技术的角度看,它又存在着很多的问题,那我们就来一起分析X86这种体系结构的特点. 要探讨 ...

  3. 广播 (Broadcast)

    广播 :在Android中,Broadcast是一种广泛运用的在应用程序之间传输信息的机制.我们拿广播电台来做个比方.我们平常使用收音机收音是这样的:许许多多不同的广播电台通过特定的频率来发送他们的内 ...

  4. Garlands CodeForces - 707E (离线树状数组)

    大意: 给定n*m矩阵, k条链, 链上每个点有权值, 每次操作可以关闭或打开一条链或询问一个子矩阵内未关闭的权值和. 关键询问操作比较少, 可以枚举每条链, 暴力算出该条链对每个询问的贡献. 最后再 ...

  5. Knights of a Polygonal Table CodeForces - 994B (贪心)

    大意:n个骑士, 每个骑士有战力p, 钱c, 每个骑士可以抢战力比他低的钱, 每个骑士最多抢k次, 对每个骑士求出最大钱数 按战力排序后, 堆维护动态前k大即可 #include <iostre ...

  6. python-day38--IO模型

    一. IO模型介绍 对于一个网络通信,IO涉及到两个阶段 1.操作系统等数据来 2.进程或线程等操作系统拷贝数据 记住这两点很重要,因为这些IO模型的区别就是在两个阶段上各有不同的情况. 二.阻塞IO ...

  7. php date时间的获取

    1.得到当前时间:date('Y-m-d H:i:s',time()); 2.得到这个月有多少天:echo $getDay = date('t',date('Y-m-d', time())); 3.得 ...

  8. 转化为json方式函数

    1,我的数据格式是: {"message":"","code":0,"data":[{"Order" ...

  9. Ubuntu解压缩zip,tar,tar.gz,tar.bz2文件命令

    tar解包:tar xvf FileName.tar打包:tar cvf FileName.tar DirName .gz解压1:gunzip FileName.gz解压2:gzip -d FileN ...

  10. IOS7 UI设计的十大准则

    陈子木 iOS7 的用户界面设计比以往更卓越,并为用户提供了更具吸引力的独特体验,带来更大的机遇.在正式写代码之前,认真考虑UI设计是否符合这十条设计准则,可以提高App的可用性与吸引力.如果要更深入 ...