D - A hard puzzle
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
题意:求a的n次幂 最后一位,(对10取余)
#include<stdio.h>
#include<iostream>
using namespace std;
long long hanshu(long long a,long long n)
{
int b=10;
long long sum=1;
a%=b;
while(n>0)
{
if(n%2!=0)
sum=(sum*a)%b;
n/=2;
a=(a*a)%b;
}
return sum;
}
int main()
{
long long a,b;
while(scanf("%I64d%I64d",&a,&b)!=EOF)
{
printf("%I64d\n",hanshu(a,b));
}
return 0;
}