結果

問題 No.546 オンリー・ワン
ユーザー 0x19f0x19f
提出日時 2017-07-15 20:31:23
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,860 bytes
コンパイル時間 581 ms
コンパイル使用メモリ 75,148 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-16 21:53:46
合計ジャッジ時間 1,045 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#define REP(i, a, n) for(int i = ((int) a); i < ((int) n); i++)
using namespace std;
typedef long long ll;

int N;
ll L, H, C[10];

template<typename FI>
void parted_rotate(FI first1, FI last1, FI first2, FI last2) {
  if(first1 == last1 || first2 == last2) return;
  FI next = first2;
  while(first1 != next) {
    std::iter_swap(first1++, next++);
    if(first1 == last1) first1 = first2;
    if(next == last2) {
      next = first2;
    } else if(first1 == first2) {
      first2 = next;
    }
  }
}

template<typename BI>
bool next_combination_imp(BI first1, BI last1, BI first2, BI last2) {
  if(first1 == last1 || first2 == last2) return false;
  auto target = last1; --target;
  auto last_elem = last2; --last_elem;
  while(target != first1 && !(*target < *last_elem)) --target;
  if(target == first1 && !(*target < *last_elem)) {
    parted_rotate(first1, last1, first2, last2);
    return false;
  }
  auto next = first2;
  while(!(*target < *next)) ++next;
  std::iter_swap(target++, next++);
  parted_rotate(target, last1, next, last2);
  return true;
}

template<typename BI>
inline bool next_combination(BI first, BI mid, BI last) {
  return next_combination_imp(first, mid, mid, last);
}

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

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

int main(void) {
  cin >> N >> L >> H;
  REP(i, 0, N) cin >> C[i];

  ll fact[11];
  fact[0] = 1;
  REP(i, 0, 10) fact[i + 1] = fact[i] * (i + 1);

  ll ans = 0;
  REP(i, 1, N + 1) {
    ll a[20];
    REP(i, 0, N) a[i] = i;
    ll cnt = 0;
    do {
      ll p = 1;
      REP(j, 0, i) p = lcm(p, C[a[j]]);
      cnt += H / p - (L - 1) / p;
    } while(next_combination(a, a + i, a + N));
    ans += (i % 2 == 0 ? -1 : 1) * i * cnt;
  }
  cout << ans << endl;
}
0