Cod sursa(job #137592)

Utilizator Omega91Nicodei Eduard Omega91 Data 17 februarie 2008 12:46:11
Problema Factoriale Scor 40
Compilator cpp Status done
Runda preONI 2008, Runda 4, Clasa a 9-a Marime 1.59 kb
#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;
		
}