Cod sursa(job #146312)

Utilizator MariusMarius Stroe Marius Data 1 martie 2008 15:39:34
Problema Indep Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2 kb
#include <fstream>

#include <vector>
#include <algorithm>

using namespace std;

const char iname[] = "indep.in";
const char oname[] = "indep.out";

#define COPY(A, B) memcpy((A), (B), sizeof(B))
#define FOR(i, a, b) for (int i = (a); i < (b); ++ i)
#define PB push_back
#define MAXN  505
#define MAXV  1005
#define MAXCIFRE 153

int n, A[MAXN];

int ret[MAXCIFRE];

int two_power[MAXN][MAXCIFRE], one[MAXCIFRE] = {1,1};


int is_prime(const int n) {
	for (int i = 2; i * i <= n; ++ i) if (n % i == 0)
		return 0;
	return 1;
}

void read_in(void)
{
	ifstream fin(iname);
	
	fin >> n; 
	FOR (i, 0, n) fin >> A[i];
	fin.close();
}

void mul(int A[], int B)
{
	int i, tr = 0;

	for (i = 1; i <= A[0] || tr; ++ i, tr /= 10)
		A[i] = (tr += A[i] * B) % 10;
	A[0] = i - 1;
}

void add(int A[], int B[])
{
	int i, tr = 0;

	for (i = 1; i <= A[0] || i <= B[0] || tr; ++ i, tr /= 10)
		A[i] = (tr += A[i] + B[i]) % 10;
	A[0] = i - 1;
}

void sub(int A[], int B[])
{
	int i, tr = 0;

	for (i = 1; i <= A[0]; ++ i)
		A[i] += (tr = (A[i] -= B[i] + tr) < 0) * 10;
	for (; A[0] > 1 && !A[A[0]]; -- A[0]);
}

void f(int primes[], int cnt_primes, int prod, int i, int sgn)
{
	if (i == cnt_primes) {
		int cnt = 0;
		FOR (i, 0, n) if (A[i] % prod == 0)
			cnt ++;
		if (sgn == +1)
			add(ret, two_power[cnt]), sub(ret, one);
		else
			add(ret, one), sub(ret, two_power[cnt]);
	} else {
		f(primes, cnt_primes, prod, i + 1, sgn);
		if (prod * primes[i] <= 1000)
			f(primes, cnt_primes, prod * primes[i], i + 1, -sgn);
	}
}

void print_out(void)
{
	ofstream fout(oname);

	for (int i = ret[0]; i >= 1; -- i)
		fout << ret[i];
	fout.close();
}

int main(void)
{	
	read_in();

	int primes[169], cnt_primes = 0;

	FOR (i, 2, 1001) if (is_prime(i))
		primes[cnt_primes ++] = i;

	COPY(two_power[0], one);
	
	FOR (i, 1, n+1) {
		COPY(two_power[i], two_power[i - 1]);
		mul(two_power[i], 2);
	}

	f(primes, cnt_primes, 1, 0, +1);

	print_out();

	return 0;
}