Cod sursa(job #638254)

Utilizator antoanelaAntoanela Siminiuc antoanela Data 20 noiembrie 2011 19:52:27
Problema Ciuperci Scor 50
Compilator cpp Status done
Runda .com 2011 Marime 0.65 kb
#include <cstdio>
#include <map>
using namespace std;
#define mod 666013
#define ll long long

int q;
ll n;
map <ll, int> m;

inline ll solve (ll n)
{
	if (n==1) return 1;
	if (n==2) return 2;
	if (m[n]) return m[n];
	ll x, y=n>>1;
	if (n&1)
	{
		x=solve(y);
		x=(x*x)%mod;
		m[n]=x;
		return x;
	} else
	{
		x=solve(y);
		y=solve(y-1);
		x=(2*x*y)%mod;
		m[n]=x;
		return x;
	}
}

int main()
{
	freopen("ciuperci.in","r",stdin);
	freopen("ciuperci.out","w",stdout);
	scanf("%d\n", &q);
	char s[20];
	int i;
	while (q--)
	{
		n=0;
		fgets(s, 20, stdin);
		for (i=0; s[i]>='0' && s[i]<='9'; i++) n=n*10+s[i]-'0';
		printf("%lld\n", solve(n));
		m.clear();
	}
}