找出数学规律

原题:

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. 怎么彻底删除2345的各种顽固Process

    清晨打开电脑,都是2345的不良新闻,心情不美美哒 2345如何卸载? “C:\Windows\System32\drivers”目录删除Mslmedia.sys 开始-运行-cmd输入“sc del ...

  2. putty xming gnome-session

    在windows里远程连接linux的最好方法. 比VNC方式好多了 1) xming启动一个窗口 2) putty 设置完X11 forwarding之后,远程登录 3) 在putty 里启动 gn ...

  3. LeetCode--204--计数质数

    问题描述: 统计所有小于非负整数 n 的质数的数量. 示例: 输入: 10 输出: 4 解释: 小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 . 方法1:经典的判断是否为质数遍历( ...

  4. LeetCode--100--相同的树

    问题描述: 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1 / \ / \ 2 3 2 3 [1,2, ...

  5. 转 解决linux下tomcat的shutdown命令杀不死进程

    tomcat在windows下可以直接关闭,但是貌似在Linux下有时候shutdown.sh 没有关闭tomcat进程; 现象:在Linux下shutdown.sh ,然后查看tomcat进程发现没 ...

  6. gleez开发环境搭建

    一.虚拟主机目录配置 1.配置apache服务器 Apache是常用的web服务器,即常见的用来处理http协议,处理网页的. Apache的配置文件都存放在/etc/apache2/目录,这里有很多 ...

  7. zzuli 1726 迷宫 BFS(题意)

    1726: 迷宫 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 502  Solved: 80 SubmitStatusWeb Board Descri ...

  8. hdu2510 爆搜+打表

    符号三角形 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  9. iOS系统版本与机型的对应关系

    1.手机系统版本:10.3 NSString* phoneVersion = [[UIDevice currentDevice] systemVersion]; 2.手机类型:iPhone 6 NSS ...

  10. linux pipes

    In Linux, a pipe is implemented using two filedata structures which both point at the same temporary ...