Cod sursa(job #388163)

Utilizator ionutz32Ilie Ionut ionutz32 Data 29 ianuarie 2010 14:36:14
Problema Pascal Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.39 kb
#include <stdio.h>
int r,d,fact[5000005],i,c,fact2[5000005],sol;
int min(int a,int b)
{
	if (a<b)
		return a;
	else
		return b;
}
int calc()
{
	switch (d)
	{
		case 2:
		case 3:
		case 5:
			if (fact[r]-fact[r-i]-fact[i])
				return 1;
			else
				return 0;
		break;
		case 4:
			if ((fact[r]-(fact[r-i]+fact[i]))/2)
				return 1;
			else
				return 0;
		break;
		case 6:
			if (min(fact[r]-(fact[r-i]+fact[i]),fact2[r]-(fact2[r-i]+fact2[i])))
				return 1;
			else
				return 0;
		break;
	}
}
int main()
{
	freopen("pascal.in","r",stdin);
	freopen("pascal.out","w",stdout);
	scanf("%d %d",&r,&d);
	switch (d)
	{
		case 2:
		case 3:
		case 5:
			for (i=1;i<=r;++i)
			{
				c=i;
				while (c%d==0)
				{
					++fact[i];
					c=c/d;
				}
				fact[i]+=fact[i-1];
			}
		break;
		case 4:
			for (i=1;i<=r;++i)
			{
				c=i;
				while (c%2==0)
				{
					++fact[i];
					c=c/2;
				}
				fact[i]+=fact[i-1];
			}
		break;
		case 6:
			for (i=1;i<=r;++i)
			{
				c=i;
				while (c%2==0)
				{
					++fact[i];
					c=c/2;
				}
				fact[i]+=fact[i-1];
				while (c%3==0)
				{
					++fact2[i];
					c=c/3;
				}
				fact2[i]+=fact2[i-1];
			}
		break;
	}
	if (r%2)
	{
		for (i=0;i<=r/2;++i)
			sol+=calc();
		printf("%d",sol*2);
	}
	else
	{
		for (i=0;i<r/2;++i)
			sol+=calc();
		printf("%d",sol*2+calc());
	}
	return 0;
}