题解:CF1718F Burenka, an Array and Queries

· · 题解

这种题本来看着就很难绷的样子。

那么区间内互质的个数,上来就容斥。
容斥系数就是 \mu。

首先,统计区间内质因子集合可以用莫队。

然后考虑根号分治,将贡献分成,都是小于等于 B=\sqrt C 的质数造成的,和 1 个大于 B 的质数带上若干个小质数造成的。

第一类。难绷吧,暴搜。
但是这里有一些小技巧值得借鉴。

auto DFS = [&] (auto &&self, int step, int prod, int cnt)->int {
  if(step == tmp.size()) {
    return (cnt & 1) ? - C / prod : C / prod;
  }
  int ans = 0;
  if(prod * tmp[step] <= C) {
    ans += self(self, step + 1, prod * tmp[step], cnt ^ 1);
  }
  ans += self(self, step + 1, prod, cnt);
  return ans;
};

比

auto DFS = [&] (auto &&self, int step, int prod, int cnt)->int {
  if(step == tmp.size()) {
    return (cnt & 1) ? - C / prod : C / prod;
  }
  int ans = 0;
  ans += self(self, step + 1, prod, cnt);
  if(prod * tmp[step] <= C) {
    ans += self(self, step + 1, prod * tmp[step], cnt ^ 1);
  }
  return ans;
};

快两三倍。

然后就是统计大质数贡献。
我的写法比较蠢,多一个 log。具体地,大质数只会出现一个,然后我弄一个 f_i 表示 \le i 的数中,和所有小质数互质的数的个数。
这个可以调和级数枚举,再前缀和累加。

然后对大质数用整除分块,瞎写一下就是 BIT 维护一下 [l, r] 以内大质数的个数。

那么总复杂度就是 O(不知道+n\sqrt q\log C+q\sqrt C\log C)。