其他分享
首页 > 其他分享> > 快速幂及求余

快速幂及求余

作者:互联网

 1 #include<bits/stdc++.h>
 2 
 3 using namespace std;
 4 typedef long long ll ;
 5 ll fun(ll x,ll y)
 6 {
 7     ll ans=1;
 8     while(y)
 9     {
10         if(y&1)
11             ans*=x;
12         x*=x;
13         y>>=1;
14     }
15     return ans;
16 }
17 int main()
18 {
19     ll a,b;
20     while(cin>>a>>b)
21     {
22         cout<<fun(a,b);
23     }
24 }
 1 #include<bits/stdc++.h>
 2 
 3 using namespace std;
 4 typedef long long ll ;
 5 ll fun(ll x,ll y,ll k)
 6 {
 7     ll ans=1;
 8     while(y)
 9     {
10         if(y&1)
11             ans*=x,ans%=k;
12         x*=x,x%=k;
13         y>>=1;
14     }
15     return ans;
16 }
17 int main()
18 {
19     ll a,b,k;
20     while(cin>>a>>b>>k)
21     {
22         cout<<fun(a,b,k);
23     }
24 }

 

标签:cout,int,ll,long,while,幂及,ans,快速,求余
来源: https://www.cnblogs.com/ziyuwang/p/10460398.html