Nombre Password [ Regístrate ]

El tamaño no es lo importante (OIE 4 - 2000) - Código en C
/* OIE 2000 - 1a fase */
/*
  El tamaño no es lo que importa
  Revision: 13/9/2001
*/


#include <stdio.h>
#include <string.h>

#define MAXN 1024L

void multiplica(char *,char *,int);

char sol[2*MAXN+5];

int main()
{
	int a,n,n1,n2,qwer;
	char f1a[MAXN+5];
	char f2a[MAXN+5];
	char f1[MAXN+5];
	char f2[MAXN+5];
	char c1,c2;

	freopen("mult.dat","r",stdin);
	freopen("mult.res","w",stdout);

	for(scanf(" %d",&qwer);qwer>0;qwer--)
	{
		memset(f1,'0',sizeof(f1));
		memset(f1a,0,sizeof(f1a));
		scanf(" %c",&c1);
		f1a[0]=c1;
		scanf("%[^\n]",(f1a+(c1!='-')));
		for(n1=strlen(f1a),a=0;a<n1;a++)
			f1[n1-a-1]=f1a[a];

		memset(f2,'0',sizeof(f2));
		memset(f2a,0,sizeof(f2));
		scanf(" %c",&c2);
		f2a[0]=c2;
		scanf("%[^\n]",(f2a+(c2!='-')));
		for(n2=strlen(f2a),a=0;a<n2;a++)
			f2[n2-a-1]=f2a[a];

		for(a=1;a<n1;a*=2);
		n1=a;
		for(a=1;a<n2;a*=2);
		n2=a;
		n=(n1>n2)?n1:n2;
		
		multiplica(f1,f2,n);

		for(a=2*n+2;a>=0 && sol[a]=='0';a--);
		if(a==-1)
			printf("0");
		else
		{
			if(!((c1=='-' && c2=='-') || (c1!='-' && c2!='-')))
				printf("-");

			for(;a>=0;a--)
				printf("%c",sol[a]);
		}
		printf("\n");
	}
	
	return(0);
}

void multiplica(char *f1,char *f2,int n)
{
	int a,b;
	char xiyi[2*MAXN+5];
	char xdyd[2*MAXN+5];
	char c1[MAXN+5];
	char c2[MAXN+5];
	char c1c2[2*MAXN+5];

	if(n==1)
	{
		memset(sol,'0',sizeof(sol));
		sol[0]=(f1[0]-'0')*(f2[0]-'0');
		sol[1]=(sol[0]/10)+'0';
		sol[0]=(sol[0]%10)+'0';
		return;
	}

	memset(xiyi,'0',sizeof(xiyi));
	multiplica(f1+n/2,f2+n/2,n/2);
	strcpy((char *)xiyi,(char *)sol);
	memset(xdyd,'0',sizeof(xdyd));
	multiplica(f1,f2,n/2);
	strcpy((char *)xdyd,(char *)sol);

	memset(c1,0,sizeof(c1));
	memset(c2,0,sizeof(c2));
	for(a=0,b=n/2;b<n;a++,b++)
	{
		c1[a]=f1[b]-f1[a]+'0';
		c2[a]=f2[a]-f2[b]+'0';
	}
	memset(c1c2,'0',sizeof(c1c2));
	multiplica(c1,c2,n/2);
	strcpy((char *)c1c2,(char *)sol);

	memset(sol,'0',sizeof(sol));
	for(a=0;a<2*n+2;a++)
	{
		sol[a]-='0';
		sol[a]+=xdyd[a]-'0';
		sol[a+n/2]+=xdyd[a]-'0';
		sol[a+n/2]+=xiyi[a]-'0';
		sol[a+n/2]+=c1c2[a]-'0';
		sol[a+n]+=xiyi[a]-'0';

		sol[a+1]+=sol[a]/10;
		sol[a]=sol[a]%10;
		if(sol[a]<0)
		{
			sol[a]+=10;
			sol[a+1]--;
		}
		sol[a]+='0';
	}
	
	return;
}


© (2001-2008) ALGORITMIA.NET - Política de privacidad