快速幂模板


 

Description

求a的b次方,取模mod(1<=a,b,mod<=1e18)

Input

多组输入,每组数据一行,3个正整数,分别为a,b,mod

Output

每组数据输出一行,为答案 

Sample Input

2 10 10000000
5 100 1
0 2 37

Sample Output

1024
0
0
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

/*long long qmod(ll a,ll b,int c)
{
    if(b==1) return a;
    if(b&1) return a*qmod(a,b-1,c)%c;
    else
    {
        ll m=qmod(a,b/2,c);
        return m*m%c;
    }
    
} */用二分还是超时
ll qmod(ll a,ll b,ll c)//快速幂
{
    int r=1;
    while(b)
    {
        if(b&1) r=a*r%c;
        a=a*a%c;
        b>>=1; b的二进制形式删除最后一位
    }
    return r;
}
int main()
{
    long long a,b,c;
    cin>>a>>b>>c;
    if(a>c) a=a%c;
    if(c==1) cout<<"0"<<endl;
    else
    {
ll ans
=qmod(a,b,c); cout<<ans<<endl; } }

参考资料: https://blog.csdn.net/qq_42265608/article/details/89475457

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM