Cod sursa(job #2915220)

Utilizator alin.gabrielAlin Gabriel Arhip alin.gabriel Data 22 iulie 2022 08:23:54
Problema Factoriale Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.46 kb
#include <fstream>
#include <cmath>
#include <iostream>
using namespace std;

int main() {
    ifstream fin("factoriale.in");
    ofstream fout("factoriale.out"); 
    
    int n, k;
    fin >> n >> k;
    int f[25] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 
                 29, 31, 37, 41, 43, 47, 53, 59, 
                 61, 67, 71, 73, 79, 83, 89, 97};
    int p[25] = {};
    for (int i = 0; i < n; i++) {
        int x;
        fin >> x;
        for (int j = 2 ; j <= x ; j++) {
            int d = 0;
            int aux = j;
            while (aux / f[d] >= 1) {
                if (aux % f[d] == 0) {
                    p[d] += 1;
                    aux = aux / f[d];
                } else d++;
            }
        }
    }

    // for (int i = 0 ; i < 25 ; i++)
    //     cout << f[i] << " ";
    // cout << "\n";
    // for (int i = 0 ; i < 25 ; i++)
    //     if (p[i] != 0)
    //         cout << p[i] << " ";

    unsigned long long res = 1;
    for (int i = 0 ; i < 25 ; i++)
        if (p[i] != 0 && p[i] % k != 0) {
            if (p[i] > k) {
                res *= pow(f[i], k - (p[i] % k));
                //cout << f[i] << " to the power of: " << k - (p[i] % k) << "\n";
            }
            else {
                res *= pow(f[i], k - p[i]);
                //cout << f[i] << " to the power of: " << k - p[i] << "\n";
            }
        }

    fout << res;

    fin.close();
    fout.close();
    return 0;
}