题目:
A^B Problem
时间限制: 1000 ms | 内存限制: 65535 KB
难度: 2
-
-
描述
-
Give you two numbers a and b,how to know the a^b's the last digit number.It looks so easy,but everybody is too lazy to slove this problem,so they remit to you who is wise.
-
- 输入
- There are mutiple test cases. Each test cases consists of two numbers a and b(0<=a,b<2^30)
- 输出
- For each test case, you should output the a^b's last digit number.
- 样例输入
-
7 66 8 800 - 样例输出
-
9 6 - 提示
- There is no such case in which a = 0 && b = 0。
-
-
\
\
代码:
#include <stdio.h>
#include<stdio.h>
int pow_mod(int a,int b,int c)
{
int s=1;
a=a%c;
while(b!=0)
{
if(b&1)
s=s*a%c;
a=a*a%c;
b>>=1;
}
return s;
}
int main()
{
int a,b,i,s;
while(~scanf("%d%d",&a,&b))
{
printf("%d\n",pow_mod(a,b,10));
}
return 0;
}
\
快速幂按位取模,存代码
同余定理:所谓的同余,顾名思义,就是许多的数被一个数d去除,有相同的余数。d数学上的称谓为模。如a=6,b=1,d=5,则我们说a和b是模d同余的。因为他们都有相同的余数1。
(a*b)mod c= ((a mod c)*(b mod c)) mod c\