Cod sursa(job #75507)

Utilizator MarcvsHdrMihai Leonte MarcvsHdr Data 2 august 2007 14:59:51
Problema Patrate2 Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.92 kb
# include <stdio.h>

const long long int BAZA=1000000000;
const long long int NCFB=9;
const long long int MAXL=10000;
typedef struct NUMAR {long long int len, v[MAXL+1];};
NUMAR sol,staple;
long long int n;

void clear_num(NUMAR &a) {a.len=0;long long int i;for (i=0;i<=MAXL;i++) a.v[i]=0;}

void inmulteste(NUMAR a, NUMAR b, NUMAR &sol)
{
long long int i,j;
clear_num(sol);
for (i=1;i<=a.len;i++)
for (j=1;j<=b.len;j++)
	sol.v[i+j-1]+=a.v[i]*b.v[j];
sol.len=a.len+b.len-1;
i=1;
while (i<sol.len||sol.v[i]>=BAZA)
	{
	sol.v[i+1]+=sol.v[i]/BAZA;
	sol.v[i]%=BAZA;
	i++;
	}
if (i>sol.len) sol.len=i;
}

void inmulteste_nr(NUMAR &sol, long long int nr)
{
long long int i;
for (i=1;i<=sol.len;i++) sol.v[i]*=nr;
i=1;
while (i<sol.len||sol.v[i]>=BAZA)
	{
	sol.v[i+1]+=sol.v[i]/BAZA;
	sol.v[i]/=BAZA;
	i++;
	}
if (i>sol.len) sol.len=i;
}

void putere(long long int n,NUMAR &sol)
{
if (n==1) sol=staple;
else
	{
	NUMAR v,w;
	clear_num(v);
	putere(n/2,v);
	inmulteste(v,v,w);
        sol=w;
	if (n%2)
		{
		inmulteste(sol,staple,v);
		sol=v;
		}
	}
}

void factorial(long long int n, NUMAR &sol)
{
if (n==1) {sol.len=1;sol.v[1]=1;}
else
	{
	NUMAR v;
	factorial(n-1,v);
	inmulteste_nr(v,n);
	sol=v;
	}
}

void citire()
{
FILE *f=fopen("patrate2.in","r");
fscanf(f,"%lld",&n);
fclose(f);
}

long long int ncf(long long int a) {if (a==0) return 1;long long int sol=0; while (a) {sol++;a/=10;} return sol;}

void scrie(NUMAR a)
{
FILE *g=fopen("patrate2.out","w");
long long int j,nrcf,i=a.len;
fprintf(g,"%lld",a.v[i]);
i--;
while (i)
	{
	nrcf=ncf(a.v[i]);
	for (j=nrcf+1;j<=NCFB;j++) fprintf(g,"0");
	fprintf(g,"%lld",a.v[i]);
	i--;
	}
fprintf(g,"\n");
fcloseall();
}

int main()
{
citire();
NUMAR put_2,fact,rez;
clear_num(rez);clear_num(fact);
staple.len=1;staple.v[1]=2;
putere(n*n,put_2);
factorial(n,fact);
inmulteste(put_2,fact,rez);
scrie(rez);
return 0;
}