Cod sursa(job #1744638)

Utilizator fanache99Constantin-Buliga Stefan fanache99 Data 20 august 2016 01:44:26
Problema Factoriale Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.22 kb
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;

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

int sieve[110], pr = 0, p[110], pw[110], v[10010];

int Power(int x, int y) {
    int answer = 0;
    while (x) {
        answer += x / y;
        x /= y;
    }
    return answer;
}

void Factorial(int x) {
    int i;
    for (int i = 1; i <= 25 && p[i] <= x; i++)
        pw[i] += Power(x, p[i]);
}

void Multiply(int a[], int x) {
    int i, c = 0;
    for (i = 1; i <= a[0] || c; i++, c /= 10)
        a[i] = (c += a[i] * x) % 10;
    a[0] = i - 1;
}

int main() {
    sieve[0] = sieve[1] = 1;
    for (int i = 2; i <= 100; i++)
        if (!sieve[i]) {
            pr++;
            p[pr] = i;
            for (int j = i * i; j <= 100; j += i)
                sieve[j] = 1;
        }
    int n, k;
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
        int x;
        cin >> x;
        Factorial(x);
    }
    v[0] = v[1] = 1;
    for (int i = 1; i <= 25; i++)
        while (pw[i] % k != 0) {
            pw[i]++;
            Multiply(v, p[i]);
        }
    for (int i = v[0]; i >= 1; i--)
        cout  << v[i];
    return 0;
}