AcWing 3625. 幂次方
原题链接
简单
作者:
无味_9
,
2025-04-01 09:43:41
· 河南
,
所有人可见
,
阅读 2
快速幂模板
#include<iostream>
using namespace std;
typedef long long LL;
const int mod=233333;
LL fastPow(LL a, LL n, LL mod)
{
if(mod==1) return 0;
LL res=1;
a%=mod;
while(n>0)
{
if(n&1)
res=(res*a)%mod;
n>>=1;
a=(a*a)%mod;
}
return res;
}
int main()
{
LL x, n;
cin >> x >> n;
LL res=fastPow(x, n, mod);
cout << res;
return 0;
}
浮点数快速幂(负指数)
#include<iostream>
using namespace std;
double fastPow(double x, int n)
{
if(n==0) return 1.0;
if(n<0) return 1.0/fastPow(x, -n);
double res=1.0;
while(n>0)
{
if(n&1) res*=x;
}
return res;
}
int main()
{
double x;
int n;
cin >> x >> n;
double res=fastPow(x, n, mod);
cout << res;
return 0;
}