HDU 1061 Rightmost Digit(找规律)


Rightmost Digit

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 43732    Accepted Submission(s): 16434

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

 1 //4为周期
 2 #include <iostream>
 3 #include <cstdio>
 4 using namespace std;
 5 int main()
 6 {
 7     long long n;
 8     cin>>n;
 9     long long t;
10     while(n--){
11         cin>>t;
12         int k=t%4;
13         t%=10;
14         if(k==2) t=t*t;
15         if(k==3) t=t*t*t;
16         if(k==0) t=t*t*t*t;
17         t%=10;
18         cout<<t<<endl;
19     }
20     return 0;
21 }
时间: 2024-10-10 10:59:08

HDU 1061 Rightmost Digit(找规律)的相关文章

HDU 1061 Rightmost Digit题解

求一个大数N^N的值的最右边的数字,即最低位数字. 简单二分法求解就可以了. 不过注意会溢出,只要把N % 10之后,就不会溢出了,不用使用long long. #include <stdio.h> int rightMost(int n, int N) { if (n == 0) return 1; int t = rightMost(n / 2, N); t = t * t % 10;; if (n % 2) t *= N; return t % 10; } int main() { in

HDU 1061.Rightmost Digit【数论及方法】【8月30】

Rightmost Digit 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 ca

HDU 1061 Rightmost Digit

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 ca

HDU - 1061 - Rightmost Digit (快速幂取模!)

Rightmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 34329    Accepted Submission(s): 13091 Problem Description Given a positive integer N, you should output the most right digit of

HDU 1061 [Rightmost Digit] 数学方法

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1061 题目大意:求N^N的个位数 关键思想:对1个N来说,乘方时末尾会循环.对所有N来说,结果以20为周期. 代码如下(第一个思想): //cnt为循环节长度 #include <iostream> #include <cmath> using namespace std; int main(){ int T; long long i,N,cnt; cin>>T; whil

HDU 1061 Rightmost Digit解决问题的方法

求大量N^N的值最右边的数字,即最低位. 它将能够解决一个简单二分法. 只是要注意溢出,只要把N % 10之后.我不会溢出,代替使用的long long. #include <stdio.h> int rightMost(int n, int N) { if (n == 0) return 1; int t = rightMost(n / 2, N); t = t * t % 10;; if (n % 2) t *= N; return t % 10; } int main() { int T

HDOJ 1061 Rightmost Digit

Rightmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 30543    Accepted Submission(s): 11624 Problem Description Given a positive integer N, you should output the most right digit of N

hdu 4952 Number Transformation (找规律)

题目链接 题意:给你个x,k次操作,对于第i次操作是:要找个nx,使得nx是>=x的最小值,且能整除i,求k次操作后的数 分析: 经过打表找规律,会发现最后的x/i,这个倍数会趋于一个固定的值,求出这个固定的值和K相乘就可以了, 为什么会趋于固定的值呢,因为最后虽然i在不断增长,但是x也是在增长的,每次的倍数会回退一个发现 有余数,然后再加上一个,所以趋于稳定. 官方题解: 1 #include <iostream> 2 #include <cstdio> 3 #includ

hdu 4861 Couple doubi (找规律 )

题目链接 可以瞎搞一下,找找规律 题意:两个人进行游戏,桌上有k个球,第i个球的值为1i+2i+?+(p−1)i%p,两个人轮流取,如果DouBiNan的值大的话就输出YES,否则输出NO. 分析:解题报告 1 #include <cstdio> 2 #include <iostream> 3 4 using namespace std; 5 int main() 6 { 7 int k, p; 8 while(cin>>k>>p) 9 { 10 if(k/