Wednesday, February 25, 2009

Bigmod using Dynamic Programming

//big mod using dinamic programming

long square(long c)
{

  return c*c;

}

long bigmod(long b,long p,long m)
{
  if (p==0)
      return 1;
  else if (p%2==0)
      return square(bigmod(b,p/2,m)%m)%m;
  else
      return ((b%m)*(bigmod(b,p-1,m)%m))%m;
}


1 comment:

nik.agarwal said...

Thanks dude...age old Headache got cured...:)