题解:CF1620G Subsequences Galore
Blog
高维前缀和板子,学过 SOSDP 不会这题的应该反思一下。
首先先不考虑对序列中每个子序列的子集进行计数,只考虑满足这一整个序列要求的不同字符串进行计数。显然我们将所有字母的个数取个最小值,假设这个最小值为
再来计算整个序列的
这样就得到了一个
考虑优化,发现本质不同的子集只有
#include <bits/stdc++.h>
#define fi first
#define se second
#define eb(x) emplace_back(x)
#define pb(x) push_back(x)
#define lc(x) (tr[x].ls)
#define rc(x) (tr[x].rs)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ldb;
using pi = pair<int, int>;
const int N = 25, SLEN = 20005, C = 500005, V = 10000005;
const int mod = 998244353;
int n, tot[N][30];
char s[N][SLEN];
int sosdp[V], f[C], g[C], ctot[30];
ll ans;
int main()
{
//freopen("sample.in", "r", stdin);
//freopen("sample.out", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 0; i < n; i++)
{
cin >> s[i] + 1;
int len = strlen(s[i] + 1);
for(int j = 1; j <= len; j++)
tot[i][s[i][j] - 'a']++;
}
for(int i = 1; i < (1 << n); i++)
{
memset(ctot, 0x3f, sizeof(ctot));
int popc = 0;
for(int j = 0; j < n; j++)
{
if(((i >> j) & 1) == 0) continue;
popc++;
for(int k = 0; k < 26; k++)
ctot[k] = min(ctot[k], tot[j][k]);
}
int tmp = 1;
for(int j = 0; j < 26; j++)
tmp = (1ll * tmp * (ctot[j] + 1)) % mod;
sosdp[i] = ((((popc & 1) ? 1 : -1) * tmp) % mod + mod) % mod;
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j < (1 << n); j++)
{
if(((j >> i) & 1) == 0) continue;
sosdp[j] = (sosdp[j] + sosdp[j ^ (1 << i)]) % mod;
}
}
for(int i = 0; i < (1 << n); i++)
{
ll nans = sosdp[i], popc = 0, ism = 0;
for(int j = 0; j < n; j++)
{
if((i >> j) & 1)
{
popc++;
ism += j + 1;
}
}
nans *= popc * ism;
ans ^= nans;
}
cout << ans;
return 0;
}
为什么这个做法是
发现此时的复杂度瓶颈在于计算单个序列的容斥值,需要枚举每一个
#include <bits/stdc++.h>
#define fi first
#define se second
#define eb(x) emplace_back(x)
#define pb(x) push_back(x)
#define lc(x) (tr[x].ls)
#define rc(x) (tr[x].rs)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ldb;
using pi = pair<int, int>;
const int N = 25, SLEN = 20005, V = 8388608 + 5;
const int mod = 998244353;
int n, tot[N][30], dp[V][26];
char s[N][SLEN];
int sosdp[V];
ll ans;
int main()
{
//freopen("sample.in", "r", stdin);
//freopen("sample.out", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 0; i < n; i++)
{
cin >> s[i] + 1;
int len = strlen(s[i] + 1);
for(int j = 1; j <= len; j++)
tot[i][s[i][j] - 'a']++;
}
for(int i = 0; i < 26; i++) dp[0][i] = 0x3f3f3f3f;
for(int i = 1; i < (1 << n); i++)
{
int popc = 0, pos = 0;
for(int j = 0; j < n; j++)
{
if(((i >> j) & 1) == 0) continue;
popc++;
pos = j;
}
int tmp = 1;
for(int j = 0; j < 26; j++)
{
dp[i][j] = min(dp[i ^ (1 << pos)][j], tot[pos][j]);
tmp = (1ll * tmp * (dp[i][j] + 1)) % mod;
}
sosdp[i] = ((((popc & 1) ? 1 : -1) * tmp) % mod + mod) % mod;
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j < (1 << n); j++)
{
if(((j >> i) & 1) == 0) continue;
sosdp[j] = (sosdp[j] + sosdp[j ^ (1 << i)]) % mod;
}
}
for(int i = 0; i < (1 << n); i++)
{
ll nans = sosdp[i], popc = 0, ism = 0;
for(int j = 0; j < n; j++)
{
if((i >> j) & 1)
{
popc++;
ism += j + 1;
}
}
nans *= popc * ism;
ans ^= nans;
}
cout << ans;
return 0;
}