杭电1097-A hard puzzle(快速幂)

淡淡的烟草味﹌ 2022-08-08 17:37 365阅读 0赞

A hard puzzle

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 35177 Accepted Submission(s): 12627

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

  1. 7 66
  2. 8 800

Sample Output

  1. 9
  2. 6

这个题要用快速幂,否则会超时!

  1. #include<cstdio>
  2. int fun(int x,int y)
  3. {
  4. x%=10;
  5. int t=1;
  6. while(y>0)
  7. {
  8. if(y&1)
  9. t=t*x%10;
  10. x=x*x%10;
  11. y/=2;
  12. }
  13. return t;
  14. }
  15. int main()
  16. {
  17. int a,b;
  18. while(scanf("%d%d",&a,&b)!=EOF)
  19. {
  20. printf("%d\n",fun(a,b));
  21. }
  22. return 0;
  23. }

发表评论

表情:
评论列表 (有 0 条评论,365人围观)

还没有评论,来说两句吧...

相关阅读