24 条题解

  • 9
    @ 2022-3-16 16:29:12

    可以分治,将 ana^n 变成 afracn2×afracn2a^{frac{n}{2}}\times a^{frac{n}{2}},但如果此时 nn 是奇数,则多乘一个 aa

    #include<bits/stdc++.h>
    #define ll long long
    using namespace std;
    ll a,b,p=1e9+7;
    ll qpow(ll a,ll b,ll p){
    	ll res=1,tmp=a;
    	while(b){
    		if(b&1)res=(res%p*tmp%p)%p;
    		tmp=(tmp*tmp)%p;
    		b>>=1;
    	}
    	return res;
    }
    int main(){
    	cin>>a>>b>>p;
    	cout<<qpow(a,b,p)<<endl;
    	return 0;
    }
    

    信息

    ID
    171
    时间
    1000ms
    内存
    256MiB
    难度
    2
    标签
    递交数
    879
    已通过
    297
    上传者