HDU 1097 A hard puzzle(快速幂)

Description
给出两个整数a和b,求a^b的个位
Input
多组输入,每组用例占一行包括两个整数a和b,以文件尾结束输入
Output
对于每组用例,输出a^b的个位
Sample Input
7 66
8 800
Sample Output
9
6
Solution
问题转化为求a^b(mod 10),简单快速幂
Code

#include<cstdio>
#include<iostream>
using namespace std;
int mod_pow(int a,int b,int p)
{
    int ans=1;
    a%=p;
    while(b)
    {
        if(b&1)
            ans=(ans*a)%p;
        a=(a*a)%p;
        b>>=1;
    }
    return ans;
} 
int main()
{
    int a,b;
    while(~scanf("%d%d",&a,&b))
        printf("%d\n",mod_pow(a,b,10));
    return 0;
}

你可能感兴趣的:(HDU 1097 A hard puzzle(快速幂))