結果

問題 No.546 オンリー・ワン
ユーザー mamekinmamekin
提出日時 2017-07-14 23:04:49
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,463 bytes
コンパイル時間 1,037 ms
コンパイル使用メモリ 103,872 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-07-29 14:38:00
合計ジャッジ時間 1,405 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
using namespace std;

long long gcd(long long a, long long b){
    while(b != 0){
        long long tmp = a % b;
        a = b;
        b = tmp;
    }
    return a;
}

long long lcm(long long a, long long b){
    return a / gcd(a, b) * b;
}

long long solve(const vector<int>& c, int limit)
{
    int n = c.size();
    long long ans = 0;

    for(int i=1; i<(1<<n); ++i){
        bitset<32> bs(i);
        long long x = 1;
        for(int j=0; j<n; ++j){
            if(bs[j])
                x = lcm(x, c[j]);
            if(x > limit){
                x = -1;
                break;
            }
        }

        if(x != -1){
            int m = bs.count();
            if(m % 2 == 1)
                ans += limit / x * m;
            else
                ans -= limit / x * m;
        }
    }

    return ans;
}

int main()
{
    int n, l, h;
    cin >> n >> l >> h;
    vector<int> c(n);
    for(int i=0; i<n; ++i)
        cin >> c[i];

    long long ans = solve(c, h) - solve(c, l-1);
    cout << ans << endl;

    return 0;
}
0