Cod sursa(job #146270)

Utilizator filipbFilip Cristian Buruiana filipb Data 1 martie 2008 15:02:15
Problema Ridicare la putere in timp logaritmic Scor Ascuns
Compilator cpp Status done
Runda Marime 0.46 kb
#include <stdio.h>
#include <assert.h>

#define MOD 1999999973
#define ll long long

ll N, P;

ll compute(ll p)
{
	if (p == 0)
		return 1;
	
	ll x = compute(p/2);
	x = (x * x) % MOD;
	if (p % 2)
		x = (x * P) % MOD;
		
	return x;		
}

int main(void)
{
	freopen("lgput.in", "r", stdin);
	freopen("lgput.out", "w", stdout);

	scanf("%lld %lld", &N, &P);
	assert(2 <= N && N <= (ll)1<<32);
	assert(2 <= P && P <= (ll)1<<32);

	printf("%lld\n", compute(P));
	
	return 0;
}