CF1043F - Make It One

考虑加入一个数如果不能使公因数减少,则可以不放。计算可得答案必定 $\leq 7$。又发现不能直接对存在性进行容斥,容斥方案数即可。

($cnt_i$ 表示以 $i$ 为因数的数的个数,$f_i$ 表示选 $ans$ 个数使得公因数为 $i$ 的方案数。)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// =================================
// author: memset0
// date: 2019.01.27 11:20:11
// website: https://memset0.cn/
// =================================
#include <bits/stdc++.h>
#define ll long long
namespace ringo {
template <class T> inline void read(T &x) {
x = 0; register char c = getchar(); register bool f = 0;
while (!isdigit(c)) f ^= c == '-', c = getchar();
while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
if (f) x = -x;
}
template <class T> inline void print(T x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) print(x / 10);
putchar('0' + x % 10);
}
template <class T> inline void print(T x, char c) { print(x), putchar(c); }

const int N = 3e5 + 10, mod = 998244353;
int n, a[N], cnt[N], f[N], inv[N], fac[N];

inline void decd(int &a, int b) { a -= b; if (a < 0) a += mod; }
inline void sumd(int &a, int b) { a += b; if (a >= mod) a -= mod; }
inline int C(int n, int m) { return n < m ? 0 : (ll)fac[n] * inv[m] % mod * inv[n - m] % mod; }

void main() {
read(n), fac[0] = fac[1] = inv[0] = inv[1] = 1;
for (int i = 1; i <= n; i++) read(a[i]), ++cnt[a[i]];
for (int i = 2; i < N; i++) fac[i] = (ll)fac[i - 1] * i % mod;
for (int i = 2; i < N; i++) inv[i] = (ll)(mod - mod / i) * inv[mod % i] % mod;
for (int i = 2; i < N; i++) inv[i] = (ll)inv[i - 1] * inv[i] % mod;
for (int i = 1; i < N; i++)
for (int j = i << 1; j < N; j += i)
sumd(cnt[i], cnt[j]);
for (int ans = 1; ans <= 7; ans++) {
for (int i = N - 1; i >= 1; i--) {
f[i] = C(cnt[i], ans);
for (int j = i << 1; j < N; j += i)
decd(f[i], f[j]);
}
if (f[1]) return print(ans, '\n');
} puts("-1");
}

} signed main() { return ringo::main(), 0; }
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×