Cod sursa(job #2532040)

Utilizator rapunzelMihnea Andreescu rapunzel Data 27 ianuarie 2020 10:23:15
Problema Factoriale Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.68 kb
/**
Go venture far beyond the shores.
Don't forsake this life of yours.
**/
#include <fstream>
#include <vector>

using namespace std;

ifstream cin("factoriale.in");
ofstream cout("factoriale.out");

const int L = 1000;
int a[L];

void mult(int x)
{
    for (int i = 0; i + 1 < L; i++)
    {
        a[i] *= x;
        if (a[i] >= 10)
        {
            a[i + 1] += a[i] / 10;
            a[i] %= 10;
        }
    }
}

int vp(int n, int p)
{
    n /= p;
    int s = 0;
    while (n)
    {
        s += n;
        n /= p;
    }
    return s;
}

bool chk(int x)
{
    if (x <= 1)
    {
        return 0;
    }
    else
    {
        for (int d = 2; d * d <= x; d++)
        {
            if (x % d == 0)
            {
                return 0;
            }
        }
        return 1;
    }
}

vector<int> ps;
int exp[100 + 7];

int main()
{
    a[0] = 1;
    for (int i = 1; i <= 100; i++)
    {
        if (chk(i))
        {
            ps.push_back(i);
        }
    }
    int n, k;
    cin >> n >> k;
    for (int i = 1; i <= n; i++)
    {
        int x;
        cin >> x;
        for (auto &y : ps)
        {
            exp[y] += vp(x, y);
        }
    }
    for (auto &y : ps)
    {
        if (exp[y] % k)
        {
            int need = k - exp[y] % k;
            for (int i = 1; i <= need; i++)
            {
                mult(y);
            }
        }
    }
    int big = 0;
    for (int i = L - 1; i >= 0; i--)
    {
        if (a[i])
        {
            big = i;
            break;
        }
    }
    for (int i = big; i >= 0; i--)
    {
        cout << a[i];
    }
    cout << "\n";
}