Cod sursa(job #3223966)

Utilizator unomMirel Costel unom Data 14 aprilie 2024 11:12:34
Problema Pascal Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.99 kb
#include <fstream>

using namespace std;

ifstream in("pascal.in");
ofstream out("pascal.out");
int n, d, ans;
int fact[2][5000005];
//fact[i] = cati d avem in descompunere lui factorial[i]

int main()
{
    in>>n>>d;

    int x;
    for(int i = 2; i<=n; i++)
    {
        fact[0][i] = fact[0][i - 1];
        fact[1][i] = fact[1][i - 1];

        if(d == 4)
        {
            x = i;
            while(x % 2 == 0)
            {
                fact[0][i]++;

                x /= 2;
            }
        }
        else if(d == 6)
        {
            x = i;
            while(x % 2 == 0)
            {
                fact[0][i]++;

                x /= 2;
            }

            while(x % 3 == 0)
            {
                fact[1][i]++;

                x /= 3;
            }
        }
        else
        {
            x = i;
            while(x % d == 0)
            {
                fact[0][i]++;

                x /= d;
            }
        }
    }


    if(d == 4)
    {
        int nr;
        for(int j = 0; j<=n; j++)
        {
            nr = fact[0][n] - fact[0][j] - fact[0][n - j];
            nr /= 2;

            if(nr > 0)
            {
                ans++;
                //out<<j<<" -> "<<nr<<'\n';
            }
        }
    }
    else if(d == 6)
    {
        int nr2, nr3;
        for(int j = 0; j<=n; j++)
        {
            nr2 = fact[0][n] - fact[0][j] - fact[0][n - j];
            nr3 = fact[1][n] - fact[1][j] - fact[1][n - j];


            if(nr2 > 0 && nr3 > 0)
            {
                ans++;
                //out<<j<<" -> "<<nr<<'\n';
            }
        }
    }
    else
    {
        int nr;
        for(int j = 0; j<=n; j++)
        {
            nr = fact[0][n] - fact[0][j] - fact[0][n - j];

            if(nr > 0)
            {
                ans++;
                //out<<j<<" -> "<<nr<<'\n';
            }
        }
    }

    out<<ans;

    return 0;
}