令 表示最大的 ,满足 。
对于所有质因子 ,设 中最小值和次小值为 ,则我们的目标值就是 。
根据提示, 我们考虑扩展 容斥
我们可以类似得到
所以
但是这个做法的操作方案还是指数级别的。这个式子已经没有什么优化的空间了,我们不妨换个方向,尝试寻找目标值与原序列相等的长度能承受指数复杂度的序列。
不难发现,我们可以进行两次「找到一个和当前序列 相同的短子序列,然后把子序列从序列中丢掉」的操作,那么两次丢掉的数就能构成我们要找的序列了。事实上,因为 ,可以证明每次丢掉的序列长度最小值不超过 ,所以我们构造的序列长度不超过 ,这是可以接受的。
那么怎么找出一个 与当前序列相等,并且长度 的子序列呢?考虑随机化。首先对序列去重,然后对于数 ,如果序列中存在 使得 ,那么我们就忽略 ,在剩下的数里面一直随 个,直到随到合法的情况。
这样看上去很对,写出来也跑得挺快,正确性我不会证,欢迎大哥来叉。
还有题解的做法:设选出的子序列为 。任选一个数 ,把 加入 ,并对于所有 ,把 最小的数 加入 ,然后若 则去掉 ,正确性不难证明。
#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;
}