Cod sursa(job #1534401)

Utilizator Vbs96Vitelaru Sebastian Vbs96 Data 23 noiembrie 2015 18:28:36
Problema Ridicare la putere in timp logaritmic Scor 100
Compilator c Status done
Runda Arhiva educationala Marime 0.42 kb
#include <stdio.h>
#include <stdlib.h>
int const m = 1999999973;
long long n, p, y;
int main()
{
	FILE *f, *g;
	f = fopen("lgput.in", "r");
	fscanf(f,"%ld%ld", &n, &p);
	fclose(f);
	g = fopen("lgput.out", "w");
	y = 1;
	while (p > 1)
	{
		if (p % 2 == 0)
		{
			n = (n*n)%m;
			p /= 2;
		}
		else
		{
			y = (y*n)%m;
			n = (n*n)%m;
			p = (p - 1) / 2;
		}
	}
	fprintf(g, "%ld", (y*n) % m);
	return 0;
}