找出数学规律

原题:

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. Python day17 模块介绍1(time,random)

    module模块和包的介绍(略掉了) 常用模块 # time模块 import time print(time.time())#时间戳,在1970年开始到现在一共多少秒 print(time.gmti ...

  2. MongoDB(课时10 数组)

    3.4.2.5 数组查询 MongoDB里面支持数组保存,一旦支持数组保存,就需要对于数组的数据进行匹配. 范例:插入一部分数组内容 课程是数组的形式(截图时少截一条信息) 此时数据包含数组内容,而后 ...

  3. 02_DllZZ.def

    ZC: 在VC6里面,只要有这个文件就可以了.但是到了 VS2010,需要手动的指定使用这个文件才行:VS2010-->项目-->属性--> 来到窗口"??? 属性页&qu ...

  4. [设计模式]适配器模式Adapter

    将一个类的接口转换成客户希望的另外一个接口. A d a p t e r模式使得原本 由于接口不兼容而不能一起工作的那些类可以一起工作.

  5. (转)不要自称是程序员,我十多年的 IT 职场总结

    其他: 我是一名程序员,工作很努力,为什么绩效还总是垫底? 外企,中年失业何去何从? 来公司半年了,也悟出了一些道理. 如果我可以给每个工程教育增加一门课,它不会涉及编译器.门电路或是时间复杂度,而是 ...

  6. 开发shellcode的艺术

    专业术语 ShellCode:实际是一段代码(也可以是填充数据) exploit:攻击通过ShellCode等方法攻击漏洞 栈帧移位与jmp esp 一般情况下,ESP寄存器中的地址总是指向系统栈且不 ...

  7. git rm删除

    在Git中,删除也是一个修改操作,我们实战一下,先添加一个新文件test.txt到Git并且提交: $ git add test.txt $ git commit -m "add test. ...

  8. CentOS7.6 Install TensorFlow

    1. install pip 1).  yum -y install epel-release 2).  yum install python-pip 3).  pip install --upgra ...

  9. loj#101. 最大流 dinic+当前弧

    板子题 当前弧优化版本 目前效率最高 //#pragma comment(linker, "/stack:200000000") //#pragma GCC optimize(&q ...

  10. dp练习(5)——最长严格上升子序列

    1576 最长严格上升子序列  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 给一个数组a1, a2 ... ...