Pagini recente » Cod sursa (job #1107625) | Cod sursa (job #2788876) | Cod sursa (job #1093774) | Istoria paginii tygyn/solutie_romana | Cod sursa (job #1785004)
#include <bits/stdc++.h>
using namespace std;
int N, n, D;
int a[6];
/// a[2] = de cate ori apare 2 in C(n,k)
/// a[3] = de cate ori apare 3 in C(n,k)
/// a[5] = de cate ori apare 5 in C(n,k)
int Solve6()
{
int x, k, cnt = 0;
for (k = 1; k <= n; k++)
{
x = N - k + 1;
while (x % 2 == 0)
{
a[2]++;
x /= 2;
}
while (x % 3 == 0)
{
a[3]++;
x /= 3;
}
x = k;
while (x % 2 == 0)
{
a[2]--;
x /= 2;
}
while (x % 3 == 0)
{
a[3]++;
x /= 3;
}
if (a[2] >= 1 && a[3] >= 1) cnt += 2;
}
if (N % 2 == 0)
{
k = n + 1;
x = N - k + 1;
while (x % 2 == 0)
{
a[2]++;
x /= 2;
}
while (x % 3 == 0)
{
a[3]++;
x /= 3;
}
x = k;
while (x % 2 == 0)
{
a[2]--;
x /= 2;
}
while (x % 3 == 0)
{
a[3]--;
x /= 3;
}
if (a[2] >= 1 && a[3] >= 1) cnt++;
}
return cnt;
}
int Solve4()
{
int x, k, cnt = 0;
for (k = 1; k <= n; k++)
{
x = N - k + 1;
while (x % 2 == 0)
{
a[2]++;
x /= 2;
}
x = k;
while (x % 2 == 0)
{
a[2]--;
x /= 2;
}
if (a[2] >= 2) cnt += 2;
}
if (N % 2 == 0)
{
k = n + 1;
x = N - k + 1;
while (x % 2 == 0)
{
a[2]++;
x /= 2;
}
x = k;
while (x % 2 == 0)
{
a[2]--;
x /= 2;
}
if (a[2] >= 2) cnt++;
}
return cnt;
}
int Solve235(int D)
{
int x, k, cnt = 0;
for (k = 1; k <= n; k++)
{
x = N - k + 1;
while (x % D == 0)
{
a[D]++;
x /= D;
}
x = k;
while (x % D == 0)
{
a[D]--;
x /= D;
}
if (a[D] >= 1) cnt += 2;
}
if (N % 2 == 0)
{
k = n + 1;
x = N - k + 1;
while (x % D == 0)
{
a[D]++;
x /= D;
}
x = k;
while (x % D == 0)
{
a[D]--;
x /= D;
}
if (a[D] >= 1) cnt++;
}
return cnt;
}
int main()
{
ifstream fin("pascal.in");
ofstream fout("pascal.out");
fin >> N >> D;
n = (N - 1) / 2;
fin.close();
if (D == 2 || D == 3 || D == 5)
fout << Solve235(D) << "\n";
if (D == 4) fout << Solve4() << "\n";
if (D == 6) fout << Solve6() << "\n";
fout.close();
return 0;
}