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 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. 

N的N次方,只需要求N的个位数的N的方的个位数,同时有一个规律,1 1 1 1 1 1 1 12 4 8 6 2 4 8 63 9 7 1 3 9 7 14 6 4 6 4 6 4 65 5 5 5 5 5 5 56 6 6 6 6 6 6 67 9 3 1 7 9 3 18 4 2 6 8 4 2 61 9 1 9 1 9 1 9所以应该很简单把  
 1 #include<iostream>
 2 #include<cstdio>
 3 using namespace std;
 4 int main()
 5 {
 6     int t;
 7     int n;
 8     int i,j;
 9     while(cin>>t)
10     {
11         while(t--)
12         {
13             cin>>n;
14             i=n%10;
15             j=n%4;
16             if(j==0)
17                 cout<<i*i*i*i%10<<endl;
18             else
19             {
20                 int ans=1;
21                 while(j--)
22                 {
23                     ans*=i;
24                     ans=ans%10;
25                 }
26                 cout<<ans<<endl;
27             }
28         }
29     }
30     return 0;
31 }
时间: 2024-10-15 18:57:02

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(找规律)

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

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 oj 1061 Rightmost Digit (快速幂算法)

这里首先要讲解一下快速幂算法: 快速幂取模算法 在网站上一直没有找到有关于快速幂算法的一个详细的描述和解释,这里,我给出快速幂算法的完整解释,用的是C语言,不同语言的读者只好换个位啦,毕竟读C的人较多~ 所谓的快速幂,实际上是快速幂取模的缩写,简单的说,就是快速的求一个幂式的模(余).在程序设计过程中,经常要去求一些大数对于某个数的余数,为了得到更快.计算范围更大的算法,产生了快速幂取模算法.[有读者反映在讲快速幂部分时有点含糊,所以在这里对本文进行了修改,作了更详细的补充,争取让更多的读者一目

杭电 HDU ACM 1061 Rightmost Digit

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