Pagini recente » Cod sursa (job #3290317) | Cod sursa (job #3225069) | Cod sursa (job #2743936) | Cod sursa (job #1911689) | Cod sursa (job #3149349)
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int rows, divisor, result, baseValue;
int powerOfTwo, powerOfThree;
int calculateExponent(int n, int p)
{
int exponent = 0;
while (n % p == 0)
{
exponent++;
n /= p;
}
return exponent;
}
int calculateValue(int k)
{
switch (divisor)
{
case 4:
{
return baseValue - calculateExponent(k, 2) + calculateExponent(rows - k + 1, 2);
break;
}
case 6:
{
powerOfThree = powerOfThree - calculateExponent(k, 2) + calculateExponent(rows - k + 1, 2);
powerOfTwo = powerOfTwo - calculateExponent(k, 3) + calculateExponent(rows - k + 1, 3);
return min(powerOfTwo, powerOfThree);
break;
}
}
return baseValue + calculateExponent(rows - k + 1, divisor) - calculateExponent(k, divisor);
}
void solveProblem()
{
int halfRows = (rows - 1) / 2, D;
for (int k = 1; k <= halfRows; k++)
{
D = calculateValue(k);
if (divisor == 6)
{
result += D > 0 ? 2 : 0;
}
else
{
if (divisor == 4)
result += D > 1 ? 2 : 0;
else
result += D > 0 ? 2 : 0;
baseValue = D;
}
}
if (rows != 0 && rows % 2 == 0)
{
D = calculateValue(rows / 2);
if (divisor == 4)
result += D > 1 ? 1 : 0;
else
result += D > 0 ? 1 : 0;
}
}
void readInputFile()
{
FILE *inputFile = fopen("pascal.in", "r");
fscanf(inputFile, "%d %d", &rows, &divisor);
}
void writeOutputFile()
{
FILE *outputFile = fopen("pascal.out", "w");
fprintf(stdout, "%d\n", result);
}
int main()
{
readInputFile();
solveProblem();
writeOutputFile();
return 0;
}