Links: http://acm.hdu.edu.cn/showproblem.php?pid=1097
A Hard Puzzle
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)Total submission (s): 34724 Accepted Submission (s): 12469
Problem Description
LCY gives a hard puzzle to feng5166,lwg,jgshining and ignatius:gave A and b,how to know the A^b.everybody objects to this BT problem,so LCY makes the problem easier than begin.
This puzzle describes That:gave A and b,how to know the A^b ' s, the last digit number. But everybody was too lazy to slove the problem,so they remit to you, who was wise.
Input
There is mutiple test cases. Each test cases consists of numbers a and B (0<a,b<=2^30)
Output
For each test case, you should output the a^b ' s last digit number.
Sample Input
7 668 800
Sample Output
96
Authoreddy
Recommendjgshining
To give you two numbers a and b, you need to find The last digit of a^b. Which 0<a,b<=2^30.
Idea-This is obviously a question of number theory. Two numbers are very large, go straight to the impossible. We can think of using two points for fast power, but the data is very large, will overflow, how to do? Then we can take the side of the mold Ah! This will neither overflow nor time out.
Complexity analysis--time complexity : O (log (b)), spatial complexity : O (1)
Attach the AC code:
#include <iostream> #include <cstdio> #include <string> #include <cmath> #include <iomanip > #include <ctime> #include <climits> #include <cstdlib> #include <cstring> #include < algorithm> #include <queue> #include <vector> #include <set> #include <map>using namespace std;typedef unsigned int ui;typedef long long ll;typedef unsigned long long ull;typedef long double ld;const double pi = a cos ( -1.0); const DOUBLE E = exp (1.0); const double EPS = 1e-8;const int mod = 10;int quick_pow (int a, int b); int main () {ios: : Sync_with_stdio (false); int A, B;while (~scanf ("%d%d", &a, &b)) {printf ("%d\n", Quick_pow (A, b));} return 0;} int Quick_pow (int a, int b) {int res = 1;while (b > 0) {if (b & 1) res = ((res%mod) * (a%mod))%mod;a = ((a%mod) * (A%mod)) %mod;b >>= 1;} return res;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 1097 A Hard Puzzle