A^B mod C代码实现!

终于过了。。。不容易呀。。。。

用的就是前面总结的知识,终于把它转化为代码了。。。。

 

#include <stdio.h> #define LL unsigned long long int //a*b mod c inline LL MulAndMod(LL a, LL b,LL c) { LL val,pre,shl_b; pre = a%c; shl_b = b; val = 0; while (shl_b) { if (shl_b&0x1) val = (val + pre)%c; shl_b>>=1; pre = (pre<<1)%c; } return val; } inline LL A_BModC(LL a,LL b,LL c) { LL val,pre,shl_b; shl_b = b; if (shl_b&0x1) val = 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; }

你可能感兴趣的:(c)