Pagini recente » Cod sursa (job #1917238) | Cod sursa (job #2884914) | Cod sursa (job #1846514) | Cod sursa (job #1537052) | Cod sursa (job #137591)
Cod sursa(job #137591)
#include <iostream>
#include <fstream>
#include <math.h>
using namespace std;
typedef struct descompunere {
int poz[4], v[101];
};
typedef struct factorial {
unsigned short int val[101];
};
int main()
{
descompunere desc[101] = {{}, {}};
factorial fact[101] = {};
ifstream f1("factoriale.in");
ofstream f2("factoriale.out");
int n, k, i, j, q, xmax = 0, cpoz, s[101] = {}, x[101], ppoz, cont;
unsigned long long suma = 1;
f1 >> n >> k;
//citim x-urile si il det. pe cel mai mare
for (i = 0; i < n; ++i) {
f1 >> x[i];
if (x[i] > xmax) xmax = x[i];
}
//desc toate nr pana la xmax si calc toate factorialele pana la xmax
for (cont = 2; cont <= xmax; ++cont) {
q = cont;
//descompun q
cpoz = 0;
if ( !(q % 2) ) {
desc[cont].poz[cpoz++] = 2;
do {
q /= 2;
++desc[cont].v[2];
} while ( !(q % 2) );
}
for (i = 3; q != 1; i += 2) {
if ( !(q % i) ) {
desc[cont].poz[cpoz++] = i;
do {
q /= i;
++desc[cont].v[i];
} while ( !(q % i) );
}
}
q = cont;
//calculez factorial[q]
for (j = 0; desc[q].poz[j]; ++j) {
ppoz = desc[q].poz[j];
fact[q].val[ppoz] += desc[q].v[ppoz];
}
for (j = 2; j <= xmax; ++j)
fact[q + 1].val[j] = fact[q].val[j];
}
//adunam factorialii
for (i = 0; i < n; ++i) {
s[2] += fact[x[i]].val[2];
for (j = 3; j <= x[i]; j += 2)
s[j] += fact[x[i]].val[j];
}
//transformam s in rasp si calc suma
s[2] = (k - s[2] % k) % k;
suma *= pow(2, s[2]);
for (i = 3; i <= xmax; i += 2) {
s[i] = (k - s[i] % k) % k;
suma *= pow(i, s[i]);
}
f2 << suma << endl;
f1.close();
f2.close();
return 0;
}