Cod sursa(job #115799)

Utilizator mithyPopovici Adrian mithy Data 16 decembrie 2007 22:53:34
Problema Multiplu Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.49 kb
#include <stdio.h>
#include <vector>
#define NMax 2000000

long A, B;
FILE *f, *g;

int C[NMax], rest[NMax], x;
int up[NMax], cif[NMax];
int in, sf;

void citire();
void rez();
long cmmdc( long A, long B );

int main()
{
	citire();
	rez();
	return 0;
}
void rez()
{
	if ( A == B && B == 1 )
	{
		fprintf( g, "1\n" );
		return;
	}
	int i, n;
	long M = A*B /cmmdc( A, B ), poz, pozb;
	std::vector<int> sol;
	long long r;

	C[0] = 1;
	rest[1%M] = 1;
	while ( 1 )
	{
		poz = in;
		x = C[in++];
		rest[(x%M)] = 0;

		if (  x % M == 0 )
		{
			pozb = in-1;
			break;
		}

		if ( rest[(x*10)%M] == 0 )
		{
			C[++sf]        = (x*10)%M;
			rest[(x*10)%M] = 1;
			cif[sf]		   = 0;
			up[sf]		   = poz;

			if ( C[sf] == 0 )
			{
				pozb = sf;
				break;
			}
		}


		if ( rest[(x*10+1)%M] == 0)
		{
			C[++sf]          = (x*10+1)%M;
			rest[(x*10+1)%M] = 1;
			cif[sf]		     = 1;
			up[sf]		     = poz;

			if ( C[sf] == 0 )
			{
				pozb = sf;
				break;
			}
		}
	}

	sol.push_back( pozb );
	while ( up[pozb] )
	{
		sol.push_back( up[pozb] );
		up[pozb] = up[up[pozb]];
	}
		
	r = 1; n = sol.size();
	for (i=n-1; i>=0; i--)
	{
		r = (r*10+cif[sol[i]]);
	}
 	fprintf( g, "%lld\n", r );
}
long cmmdc( long A, long B )
{
	long R;
	while ( B )
	{
		R = A % B;
		A = B;
		B = R;
	}
	return A;
}
void citire()
{
	f = fopen( "multiplu.in", "rt" );
	g = fopen( "multiplu.out", "wt" );

	fscanf( f, "%ld %ld", &A, &B );
	fclose( f );
}