CF803F Coprime Subsequences 题解
https://www.luogu.com.cn/problem/CF803F
与当前 No. 1 题解类似,使用容斥原理,不要莫反。
首先,我们知道,对于长度为
不会这个的请不要看绿题,谢谢()
然后我们考虑,这些子序列中,有哪些子序列的
我们枚举一个
即定义式:
我们发现
求出所有
然后考虑容斥。为什么要容斥?
显然
那么我们设
显然
白话:
然后考虑实现,显然
因此可以倒叙遍历
有一个原理叫做一个序列的
时间复杂度:
评测记录:https://www.luogu.com.cn/record/144075059。
代码:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int N = 1e5 + 10;
constexpr ll MOD = 1e9 + 7;
int app[N];
ll fac[N], g[N];
inline ll mns(ll a, ll b) {
return (a - b + MOD) % MOD;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
int n, x; cin >> n;
for (int i = 0; i < n; ++i) cin >> x, ++app[x];
fac[0] = 1; for (int i = 1; i <= n; ++i) fac[i] = fac[i - 1] * 2 % MOD;
for (int i = N - 1; i; --i) {
int cnt = 0; for (int j = i; j < N; j += i) cnt += app[j];
g[i] = fac[cnt] - 1;
for (int j = i + i; j < N; j += i) g[i] = mns(g[i], g[j]);
} cout << g[1] << endl;
return 0;
}