結果

問題 No.546 オンリー・ワン
ユーザー 0w10w1
提出日時 2017-12-28 11:59:06
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,195 bytes
コンパイル時間 1,136 ms
コンパイル使用メモリ 65,412 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-23 07:55:52
合計ジャッジ時間 1,803 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

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

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

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

    for (int i = 0; i < 1 << n; i++) {
        int64_t 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];
            }
        }
    }

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

    return ret;
}

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

    std::vector<int64_t> 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