Cod sursa(job #66086)

Utilizator peanutzAndrei Homorodean peanutz Data 15 iunie 2007 11:13:09
Problema Patrate2 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.4 kb
#include <stdio.h>
#include <string.h>
#define NMAX 10000

int fact[2][NMAX];
int doi[NMAX];
int n;

void mulf(int b, int crt, int last)
{
	int i, t;
	for(i = 1, t = 0; i <= fact[last][0] || t; ++i, t /= 10)
		fact[crt][i] = (t += fact[last][i]*b) % 10;
	fact[crt][0] = i-1;
}

void do_fact()
{
	int i, k = 0;
	fact[k][0] = fact[k][1] = 1;

	for(i = 2; i <= n; ++i, k = 1-k)
	{
		mulf(i, 1-k, k);
	}
}

void mulp(int b)
{
	int i, t;
	for(i = 1, t = 0; i <= doi[0] || t; ++i, t /= 10)
		doi[i] = (t += doi[i]*b) % 10;
	doi[0] = i-1;
}

void pow()
{
	int i, until = n*n;
	doi[0] = doi[1] = 1;

	while(until - 16 >= 0)
	{
		mulp(16);
		until -= 4;
	}
	while(until)
	{
		mulp(2);
		--until;
	}
}

void mul()
{
	int i, j, t;
	int c[NMAX];
	int crt = !(n%2);
	memset(c, 0, sizeof(c));

	for(i = 1; i <= doi[0]; ++i)
	{
		for(t = 0, j = 1; j <= fact[crt][0] || t; ++j, t /= 10)
			c[i+j-1] = (t += c[i+j-1] + doi[i]*fact[crt][j]) % 10;
		if(c[0] < i+j-1)
			c[0] = i+j-1;
	}
	for(; c[0] > 0 && !c[c[0]]; --c[0]);

        for(i = c[0]; i; --i)
		printf("%d", c[i]);
	printf("\n");
}

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

	scanf("%d", &n);

	do_fact();

	pow();

	mul();

	fclose(stdin);
	fclose(stdout);

	return 0;
}