Pagini recente » Cod sursa (job #320074) | Cod sursa (job #824150) | Cod sursa (job #2085215) | Cod sursa (job #2334104) | Cod sursa (job #1466079)
#include <stdio.h>
int r, d, total[6];
int count = 0, i, aux, mult;
int pwr(int n, int div)
{
int c = 0;
aux = n;
while(aux)
{
if (div == 2)
aux >>= 1;
else
aux /= div;
c += aux;
}
return c;
}
int gain(int a, int b, int div)
{
int c = 0;
while(a && a%div == 0)
{
c++;
if (div == 2)
a >>= 1;
else
a /= div;
}
while(b && b%div == 0)
{
c--;
if (div == 2)
b >>= 1;
else
b /= div;
}
return c;
}
int main()
{
FILE *in, *out;
in = fopen("pascal.in", "r");
out = fopen("pascal.out", "w");
fscanf(in, "%d %d", &r, &d);
if (r > 1)
{
total[2] = pwr(r, 2) - 2*pwr(r/2, 2);
total[3] = pwr(r, 3) - 2*pwr(r/2, 3);
total[5] = pwr(r, 5) - 2*pwr(r/2, 5);
mult = (r%2 == 0) ? 1 : 2;
if (d == 2)
count += mult*(total[2] ? 1 : 0);
else if (d == 3)
count += mult*(total[3] ? 1 : 0);
else if (d == 4)
count += mult*(total[2] >= 2 ? 1 : 0);
else if (d == 5)
count += mult*(total[5] ? 1 : 0);
else if (d == 6)
count += mult*(total[2] && total[3] ? 1 : 0);
}
mult = 2;
for (i = r/2 - 1; i >= 1; i--)
{
if (d == 2 || d == 4 || d == 6)
total[2] += gain(i, r-i+1, 2);
if (d == 3 || d == 6)
total[3] += gain(i, r-i+1, 3);
if (d == 5)
total[5] += gain(i, r-i+1, 5);
if (d == 2)
count += mult*(total[2] ? 1 : 0);
else if (d == 3)
count += mult*(total[3] ? 1 : 0);
else if (d == 4)
count += mult*(total[2] >= 2 ? 1 : 0);
else if (d == 5)
count += mult*(total[5] ? 1 : 0);
else if (d == 6)
count += mult*(total[2] && total[3] ? 1 : 0);
}
fprintf(out, "%d\n", count);
fclose(in);
fclose(out);
return 0;
}