結果

問題 No.546 オンリー・ワン
ユーザー 0w1
提出日時 2017-12-28 16:01:03
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 1,153 bytes
コンパイル時間 1,506 ms
コンパイル使用メモリ 173,096 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-12-21 09:00:56
合計ジャッジ時間 2,003 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 7
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

template<typename T>
void mobius_transform(std::vector<T> &mob) {
  for (int i = 0; i < __builtin_ctz(mob.size()); ++i) {
    std::for_each(mob.begin(), mob.end(), [&, s = 0](const T &x) mutable {
      if (~s++ >> i & 1) mob[s - 1 | 1 << i] -= x;
    });
  }
}

int solve(int n, const std::vector<int> &c) {
  std::vector<long long> mob(1 << c.size());
  for (int s = 0; s < 1 << c.size(); ++s) {
    int lcm = std::accumulate(c.begin(), c.end(), 1, [&, i = 0](int x, int y) mutable {
      if (~s >> i++ & 1) return x;
      return static_cast<int>(std::min<long long>(1LL << 30, 1LL * x / std::__gcd(x, y) * y));
    });
    mob[s] = n / lcm;
  }
  mobius_transform(mob);
  return std::accumulate(c.begin(), c.end(), 0, [&, i = 0](int x, int y) mutable {
    return x + std::abs(mob[(1 << c.size()) - 1 ^ 1 << i++]) - std::abs(mob[(1 << c.size()) - 1]);
  });
}

signed main() {
  std::ios::sync_with_stdio(false);
  int N, L, H;
  std::cin >> N >> L >> H;
  std::vector<int> C(N);
  std::for_each(C.begin(), C.end(), [&](int &v) { std::cin >> v; });
  std::cout << solve(H, C) - solve(L - 1, C) << std::endl;
  return 0;
}
0