天天看點

ACM-數論之A hard puzzle——hdu1097A hard puzzle

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

該題目就是求a的b次方 個位數的數字為多少。
顯然不能暴力....找規律吧,于是就可以發現:

      一次方  二次方   三次方   四次方   五次方
   1   均為1
   2    2       4        8         6        2
   3    3       9        7         1        3
   4    4       6        4         6        4
   5   均為5
   6   均為6
   7    7       9        3         1        7
   8    8       4        2         6        8
   9    9       1        9         1        9
   0   均為0

可以發現 規律很明顯,是以嘛,敲代碼吧~
       
#include <iostream>
using namespace std;
int main()
{
    int a,b;
    int i,j,k,c;
    int arr[10][10];

    for(i=0;i<10;++i)
    {
        for(j=0;j<3;++j)
        {
            c=i;
            for(k=0;k<j;++k)
                c*=i;
            arr[i][j+1]=c%10;
        }
        arr[i][0]=(i*i*i*i)%10;
    }
    
    
    while(cin>>a>>b)
    {
        a%=10;
        
        if(a==1 || a==5 && a==6 &&a==0)
            cout<<a<<endl;
        else if(a==4 || a==9)
        {
            c=b%2;
            cout<<arr[a][c]<<endl;
        }
        else 
        {
            c=b%4;
            cout<<arr[a][c]<<endl;
        }
    }
    return 0;
}