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

本题的大意是输入一个1到1000000000的整数N,要求输出N^N次方后的最后一个数字.....

解题思路:因为数据很大,只需要输出最后一位,所以对每个数据先求余得到最后一位数字,然后找出其最后一位的周期即可....


 1 /**************************************************
2 HDOJ 1061 已经AC
3 ***************************************************/
4 #include <iostream>
5 using namespace std;
6 int main()
7 {
8 int N,T;
9 int round=0;
10 int r[100]={0};
11 int i=0;
12 cin>>T;
13 while(T--)
14 {
15 cin>>N;
16 r[1]=N%10;//一定要对其求余
17 r[2]=(N%10)*(N%10)%10;//求余后再相乘,然后再求余,因为如果直接相乘时碰到1000000000时会发生错误
18 for(i=3;;i++)
19 {
20 r[i]=r[i-1]*(N%10)%10;
21 if(r[i]==r[1])
22 {round=i-1;break;}
23 }
24 if(N%round==0)
25 cout<<r[round]<<endl;
26 else cout<<r[N%round]<<endl;
27 }
28 //while(1);
29 return 0;
30 }

HDOJ 1061 Rightmost Digit,布布扣,bubuko.com

时间: 2025-01-04 09:10:32

HDOJ 1061 Rightmost Digit的相关文章

hdoj 1061 Rightmost Digit【快速幂求模】

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

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

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

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

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

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