题目及翻译
题面
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.
翻译
lcy给feng5166,lwg,JGShining和Ignatius出了个难题:给出一个a和b,如何得到a的b次方。大家都觉得这个问题太变态了,所以lcy把题目变简单了。
现在的问题被削弱成了:给出一个a和b,怎样知道a的b次方的个位数字。但是大家都懒得去做这个问题,所以把问题交给了你这个聪明蛋。
输入
There are mutiple test cases. Each test cases consists of two numbers a and b(0<a,b<=2^30)
翻译
输入有多组样例,每组样例有a和b两个数字。
(0<a,b<=2^30)
输出
For each test case, you should output the a^b’s last digit number.
翻译
对于每组样例,输出a的b次方的个位数字。
输入样例
7 66
8 800
输出样例
9
6
题目思路
快速幂取模10运算即可得到个位数。
注意事项
数据在int范围内,但是到2的30次方,暴力会超时。
快速幂运算可以很好的解决这个问题。
AC代码
C/C++(几乎没有代码变更)
用时0MS 内存1208K 长度533B
#include<stdio.h>
int qPow(int a,int b,int Mod) { //快速幂计算a ^ b % Mod
int Ans = 1;//众所周知1的0次方为1
a = a % Mod;//计算前也要取模
while(b > 0) { //指数>1时才计算
if(b & 1) Ans = Ans * a % Mod;//指数为单数时才有值需要附加
b >>= 1;//指数减半
a = a * a % Mod;//乘数在指数减半的时候应当翻倍,并取模(乘法取模不影响)
}
return Ans;
}
int main() {
int a,b;
while(~scanf("%d %d",&a,&b))//这里c++只需要while(cin>>a>>b)
printf("%d\n",qPow(a,b,10));//取模10获得个位数字
return 0;
}
Java
网吧没有环境不好写呢⊙﹏⊙
本文作者 CSDN@扶她小藜
个人主页链接 https://blog.csdn.net/weixin_44579869