快速幂取模

看到一个快速幂取模的模版,挺好用的,为了避免中间过程超int,我全部用long long 

 

#include<stdio.h>

#include<string.h>

#include<stdlib.h>



typedef long long LL;

const LL c = 10007;

LL a, b;



LL exp_mod(LL a, LL b, LL c)

{

    LL r = 1;

    if(a > c) a %= c;

    while(b)

    {

        if(b & 1) r = (r * a) % c;

        a = (a * a) % c;

        b >>= 1;

    }

    return r;

}



int main()

{

    while(scanf("%lld%lld", &a, &b) == 2)

    {

        printf("%lld\n", exp_mod(a, b, c));

    }

    return 0;

}

 

 

 

你可能感兴趣的:(快速)