题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1097
Problem Description
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
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
ll a,b;
int main()
{
while(scanf("%lld%lld",&a,&b) != EOF) {
ll ans = 1;
a %= 10;
ll base = a;
while(b) {
if(b & 1) {
ans = (ans*base)%10;
}
base = (base*base)%10;
b >>= 1;
}
printf("%lld\n",ans%10);
}
return 0;
}
本文介绍了一种高效的算法,用于解决大数幂运算中求解尾数的问题。通过优化的算法,即使在大数范围内,也能快速准确地找到a^b的最后一位数字,适用于竞赛编程和数学计算。
372

被折叠的 条评论
为什么被折叠?



