Cod sursa(job #1468732)

Utilizator tamionvTamio Vesa Nakajima tamionv Data 6 august 2015 20:35:43
Problema Light2 Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.74 kb
#include <fstream>
#include <vector>
using namespace std;

constexpr int bitcount(const long long x, const int rez = 0){
	return (x==0) ? rez : bitcount(x ^ (x&-x), rez+1); }

constexpr int euclid(const long long a, const long long b){
	return (a==0) ? b : euclid(b%a, a); }

int main(){
	ifstream f("light2.in");
	ofstream g("light2.out");
	long long n, k;
	f >> n >> k;
	vector<long long> v(k);
	for(auto& x : v){
		f >> x; }

	long long rez = 0;
	for(int config = 1; config < (1<<k); ++config){
		long long nr = 1, lcm = 1;
		for(int i = 0; i < k; ++i){
			if((config>>i)&1){
				nr *= v[i];
				lcm = ((lcm * v[i]) / euclid(lcm, v[i])); } }
		const int bc = bitcount(config);
		rez += (bc%2==0 ? -1 : 1) * (n/lcm) * (1<<(bc-1)); }
	g << rez;
	return 0; }