快速冪取模

快速冪取模模板,求m的n次方:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
const int MOD=1234567;

ll modexp(ll a, ll b)
{
    ll res=1;
    while(b>0)
    {
        if(b&1) res=res*a%MOD;
        b=b>>1;
        a=a*a%MOD;
    }
    return res;
}

int main() {
    //freopen("in.txt","r",stdin);
    int m,n;
    scanf("%d",&n);
    printf("%d\n",modexp(m,n));
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章