結果

問題 No.546 オンリー・ワン
ユーザー face4
提出日時 2019-02-12 14:17:25
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 671 bytes
コンパイル時間 503 ms
コンパイル使用メモリ 64,404 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-09-13 09:38:48
合計ジャッジ時間 1,123 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 7
権限があれば一括ダウンロードができます

ソースコード

diff #

// 包除原理?
#include<iostream>
using namespace std;

typedef long long ll;

ll gcd(ll a, ll b){
    return b == 0 ? a : gcd(b, a%b);
}

int main(){
    int n, l, h;
    cin >> n >> l >> h;
    int c[n];
    for(int i = 0; i < n; i++)  cin >> c[i];
    ll ret = 0;
    for(int s = 1; s < 1<<n; s++){
        int pop = 0; ll accum = 1;
        for(int i = 0; i < n; i++){
            if(accum > h)   break;
            if((s>>i)&1)    pop++, accum = accum * c[i] / gcd(accum, c[i]);
        }
        if(accum > h)   continue;
        ll x = (l+accum-1)/accum, y = h/accum;
        ret += (2*(pop%2)-1) * (y-x+1) * pop;
    }
    cout << ret << endl;
    return 0;
}
0