首页 > 技术文章 > 杭电1097-A hard puzzle

wft1990 2015-03-12 22:50 原文

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
 
 

题目分析:

1^1=1 1^2=1 1^3=1 1^4=1 1^5=1 ...从4次方开始周期为1
2^1=2 2^2=4 2^3=8 2^4=6 2^5=2 ...从4次方开始周期为4
3^1=3 3^2=9 3^3=7 3^4=1 3^5=3 ...从4次方开始周期为4
4^1=4 4^2=6 4^3=4 4^4=6 4^5=4 ...从4次方开始周期为2
...
9^1=9 9^2=1 9^3=9 9^4=1 9^5=9 ...从4次方开始周期为2

同时要理解题目下方 (a%10)和a相同的幂次方尾数的结果是相同的,

 

#include<stdio.h>
main()
{
    int a,b,c;
    while(~scanf("%d%d",&a,&b))
    {
        b%=4;a%=10;c=a;
        if(b==0)
        b=4;
        while(--b)
        c=c*a%10;
        printf("%d\n",c);
    }
}

推荐阅读