結果

問題 No.546 オンリー・ワン
ユーザー pekempeypekempey
提出日時 2017-07-16 18:28:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,357 bytes
コンパイル時間 720 ms
コンパイル使用メモリ 74,488 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-16 22:36:01
合計ジャッジ時間 1,162 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 3 ms
5,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>

long long gcd(long long x, long long y) {
	if (y == 0) return x;
	return gcd(y, x % y);
}

long long lcm(long long x, long long y) {
	return std::min(1000000001LL, x / gcd(x, y) * y);
}

long long f(long long x, std::vector<long long> c) {
	const int n = c.size();

	// |A * !B * !C| + |!A * !B * !C| = |!B * !C|
	// |A * !B * !C| = |!B * !C| - |!A * !B * !C|

	std::vector<long long> cnt(1 << n);

	for (int i = 0; i < 1 << n; i++) {
		long long l = 1;
		for (int j = 0; j < n; j++) {
			if (i >> j & 1) {
				l = lcm(l, c[j]);
			}
		}
		cnt[i] = x / l;
	}

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < 1 << n; j++) {
			if (~j >> i & 1) {
				cnt[j | 1 << i] -= cnt[j];
			}
		}
	}
	// |A| |B| |A*B|
	// cnt[00] = x
	// cnt[01] = |A|-x
	// cnt[10] = |B|-x
	// cnt[11] = |A*B|-|A|+x-|B|

	for (int i = 0; i < 1 << n; i++) {
		int p = 1;
		for (int j = 0; j < n; j++) {
			if (i >> j & 1) {
				p *= -1;
			}
		}
		cnt[i] *= p;
	}

	long long ret = 0;
	for (int i = 0; i < n; i++) {
		int u = ((1 << n) - 1) ^ (1 << i);
		int v = ((1 << n) - 1);
		ret += cnt[u] - cnt[v];
	}

	return ret;
}

int main() {
	long long N, L, H;
	std::cin >> N >> L >> H;

	std::vector<long long> c(N);
	for (int i = 0; i < N; i++) {
		std::cin >> c[i];
	}

	std::cout << f(H, c) - f(L - 1, c) << std::endl;
}
0