結果

問題 No.546 オンリー・ワン
ユーザー PachicobuePachicobue
提出日時 2017-07-14 22:32:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,611 bytes
コンパイル時間 1,280 ms
コンパイル使用メモリ 166,940 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-07-29 14:31:24
合計ジャッジ時間 2,041 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

#define show(x) cout << #x << " = " << x << endl

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "sz=" << v.size() << "\n[";
    for (const auto& p : v) {
        os << p << ",";
    }
    os << "]\n";
    return os;
}

template <typename S, typename T>
ostream& operator<<(ostream& os, const pair<S, T>& p)
{
    os << "(" << p.first << "," << p.second
       << ")";
    return os;
}


constexpr ll MOD = 1e9 + 7;

template <typename T>
constexpr T INF = numeric_limits<T>::max() / 100;

template <typename T>
T gcd(const T a, const T b)
{
    return b ? gcd(b, a % b) : a;
}

template <typename T>
T lcm(const T a, const T b)
{
    return (a / gcd(a, b)) * b;
}


int main()
{
    int N;
    cin >> N;
    ll L, H;
    cin >> L >> H;
    vector<ll> c(N);
    ll total = 0;
    for (ll i = 0; i < N; i++) {
        cin >> c[i];
        total += H / c[i] - (L - 1) / c[i];
    }
    const int maximum = 1 << N;
    for (int i = 1; i < maximum; i++) {
        int num = i;
        int cnt = 0;
        ll d = 1;
        for (int j = 0; j < N; j++) {
            if (num % 2 == 1) {
                cnt++;
                d = lcm(d, c[j]);
            }
            num /= 2;
        }
        if (cnt <= 1) {
            continue;
        }
        if (cnt % 2 == 0) {
            total -= cnt * ((H / d) - (L - 1) / d);
        } else {
            total += cnt * ((H / d) - (L - 1) / d);
        }
    }
    cout << total << endl;
    return 0;
}
0