A hard puzzle
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 16669 Accepted Submission(s): 5936
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.
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
AC code:
#include <iostream>
using namespace std;
int main()
{
int a,b;
while(scanf("%d%d",&a,&b)!=EOF)
{
a%=10;
if(a==0||a==5||a==6||a==1) printf("%d\n",a);
else if(a==4||a==9)
{
printf("%d\n",b%2==0?a*a%10:a);
}
else
{
if(b%4==1) printf("%d\n",a);
else if(b%4==2) printf("%d\n",a*a%10);
else if(b%4==3) printf("%d\n",a*a*a%10);
else printf("%d\n",a*a*a*a%10);
}
}
return 0;
}
本文介绍了一个经典的算法问题——快速计算a的b次方结果的最后一位数字。通过分析不同基数的幂次方尾数规律,给出了一种高效算法实现方案。此算法特别针对大整数情况进行了优化。
1152

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



