CF1687E Become Big For Me

· · 题解

令 v_p(n) 表示最大的 k,满足 p^k \mid n。

对于所有质因子 p,设 v_p(a_i) 中最小值和次小值为 x_p, y_p,则我们的目标值就是 \prod_p p^{x_p + y_p}。

根据提示, 我们考虑扩展 \min - \max 容斥

\min_k(S) = \sum\limits_{T \subseteq S} (-1) ^ {|T| - k} \binom{|T| - 1}{k - 1} \max(T)

我们可以类似得到

\gcd_k(S) = \sum\limits_{T \subseteq S} \text{lcm}(T) ^ {(-1) ^ {|T| - k} \binom{|T| - 1}{k - 1}}

所以

\prod\limits_p p^{x_p + y_p} & = \gcd(a) \times \gcd_2(a) \\ & = \sum\limits_{T \subseteq S} \text{lcm}(T) ^ {(-1) ^ {|T| - 1}} \times \text{lcm}(T) ^ {(-1) ^ {|T|} (|T| - 1)} \\ & = \sum\limits_{T \subseteq S} \text{lcm}(T) ^ {(-1) ^ {|T|} (|T| - 2)} \\ \end{aligned}

但是这个做法的操作方案还是指数级别的。这个式子已经没有什么优化的空间了,我们不妨换个方向,尝试寻找目标值与原序列相等的长度能承受指数复杂度的序列。

不难发现,我们可以进行两次「找到一个和当前序列 \rm gcd 相同的短子序列,然后把子序列从序列中丢掉」的操作,那么两次丢掉的数就能构成我们要找的序列了。事实上,因为 2 \times 3 \times 5 \times 7 \times 11 \times 13 \times 17 \times 19 > 10^6,可以证明每次丢掉的序列长度最小值不超过 7,所以我们构造的序列长度不超过 14,这是可以接受的。

那么怎么找出一个 \gcd 与当前序列相等,并且长度 \le 7 的子序列呢?考虑随机化。首先对序列去重,然后对于数 u,如果序列中存在 v 使得 v < u, v \mid u,那么我们就忽略 u,在剩下的数里面一直随 7 个,直到随到合法的情况。

这样看上去很对,写出来也跑得挺快,正确性我不会证,欢迎大哥来叉。

还有题解的做法:设选出的子序列为 S。任选一个数 x = \prod_i p_i^{k_i},把 x 加入 S,并对于所有 i,把 v_{p_i}(a_i) 最小的数 y 加入 S,然后若 |S| = 8 则去掉 x,正确性不难证明。

#include <bits/stdc++.h>

using namespace std;

#define rep(i, s, e) for (int i = s; i <= e; ++i)
#define drep(i, s, e) for (int i = s; i >= e; --i)
#define file(a) freopen(#a".in", "r", stdin), freopen(#a".out", "w", stdout)
#define pv(a) cout << #a << " = " << a << endl
#define pa(a, l, r) cout << #a " : "; rep(_, l, r) cout << a[_] << ' '; cout << endl

const int N = 1e5 + 10;
const int W = 1e6;
const int L = 20;
const int K = 7;

int read() {
  int x = 0, f = 1; char c = getchar();
  for (; c < '0' || c > '9'; c = getchar()) if (c == '-') f = -1;
  for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - 48;
  return x * f;
}

mt19937 rnd(time(0));

int Rand(int l, int r) {
  return l + rnd() % (r - l + 1);
}

#define dig(x, i) ((x) >> (i) & 1)
#define pc(x) __builtin_popcount(x)

int p[W + 10], tot; bool tag[W + 10];
void sieve(int w) {
  rep(i, 2, w) {
    if (!tag[i]) p[++ tot] = i;
    for (int j = 1; j <= tot && i * p[j] <= w; ++ j) {
      tag[i * p[j]] = true;
      if (!(i % p[j])) break;
    }
  }
}

int C[L][L];
void init(int w) {
  rep(i, 0, w) C[i][0] = 1;
  rep(i, 1, w) {
    rep(j, 1, i) C[i][j] = C[i - 1][j] + C[i - 1][j - 1];
  }
}

int n, m, a[N], b[N], c[N], pw[N], id[N];
bool mask[N], pax[W + 10];

int check(int l, int r) {
  int res = 0;
  rep(i, l, r) res = __gcd(res, a[id[i]]);
  return res;
}

void filt() {
  int len = 0, cnt = 0, g = 0;
  rep(i, 1, n) if (!mask[i]) id[++ len] = i, g = __gcd(g, a[i]);
  if (!len) return;
  sort(id + 1, id + len + 1, [](int i, int j) { return a[i] < a[j]; });
  rep(i, 1, len) {
    int u = id[i];
    if (pax[a[u]]) continue;
    for (int j = a[u]; j <= W; j += a[u]) pax[j] = true;
    id[++ cnt] = u;
  }
  bool flag = false;
  int lim = min(cnt, K);
  while (!flag) {
    random_shuffle(id + 1, id + cnt + 1);
    rep(i, 0, cnt - lim) {
      if (check(i + 1, i + lim) == g) {
        rep(j, 1, lim) c[++ m] = id[i + j], mask[id[i + j]] = true;
        flag = true; break;
      }
    }
  }
  rep(i, 1, W) pax[i] = false;
  return;
}

void work() {
  sort(c + 1, c + m + 1);
  int lim = 1 << m, cnt = 0;
  rep(i, 1, m) cnt += C[m][i] * abs(i - 2);
  printf("%d\n", cnt);
  for (int s = 1; s < lim; ++ s) {
    int cnt = pc(s), t = (cnt & 1 ? -1 : 1) * (cnt - 2), op = 0;
    if (t < 0) op = 1, t = -t;
    while (t --) {
      printf("%d %d ", op, cnt);
      rep(i, 1, m) if (dig(s, i - 1)) printf("%d ", c[i]);
      printf("\n");
    }
  }
}

int main() {
  srand(time(0));
  n = read(), sieve(W), init(15);
  rep(i, 1, n) a[i] = read(), id[i] = i;
  filt(), filt();
  work();
  return 0;
}