首页 > 其他 > 详细

杭电1097-A hard puzzle

时间:2015-03-12 23:47:08      阅读:302      评论:0      收藏:0      [点我收藏+]
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);
    }
}

杭电1097-A hard puzzle

原文:http://www.cnblogs.com/wft1990/p/4333993.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!