ACM--快速幂--HDOJ 1061--Rightmost Digit

HDOJ地址:http://acm.hdu.edu.cn/showproblem.php?pid=1061

快速幂算法讲解:http://blog.csdn.net/qq_26891045/article/details/51334101

Rightmost Digit

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

Total Submission(s):45746  
  Accepted Submission(s):
17221

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.

================================天生骄傲的分割线=============================

题目的意思已经非常明确了,就是求n^n的个位数,求它的个位数就相当于进行mod 10运算,这里就直接用快速幂算法,就可以得出结果,题目给定的N可能会比较大,所以用long long比较保险。

#include<iostream>
#include<stdio.h>
using namespace std;
/**
  快速幂算法
*/
long long Calculation(long long a,long long b,long long c){
   int ans=1;
   a=a%c;
   while(b>0){
      if(b%2==1)
        ans=(ans*a)%c;
      b=b/2;
      a=(a*a)%c;
   }
   return ans;
}
int main(){
   int n,N;
   cin>>n;
   while(n--){
     cin>>N;
     printf("%d\n",Calculation(N,N,10));
   }
   return 0;
}

参考博客:http://blog.csdn.net/whjkm/article/details/42803805

时间: 2024-10-07 05:34:26

ACM--快速幂--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): 30543    Accepted Submission(s): 11624 Problem Description Given a positive integer N, you should output the most right digit of N

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 (快速幂取模!)

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 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 ACM 1061 Rightmost Digit

1.每次计算只要取最后一位即可. 2.使用快速幂提高效率. #include<iostream> using namespace std; int pow(int a,int n) { if(n==1) return a; else if(n%2==1) return (a*pow(a,n-1))%10; else { int tmp; tmp=pow(a,n>>1); return (tmp*tmp)%10; } } int main() { int T,n; cin>&g

矩阵快速幂 HDOJ 5318 The Goddess Of The Moon

题目传送门 1 /* 2 DP::dp[i][k] 表示选择i个字符串,最后一次是k类型的字符串,它由sum (dp[i-1][j]) (a[j], a[k] is ok)累加而来 3 矩阵快速幂:将n个字符串看成n*n的矩阵,如果匹配,矩阵对应位置为1.矩阵缩短递推dp时间,然后乘m-1次(dp[m][i])累加即可 4 注意去重 5 详细解释:http://blog.csdn.net/keshuai19940722/article/details/47111215 6 */ 7 #inclu

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