Cod sursa(job #1785013)

Utilizator dnprxDan Pracsiu dnprx Data 20 octombrie 2016 19:53:36
Problema Pascal Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.9 kb
#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;
}