結果

問題 No.546 オンリー・ワン
ユーザー Kutimoti_TKutimoti_T
提出日時 2018-04-24 22:53:36
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 896 bytes
コンパイル時間 2,838 ms
コンパイル使用メモリ 147,308 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-09-10 00:01:51
合計ジャッジ時間 2,317 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i,s,e) for(int (i) = (s);(i) <= (e);(i)++)

int N;
i64 L,H;

i64 gcd(i64 a,i64 b){
  if(b == 0) return a;
  return gcd(b , a % b);
}

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

int main(){
  cin >> N >> L >> H;

  vector<i64> C(N);

  rep(i,0,N - 1) cin >> C[i];

  vector<i64> LCM((1 << N),-1);
  rep(i,0,N - 1){
    LCM[1 << i] = C[i];
  }
  i64 sum = 0;
  rep(i,1,(1 << N) - 1){
    if(LCM[i] == -1){
      int before = i - (i & -i);
      int index = 0;
      rep(j,0,N - 1){
        if((i & -i) & (1 << j)){
          index = j;
          break;
        }
      }
      LCM[i] = lcm(LCM[before],C[index]);
    }

    i64 res = H / LCM[i] - (L - 1) / LCM[i];
    i64 p;
    if(__builtin_popcount(i) & 1) p = 1;
    else p = -1;
    sum += res * p * __builtin_popcount(i);
  }

  cout << sum << endl;
}
0