hdu 2035

tips

1. 数据规模一般时,(a^b)%m=(a%m a%m ...a%m)%m,数据规模很大如10^9时,用快速幂;

2. 注意WA代码写法的数据溢出;

3. 思考为何AC代码里last_rem*a%m是可行的;

4. 最后三位整数:a^b%1000

 

题目链接https://acm.hdu.edu.cn/showproblem.php?pid=2035

 

 1 //WA代码
 2 
 3 #include<iostream>
 4 #include<cmath>
 5 
 6 using namespace std;
 7 
 8 int main()
 9 {
10     int a,b;
11     
12     while(cin>>a>>b)
13     {
14         if(a==0&&b==0)
15             break;
16         int res=1;
17         while(b--)
18         {
19             res*=a%1000;
20         }
21         cout<<res%1000<<endl;
22     }
23     return 0;
24 }

 

 

 

 1 //AC代码
 2 
 3 #include<iostream>
 4 #include<cmath>
 5 
 6 using namespace std;
 7 
 8 int main()
 9 {
10     int a,b;
11     
12     while(cin>>a>>b)
13     {
14         if(a==0&&b==0)
15             break;
16         int res=1;
17         while(b--)
18         {
19             res*=a;
20             res%=1000;
21         }
22         cout<<res%1000<<endl;
23     }
24     return 0;
25 }

 

posted @ 2021-09-12 18:42  细妹  阅读(34)  评论(0)    收藏  举报