快速幂取模ADT

int pow_mod(int a, int n, int m){
    if(n==0) return 1;
    int x=pow_mod(a, n/2, m);
    long long ans = (long long)x*x%m;
    if(n%2==1)ans=ans*a%m;
    return (int)ans;
}

int quick(int a,int b,int c){
    int ans = 1;
    a = a%c;
    while(b>0){
        if(b%2==1)ans=(ans*a)%c;//状态迭代
        b= b/2;
        a = (a*a)%c;
    }
    return ans;
}

上面的是刘汝佳的写法,复杂度为$O(logn)$

posted @ 2018-04-06 18:14  摇啊摇啊  阅读(132)  评论(0编辑  收藏  举报