question:
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<=10^18)
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
#include<stdio.h>
long long ksm(long long,long long,long long);
long long ksc(long long,long long,long long);
int main()
{
long long n,m;
while(~scanf("%lld%lld",&n,&m))
printf("%lld\n",ksm(n,m,10));
return 0;
}
long long ksm(long long a,long long b,long long c)/*快速幂函数*/
{
long long ans=1;
while(b)
{
if(b&1)
ans=ksc(ans,a,10)%c;
a=(a*a)%c;
b/=2;
}
return ans;
}
long long ksc(long long a,long long b,long long c)/*快速乘函数*/
{
long long ans=0;
while(b)
{
if(b&1)
ans=(ans+a)%c;
a=(a+a)%c;
b/=2;
}
return ans;
}
注意:本体a和b的取值太大,以至于及时两个数相乘都能爆。
模运算
- (a+b)%p=(a%p+b%p)%p
- (a−b)%p=(a%p−b%p+p)%p
- (a∗b)%p=(a%p)∗(b%p)%p