Cod sursa(job #1477776)

Utilizator herbertoHerbert Mohanu herberto Data 26 august 2015 22:07:44
Problema Ridicare la putere in timp logaritmic Scor 10
Compilator c Status done
Runda Arhiva educationala Marime 0.4 kb
#include <stdio.h>
#include <stdlib.h>

int main(){
  FILE*fin=fopen("lgput.in", "r");
  FILE*fout=fopen("lgput.out", "w");
  long long x, n, p;
  fscanf(fin, "%lld%lld", &n, &p);
  x=n;
  while(p>1){
    if(p%2==0){
      x=x*x;
      p/=2;
      x%=1999999973;
    }
    else{
      x=x*(x*x);
      p=(p-1)/2;
      x%=1999999973;
    }
  }
  fprintf(fout, "%lld", x);
  return 0;
}