要求A^B的后三位整数,每次乘积后再对1000取余即可,其中第一种为快速幂。
- #include <iostream>
 - #include <cmath>
 - using namespace std;
 - long long mod_pow(long long x,long long n)
 - {
 - if(n==0) return 1;
 - long long res=mod_pow(x,n/2);
 - res=res*res%1000;
 - if(n%2==1) res=res*x%1000;
 - return res;
 - }
 - int main()
 - {
 - long long a,b;
 - long long res;
 - while(cin>>a>>b)
 - {
 - if(a==0 && b==0) break;
 - res=mod_pow(a,b);
 - cout<<res<<endl;
 - }
 - return 0;
 - }
 - //int main()
 - //{
 - // int a,b,i,ans;
 - // while(cin>>a>>b)
 - // {
 - // ans=1;
 - // if(a==0&&b==0) break;
 - // for(i=1;i<=b;i++)
 - // {
 - // ans=ans*a%1000;
 - // }
 - // cout<<ans<<endl;
 - // }
 - // return 0;
 - //}
 

京公网安备 11010502036488号