結果
問題 | No.546 オンリー・ワン |
ユーザー | veqcc |
提出日時 | 2019-02-27 00:42:59 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 3 ms / 2,000 ms |
コード長 | 1,689 bytes |
コンパイル時間 | 857 ms |
コンパイル使用メモリ | 95,836 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-06-23 04:58:35 |
合計ジャッジ時間 | 1,341 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 7 |
ソースコード
#include <algorithm>#include <iostream>#include <iomanip>#include <cstring>#include <string>#include <vector>#include <queue>#include <cmath>#include <stack>#include <set>#include <map>typedef long long ll;typedef unsigned int uint;using namespace std;ll gcd(ll a, ll b) {if (b == 0) return a;return gcd(b, a % b);}ll lcm(ll a, ll b) {ll g = gcd(a, b);return a / g * b;}int main() {int N, L, H;cin >> N >> L >> H;L--;int C[N];for (int i = 0; i < N; i++) cin >> C[i];ll ans = 0LL;for (int i = 0; i < N; i++) {// C[i]だけで割り切れる数を数える// -> C[i]で割り切れる数 - C[i]以外のもので少なくとも一つ以上割り切れる数vector <int> remain;for (int j = 0; j < N; j++) {if (i != j) remain.push_back(C[j]);}ll ret = 0LL;for (int j = 1; j < (1 << (N - 1)); j++) {ll l = C[i];for (int k = 0; k < N - 1; k++) {if (j >> k & 1) l = lcm(l, remain[k]);if (l > H) break;}if (__builtin_popcount(j) % 2 == 1) ret += H / l;else ret -= H / l;}ans += H / C[i] - ret;ret = 0LL;for (int j = 1; j < (1 << (N - 1)); j++) {ll l = C[i];for (int k = 0; k < N - 1; k++) {if (j >> k & 1) l = lcm(l, remain[k]);if (l > L) break;}if (__builtin_popcount(j) % 2 == 1) ret += L / l;else ret -= L / l;}ans -= L / C[i] - ret;}cout << ans << "\n";return 0;}