long long 模幂运算

typedef long long LL;

LL mul(LL a, LL b, LL mod) {
    LL ret = 0;
    while (b) {
        if (b & 1)
            (ret+=a)%=mod;
        b>>=1, (a<<=1)%=mod;
    }
    return ret;
}

LL mpow(LL x, LL y, LL mod) {
    LL ret = 1;
    while (y) {
        if (y & 1)
            ret = mul(ret, x, mod);
        y >>= 1, x = mul(x, x, mod);
    }
    return ret;
}

  

直接乘的话,会爆long long ,所以采取一边加,一边模的温和运算。

posted @ 2015-08-27 21:34  站在边缘的人  阅读(308)  评论(0编辑  收藏  举报