更高效率的a^b mod c

#include <stdio.h> #define LL unsigned long long int inline LL mod(LL a,LL b) { while (a>=b) a-=b; return a; } //a*b mod c inline LL MulAndMod(LL a, LL shl_b,LL c) { LL val,pre; pre = mod(a,c); val = 0; while (shl_b) { if (shl_b&0x1) val = mod(val + pre,c); shl_b>>=1; pre = mod(pre<<1,c); } return val; } inline LL A_BModC(LL a,LL shl_b,LL c) { LL val,pre; if (shl_b&0x1) val = mod(a,c); else val = 1; shl_b >>= 1; pre = MulAndMod(a,a,c); while (shl_b) { if (shl_b&0x1) val = MulAndMod(val,pre,c); shl_b>>=1; pre = MulAndMod(pre,pre,c); } return val; } int main() { LL a,b,c; while (scanf("%llu%llu%llu", &a,&b,&c)!=EOF) printf("%llu/n", A_BModC(a,b,c)); return 0; }

你可能感兴趣的:(更高效率的a^b mod c)