Cod sursa(job #115736)

Utilizator mithyPopovici Adrian mithy Data 16 decembrie 2007 21:18:25
Problema Multiplu Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.9 kb
#include <stdio.h>
#define NMax 2000000

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

long long C[NMax], rest[NMax], x;
int in, sf;

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

int main()
{
	citire();
	rez();
	return 0;
}
void rez()
{
	int i, j,ok;
	long M = A*B /cmmdc( A, B );
	

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

		if ( x%M == 0 )
		{
			fprintf( g, "%lld\n", x );
			break;
		}
	
		if ( rest[(x*10)%M] == 0 )
		{
			C[++sf]        = x*10;
			rest[(x*10)%M] = 1;
		}

		if ( rest[(x*10+1)%M] == 0)
		{
			C[++sf]          = x*10+1;
			rest[(x*10+1)%M] = 1;
		}
	}
}
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 );
}