Cod sursa(job #2240367)

Utilizator theodor.moroianuTheodor Moroianu theodor.moroianu Data 13 septembrie 2018 10:10:55
Problema Light2 Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.13 kb
#include <bits/stdc++.h>
using namespace std;
typedef long long i64;

i64 coef_x[100]; /// ce am facut cand am luat cate x
i64 comb[1010][1010];

void calc_comb()
{
	for (int i = 0; i < 100; i++)
		comb[i][0] = 1;

	for (int i = 1; i <= 100; i++)
		for (int j = 1; j <= 100; j++)
			comb[i][j] = comb[i][j - 1] + comb[i - 1][j - 1];
}

void calc_coef()
{
	calc_comb();
	for (int i = 1; i <= 100; i++) {
		i64 coef_act = 0;
		for (int j = 1; j < i; j++)
			coef_act += coef_x[j] * comb[i][j];
		i64 ce_vreau = ((i & 1) ? 1 : 0);
		coef_x[i] = ce_vreau - coef_act;
	}
}

i64 gcd(i64 a, i64 b)
{
	while (a)
		swap(a, b %= a);
	return b;
}

i64 l[100];
int k;
i64 ans;
i64 N;

void bkt(int poz, int nr, i64 lcm)
{
	if (poz == k) {
		ans += coef_x[nr] * (N / lcm);
		return;
	}

	/// 1 -> iau poz
	bkt(poz + 1, nr + 1, lcm / gcd(lcm, l[poz]) * l[poz]);
	/// 2 -> nu iau poz
	bkt(poz + 1, nr, lcm);
}

int main()
{
	ifstream in("light2.in");
	ofstream out("light2.out");

	in >> N >> k;
	for (int i = 0; i < k; i++)
		in >> l[i];

	calc_coef();

	bkt(0, 0, 1);

	out << ans << '\n';

	return 0;
}