Cod sursa(job #488134)

Utilizator ChallengeMurtaza Alexandru Challenge Data 27 septembrie 2010 18:55:30
Problema Multiplu Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.03 kb
#include <fstream>
#include <queue>

using namespace std;

const char InFile[]="multiplu.in";
const char OutFile[]="multiplu.out";
const int MaxAB=2000010;

ifstream fin(InFile);
ofstream fout(OutFile);

queue<unsigned long long> q;
unsigned long long sol,nr;
int A,B,nrind=0;
char rest[MaxAB];

int cmmdc(int a, int b)
{
	int r=a%b;
	while(r)
	{
		a=b;
		b=r;
		r=a%b;
	}
	return b;
}

int cmmmc(int A, int B)
{
	return A*B/cmmdc(A,B);
}

int main()
{
	fin>>A>>B;
	fin.close();

	A=cmmmc(A,B);
	if(1%A==0)
	{
		sol=1;
	}
	else
	{
		rest[1]=1;
		q.push(1);
		while(true)
		{
			nr=q.front();
			q.pop();
			if((nr*10)%A==0)
			{
				sol=nr*10;
				break;
			}
			else
			{
				if(rest[nr*10%A]==0)
				{
					q.push(nr*10);
					rest[nr*10%A]=1;
				}
			}

			if((nr*10+1)%A==0)
			{
				sol=nr*10+1;
				break;
			}
			else
			{
				if(rest[(nr*10+1)%A]==0)
				{
					q.push(nr*10+1);
					rest[(nr*10+1)%A]=1;
				}
			}
		}
	}

	fout<<sol;
	fout.close();
	return 0;
}