A的B次方对C取余

来源:互联网 发布:windows组件中没有ie 编辑:程序博客网 时间:2024/05/01 18:40
#include <stdlib.h>#include <stdio.h>/*a^b = (a^2)^(b/2)*a设 a=k1*c+m;b=k2*c+n;则 a*b % c = (k1*c+m)*(k2*c+n) % c = (k1*k2*c*c+m*k1*c+n*k2*c + m*n)%c */int modExp(int a,int b,int c){int t,y;t = 1; y = a;while (b!=0){//例如:5^7 % 3 = (5^2)^(7/2) * 5 %3if (b%2==1)t = t*y % c;y = y*y % c;b = b/2;}return t;}int main(){int a,b,c;scanf("%d%d%d",&a,&b,&c);printf("modExp:%d\n",modExp(a,b,c));return 0;}