【1097】A hard puzzle
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
//通过找规律可以发现,最大的循环情况是4,
//所以为了简便思考,就都当做4个一循环来做
//0~9的次方末位:{1,0,0},{1,1},{1,2,4,8,6,2},
//{1,3,9,7,1},{1,4,6,4},{1,5,5},{1,6,6},
//{1,7,9,3,1},{1,8,4,2,6,8},{1,9,1};
//0~9几个数开始循环:{1,1,4,4,2,1,1,4,4,2};
//0~9从哪个次方开始会存在循环:{1,0,1,0,1,1,1,0,1,0};
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b,c[4];
while(scanf("%d %d",&a,&b)!=EOF)
{
a=a%10;
c[0]=a;//一次方的末尾数
c[1]=(c[0]*a)%10;//二次方的末尾数
c[2]=(c[1]*a)%10;//三次方的末尾数
c[3]=(c[2]*a)%10;//四次方的末尾数
if(b%4==1)
printf("%d\n",c[0]);
if(b%4==2)
printf("%d\n",c[1]);
if(b%4==3)
printf("%d\n",c[2]);
if(b%4==0)
printf("%d\n",c[3]);
}
return 0;
}
下次补充快速幂的解法