Pagini recente » Cod sursa (job #45084) | Cod sursa (job #937645) | Cod sursa (job #1448970) | Cod sursa (job #1521469) | Cod sursa (job #3149351)
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int rows, divisor, result, baseValue;
int powerOfTwo, powerOfThree;
int min(int a, int b)
{
if (a < b)
return a;
return b;
}
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");
if (inputFile == NULL)
{
fprintf(stderr, "Error opening input file.\n");
exit(1); // Exit with an error code
}
int result = fscanf(inputFile, "%d %d", &rows, &divisor);
fclose(inputFile);
if (result != 2)
{
fprintf(stderr, "Error reading input values.\n");
exit(1); // Exit with an error code
}
}
void writeOutputFile()
{
FILE *outputFile = fopen("pascal.out", "w");
if (outputFile == NULL)
{
fprintf(stderr, "Error opening output file.\n");
exit(1); // Exit with an error code
}
fprintf(outputFile, "%d\n", result);
fclose(outputFile);
}
int main()
{
readInputFile();
solveProblem();
writeOutputFile();
return 0;
}