Mai intai trebuie sa te autentifici.

Cod sursa(job #603374)

Utilizator SteveStefan Eniceicu Steve Data 15 iulie 2011 18:20:46
Problema Pascal Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.26 kb
#include <fstream.h>

int main()
{
	long R;
	long D, aux, Rs=0, Rs2=0, i, total=0;
	int v[5000001], v2[5000001];
	ifstream fin;
	fin.open("pascal.in");
	fin>>R>>D;
	fin.close();
	if (D == 2)
	{
		aux=R;
		while (aux)
		{
			aux/=2;
			Rs+=aux;
		}
		v[0]=0;
		for (i=1; i<=R; i++)
		{
			v[i]=0;
			aux=i;
			while (aux)
			{
				aux/=2;
				v[i]+=aux;
			}
		}
		if (R & 1)
		{
			for (i=1; i<=R/2; i++)
			{
				if (Rs > v[i]+v[R-i]) total+=2;
			}
		}
		else
		{
			for (i=1; i<R/2; i++)
			{
				if (Rs > v[i]+v[R-i]) total+=2;
			}
			if (Rs > 2*v[R/2]) total++;
		}
	}
	else
	{
		if (D == 3)
		{
			aux=R;
			while (aux)
			{
				aux/=3;
				Rs+=aux;
			}
			v[0]=0;
			for (i=1; i<=R; i++)
			{
				v[i]=0;
				aux=i;
				while (aux)
				{
					aux/=3;
					v[i]+=aux;
				}
			}
			if (R & 1)
			{
				for (i=1; i<=R/2; i++)
				{
					if (Rs > v[i]+v[R-i]) total+=2;
				}
			}
			else
			{
				for (i=1; i<R/2; i++)
				{
					if (Rs > v[i]+v[R-i]) total+=2;
				}
				if (Rs > 2*v[R/2]) total++;
			}
                }
		else
		{
			if (D == 5)
			{
				aux=R;
				while (aux)
				{
					aux/=5;
					Rs+=aux;
				}
				v[0]=0;
				for (i=1; i<=R; i++)
				{
					v[i]=0;
					aux=i;
					while (aux)
					{
						aux/=5;
						v[i]+=aux;
					}
				}
				if (R & 1)
				{
					for (i=1; i<=R/2; i++)
					{
						if (Rs > v[i]+v[R-i]) total+=2;
					}
				}
				else
				{
					for (i=1; i<R/2; i++)
					{
						if (Rs > v[i]+v[R-i]) total+=2;
					}
					if (Rs > 2*v[R/2]) total++;
				}
                        }
			else
			{
				if (D == 4)
				{
					aux=R;
					while (aux)
					{
						aux/=2;
						Rs+=aux;
					}
					//Rs/=2;
					v[0]=0;
					for (i=1; i<=R; i++)
					{
						aux=i;
						v[i]=0;
						while (aux)
						{
							aux/=2;
							v[i]+=aux;
						}
						//v[i]/=2;
						//v[i]+=v[i-1];
					}
					if (R & 1)
					{
						for (i=1; i<=R/2; i++)
						{
							aux=v[i]+v[R-i];
							if (Rs > aux+1) total+=2;
						}
					}
					else
					{
						for (i=1; i<R/2; i++)
						{
							aux=v[i]+v[R-i];
							if (Rs > aux+1) total+=2;
						}
						if (Rs > (v[R/2]*2)+1) total++;
					}
				}
				else
				{
					if (D == 6)
					{
						aux=R;
						while (aux)
						{
							aux/=2;
							Rs+=aux;
						}
						aux=R;
						while (aux)
						{
							aux/=3;
							Rs2+=aux;
						}
						v[0]=0;
						v2[0]=0;
						for (i=1; i<=R; i++)
						{
							v[i]=0;
							aux=i;
							while (aux)
							{
								aux/=2;
								v[i]+=aux;
							}
						}
						for (i=1; i<=R; i++)
						{
							v2[i]=0;
							aux=i;
							while (aux)
							{
								aux/=3;
								v2[i]+=aux;
							}
						}
						if (R & 1)
						{
							for (i=1; i<=R/2; i++)
							{
								if ((Rs > v[i]+v[R-i]) && (Rs2 > v2[i]+v[R-i])) total+=2;
							}
						}
						else
						{
							for (i=1; i<R/2; i++)
							{
								if ((Rs > v[i]+v[R-i]) && (Rs2 > v2[i]+v2[R-i])) total+=2;
							}
							if ((Rs > 2*v[R/2]) && (Rs2 > 2*v2[R/2])) total++;
						}
					}
				}
			}
		}
        }
	ofstream fout;
	fout.open("pascal.out");
	fout<<total;
	fout.close();
	return 0;
}