板子:
不取余
#include <iostream>
#define LL long long
using namespace std;
LL quickPow(LL a,LL b)
{
LL res = 1;
while(b)
{
if(b&1) res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
int main()
{
std::ios::sync_with_stdio(0);
cin.tie(0);
LL a,b;
cin>>a>>b;
cout<<a<<"^"<<b<<"="<<quickPow(a,b)<<endl;
return 0;
}
取余
#include <iostream>
#define LL long long
using namespace std;
LL quickPow(LL a,LL b,LL mod)
{
LL res = 1;
while(b)
{
if(b&1) res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res%mod;//这里%是要注意这种情况a = 1 b = 0 mod = 1
}
int main()
{
std::ios::sync_with_stdio(0);
cin.tie(0);
LL a,b,mod;
cin>>a>>b>>mod;
cout<<a<<"^"<<b<<" mod "<<mod<<"="<<quickPow(a,b,mod)<<endl;
return 0;
}