0w1

Yuki 546 オンリー・ワン

Problem Description:
Given N numbers as C[0...N], L, and H, find the number of values in range [L, H] that is multiple of exactly one value in C.

Constraints:
1 ≤ N ≤ 10
1 ≤ C[i] ≤ 1e9
1 ≤ L ≤ H ≤ 1e9

Solution:
Solve for (L' = 1, H' = H) and (L' = 1, H' = L - 1) independently.
inex[s] = number of values in range [L', H'] that is multiple of lcm in set s.
Apply mobius transformation on inex[], and we have:
mob[s] = number of values in range [L', H'] that is multiple of any value in set s, with negative sign when set size is even.
The answer is sum(abs(mob[s] + mob[s - c] for c in s).

Code:

#include <bits/stdc++.h>

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

int solve(int n, const std::vector<int> &c) {
  std::vector<long long> mob(1 << c.size());
  for (int s = 1; s < mob.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::abs(std::accumulate(c.begin(), c.end(), 0, [&, i = 0](long long x, int y) mutable {
    return x + mob[(1 << c.size()) - 1 ^ 1 << i++] + 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;
}