杭电 1097 A hard puzzle

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 is too lazy to slove this problem,so they remit to you who is wise.
 
Input
There are mutiple test cases. Each test cases consists of two 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 66 8 800
 
Sample Output
9 6
 
Author
eddy
 
Recommend
JGShining
    问题描述:给两个32位的整型数啊a, b,计算出a^b的最后一位!
    问题解答:本题的解决方法:快速幂取余。所选用算法的时间复杂度:log(b);但是它的相关理论我现在还不是很理解,只是先拿来借用!
 1 #include <stdio.h>
 2 
 3 int pow_mod( int a, int n, int m )
 4 {
 5     int ans = 1;
 6     a = a % m;
 7     while( n > 0 )
 8     {
 9         if( n % 2 == 1 )
10             ans = ( ans * a ) % m;
11         n = n / 2;
12         a = ( a * a ) % m;
13     }
14     return ans;
15 }
16 int main()
17 {
18     int a, n;
19     while( scanf( "%d%d",&a, &n ) != EOF )
20         printf( "%d\n", pow_mod(a,n,10) );
21 
22     return 0;
23 }
View Code
原文地址:https://www.cnblogs.com/yizhanhaha/p/3132333.html