求大量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…
HDU 1061 题目大意:给定数字n(1<=n<=1,000,000,000),求n^n%10的结果 解题思路:首先n可以很大,直接累积n^n再求模肯定是不可取的, 因为会超出数据范围,即使是long long也无法存储. 因此需要利用 (a*b)%c = (a%c)*(b%c)%c,一直乘下去,即 (a^n)%c = ((a%c)^n)%c; 即每次都对结果取模一次 此外,此题直接使用朴素的O(n)算法会超时,因此需要优化时间复杂度: 一是利用分治法的思想,先算出t = a^(n/2),若…
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…
Rightmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 57430    Accepted Submission(s): 21736 Problem Description Given a positive integer N, you should output the most right digit of N…
解决本题使用数学中的快速幂取余: 该方法总结挺好的:具体参考http://www.cnblogs.com/PegasusWang/archive/2013/03/13/2958150.html #include<iostream> #include<cmath> using namespace std; int PowerMod(__int64 a,__int64 b,int c)//快速幂取余 { ; a=a%c; ) { ==)//如果为奇数时,要多求一步,可以提前放到ans中…
题意:给定一个数,求n^n的个位数. 析:很简单么,不就是快速幂么,取余10,所以不用说了,如果不会快速幂,这个题肯定是周期的, 找一下就OK了. 代码如下: #include <iostream> #include <cstdio> #include <algorithm> #include <queue> #include <vector> #include <cstring> #include <map> using…
链接:传送门 题意:求 N^N 的个位 思路:快速幂水题 /************************************************************************* > File Name: hdu1061.cpp > Author: WArobot > Blog: http://www.cnblogs.com/WArobot/ > Created Time: 2017年05月17日 星期三 22时50分05秒 **************…
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…
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…
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…