A hard puzzle

来源:互联网 发布:贵州大数据培训 编辑:程序博客网 时间:2024/06/14 16:06
               A hard puzzle
Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 5   Accepted Submission(s) : 2

Font: Times New Roman | Verdana | Georgia

Font Size:

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 668 800

Sample Output

96

Author

eddy




思路:
题意为求一个数a的n次方的最后一位数是什么。
草稿纸上随便写几个数就会发现 a^n的个位数是有规律的,并且和a的个位有关,比如3^n个位可能是3,9,7,1;8^n的个位可能是8,4,2,6;那么与n 什么关系呢 ?如果n的最后两位数对4求余,余几就是第几个,比如63^13215165,相当于求3^65的个位数65%4=1,那么63^13215165的个位数就是3。其他同理。
下面给出代码:
#include<iostream>#include<cstring>using namespace std;int main(){long long a,b;int c[4];while(cin>>a){cin>>b;int m=b%4;//求n对4的余数c[1]=a%10;for(int i=2;i<4;i++)//求a的个位数的四种可能情况,c[0],c[1],c[2],c[3]c[i]=(c[i-1]*c[1])%10;c[0]=(c[3]*c[1])%10;cout<<c[m]<<endl;}}

原创粉丝点击