Cod sursa(job #156117)

Utilizator rethosPaicu Alexandru rethos Data 12 martie 2008 12:53:12
Problema Ridicare la putere in timp logaritmic Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.32 kb
#include <fstream.h>
#define mod 1999999973
long lg(long x,long p)
{ if (p==0) return 1;
  if (p%2) return (x*lg(x*x,(p-1)/2))%mod;
  return (lg(x*x,p/2)%mod);
}
int main()
{ unsigned long x,p;
  ifstream f("lgput.in");
  ofstream g("lgput.out");
  f>>x>>p;
  f.close();
  g<<lg(x,p);
  g.close();
  return 0;
}