Cod sursa(job #798499)

Utilizator stanescumalinStanescu Malin Octavian stanescumalin Data 16 octombrie 2012 18:15:26
Problema Pascal Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.24 kb
#include <iostream>
#include <fstream>

using namespace std;

long long min(long long a, long long b)
{
	return a>b?b:a;
}
long long gf(long long nr, long long d)
{
	long long i;
	for(i=0; ((nr%d==0) && (nr != 0)); i++) nr = nr/d;
	return i;
}

int main()
{
	long long r, d;
	long long x2, x3, x5, cnt, i;
	ifstream fin("pascal.in");
	ofstream fout("pascal.out");
	fin>>r; fin>>d;
	x2 = 0; x3 = 0; x5 = 0; cnt = 0;
	for(i=0; i<r/2; i++)
	{
		if(d % 2 == 0)
		{
			x2 = x2 + gf(r-i, 2);
			x2 = x2 - gf(i+1, 2);
		}
		if(d % 3 == 0)
		{
			x3 = x3 + gf(r-i, 3);
			x3 = x3 - gf(i+1, 3);
		}
		if(d % 5 == 0)
		{
			x5 = x5 + gf(r-i, 5);
			x5 = x5 - gf(i+1, 5);
		}
		if(d == 2) if(x2 > 0) cnt++;
		if(d == 3) if(x3 > 0) cnt++;
		if(d == 4) if(x2 > 1) cnt++;
		if(d == 5) if(x5 > 0) cnt++;
		if(d == 6) if(min(x2, x3) > 0) cnt++;
	}
	cnt = cnt*2;
	if(r % 2 == 0)
	{
		x2 = x2 + gf(r-i, 2);
		x3 = x3 + gf(r-i, 3);
		x5 = x5 + gf(r-i, 5);
		x2 = x2 - gf(i+1, 2);
		x3 = x3 - gf(i+1, 3);
		x5 = x5 - gf(i+1, 5);
		if(d == 2) if(x2 > 0) cnt--;
		if(d == 3) if(x3 > 0) cnt--;
		if(d == 4) if(x2 > 1) cnt--;
		if(d == 5) if(x5 > 0) cnt--;
		if(d == 6) if(min(x2, x3) > 0) cnt--;
	}
	fout<<cnt;
	return 0;
}