IT民工
加油!

看到一个快速幂取模的模版,挺好用的,为了避免中间过程超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;
}

 

 

 

posted on 2012-08-06 09:39  找回失去的  阅读(231)  评论(0编辑  收藏  举报