题解:CF1620G Subsequences Galore

· · 题解

Blog

高维前缀和板子,学过 SOSDP 不会这题的应该反思一下。

首先先不考虑对序列中每个子序列的子集进行计数,只考虑满足这一整个序列要求的不同字符串进行计数。显然我们将所有字母的个数取个最小值,假设这个最小值为 a_i,则满足该序列要求的不同字符串共有 \prod_{i = 0}^{25} (a_i + 1) 个。因为所有字母是按照字典序排列的。

再来计算整个序列的 f 值,因为 n \le 23,显然我们可以利用集合求并的容斥来计算它本身每一个子集的答案。在这个容斥中,\operatorname{popcount} 为奇数的子集系数为 1,否则为 -1。

这样就得到了一个 O(n\times 2^{2n - 1}\times |\sum|) 的做法。

考虑优化,发现本质不同的子集只有 2^{23} 个,而每个子序列的 f 值都由它的子集构成。于是考虑使用 SOSDP 求子集和。因为子集的系数由 \operatorname{popcount} 决定,所以不受全集影响,直接求和即可。这样就得到了一个 O(n \times 2^{n-1}\times |\sum|) 的做法。代码如下:

#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;
}

为什么这个做法是 O(n \times 2^{n-1}\times |\sum|) 而不是 O(2^{n}\times |\sum|\times n)?我们考虑进入枚举字母循环的次数,发现恰好等于所有子集的 \operatorname{popcount},而这个东西的值显然是 n\times 2^{n-1}。所以才是 O(n \times 2^{n-1}\times |\sum|) 的复杂度。

发现此时的复杂度瓶颈在于计算单个序列的容斥值,需要枚举每一个 1 的每一个字母。于是考虑一个常见的状压 DP 优化思路:从 \operatorname{lowbit} 处递推计算贡献。定义 dp_{i, j} 表示全集为 i,对于其所有子集,字母 j 出现的最小次数。令 v = i \oplus \operatorname{lowbit}(i),从 v 处枚举每一个字母递推即可。时间复杂度 O(2^n\times \max(n, |\sum|)),比原做法常数小了一半。

#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;
}