Cod sursa(job #1477780)

Utilizator herbertoHerbert Mohanu herberto Data 26 august 2015 23:10:31
Problema Ridicare la putere in timp logaritmic Scor 100
Compilator c Status done
Runda Arhiva educationala Marime 0.46 kb
#include <stdio.h>
#include <stdlib.h>
#define m 1999999973
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=1;
  while(p>0){
    if(p%2==0){
      n=(n*n)%m;
      p/=2;
    }
    else{
      x=(x*n)%m;
      p--;
    }
    printf("%lld %lld %lld\n", x, n, p);
  }
  printf("%lld %lld %lld\n", x, n, p);
  fprintf(fout, "%lld", x);
  return 0;
}