Pagini recente » Cod sursa (job #869964) | Cod sursa (job #2764768) | Cod sursa (job #2415483) | Monitorul de evaluare | Cod sursa (job #3149347)
#include <fstream>
#include <iostream>
using namespace std;
ifstream inputFile("pascal.in");
ofstream outputFile("pascal.out");
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()
{
inputFile >> rows >> divisor;
}
void writeOutputFile()
{
outputFile << result;
}
int main()
{
readInputFile();
solveProblem();
writeOutputFile();
return 0;
}