HDU 1097 a hard puzzle

A hard puzzle

Time Limit: 2000/1000 MS(Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 24457    Accepted Submission(s): 8676


Problem Description

lcy gives a hardpuzzle to feng5166,lwg,JGShining and Ignatius: gave a and b,how to know thea^b.everybody objects to this BT problem,so lcy makes the problem easier thanbegin.
this puzzle describes that: gave a and b,how to know the a^b's the last digit number.Buteverybody is too lazy to slove this problem,so they remit to you who is wise.

 

 

Input

There are mutipletest cases. Each test cases consists of two numbers a and b(0<a,b<=2^30)

 

 

Output

For each testcase, you should output the a^b's last digit number.

 

 

Sample Input

7 66

8 800

 

 

Sample Output

9

6

 

很容易发现 a^b 的规律,个位数每四个一循环,有的虽然周期是2,但是4个一定是一个周期。

2: 2 4 8 16 32 64

3: 3 9 27 81 243 729

4: 4 16 64 256 1024

另外,我开始没有发现题目说a ,b 都 大于零。

因此,我的代码支持 0 ^ 0 =1(貌似在哪见过), 0 ^ 1=0 , 1 ^ 0=1;  

 

#include "string"
#include "iostream"

using namespace std;

int main(  )
{int m,n,s,i,tem;
  
  while(cin>>m>>n)
  {
  if( m==1||n==0 )
    s=1;
  else if( m%10==0 )
    s=0;
  else if( n==1 )
    s=m%10;
  else{ tem=1;
        m=m%10;//只保留个位数
	if( n%4==0 )
  {
	tem=m*m*m*m;
	}
else
    for( i=0;i<n%4;i++ )   
      tem*=m;
    s=tem%10;
  }
 
    cout<<s<<endl; 
  }

  return 0;
}



 

posted @   一杯半盏  阅读(118)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示