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 }
浙公网安备 33010602011771号