Pagini recente » Cod sursa (job #1984614) | Cod sursa (job #304326) | Cod sursa (job #1172643) | Cod sursa (job #3149784) | Cod sursa (job #3149348)
#include <stdio.h>
#include <stdlib.h>
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;
}
}
int main()
{
char input[100]; // Assuming a reasonable input size
if (fgets(input, sizeof(input), stdin) != NULL)
{
sscanf(input, "%d %d", &rows, &divisor);
solveProblem();
printf("%d\n", result); // Output to stdout
}
return 0;
}