結果

問題 No.546 オンリー・ワン
ユーザー 0w10w1
提出日時 2017-12-28 16:01:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,153 bytes
コンパイル時間 1,534 ms
コンパイル使用メモリ 169,236 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-23 07:59:05
合計ジャッジ時間 2,106 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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