Saturday, January 21, 2012

Bigmod Recursive Impletation

#include<stdio.h>
#include<stdlib.h>


long squre(long x)
{
return x*x;
}
long big_mod(long b,long p,long m)
{
if(p==0) return 1;
else if(p%2==0) return squre(big_mod(b,p/2,m))%m;//squre(x)=x*x;
else return ((b%m)*big_mod(b,p-1,m))%m;
}
int main()
{
int b,p,m;
while(scanf("%d%d%d",&b,&p,&m)==3)
{
   printf("%d\n",big_mod(b,p,m));
}
return 0;
}
/*
Input:
3
18132
17


17
1765
3


2374859
3029382
36123
Output:
13
2
13195
*/

No comments:

Post a Comment