tctony

Focus on linux,emacs,c/c++,python,algorithm...

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  17 随笔 :: 0 文章 :: 7 评论 :: 0 Trackbacks

 

A hard puzzle

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


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
#include<iostream>
using namespace std;

int main() {
    
long a,b;
    
int t,x[6],i;
    
while(cin>>a>>b){
        x[
1]=a=a%10;
        i
=1;
        
do {
            
++i;
            x[i]
=(x[i-1]*a)%10;
        }
while(x[i]!=x[1]);
        t
=i-1;
        x[
0]=x[i-1];
        printf(
"%d\n",x[b%t]);
    }

    
return 0;
}


此处a,b要用long类型,开始用的整型,一直WA。。 - -!
posted on 2007-11-29 12:22 tctony 阅读(1186) 评论(3)  编辑 收藏 引用

评论

# re: HDU 1097 2010-04-06 10:45 蓝色书虫
前辈,能解释下程序的思想吗?没怎么看明白。。。。  回复  更多评论
  

# re: HDU 1097 2010-04-06 11:47 tctony
@蓝色书虫
考虑a^b最后一位的循环  回复  更多评论
  

# re: HDU 1097 2012-01-10 16:40 ㄗòsら顆糖
#include<iostream>
using namespace std;
int main()
{
//int a,b,i;
long a,b;
int s[10],i;
while(cin>>a>>b)
{
s[1]=a%10; //取数组s[1]为a的个位数
i=1;
do
{
i++; //i=2
s[i]=(s[i-1]*a)%10; //个位数*幂再取10的余数
}while(s[i]!=s[1]); //循环到与a的个位数为止,记下i的循环次数
s[0]=s[i-1]; //i-1为个位数的循环数
cout<<s[b%(i-1)]<<endl;
}
return 0;
}
参考你的,怎么错了????wa  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理