子集DP(SOS DP)

· · 算法·理论

板子

如果想求

f_{T}=\sum_{i\subseteq T}a_i

怎么办?其中 T\leq 2^n。

solution 1

根据定义,有朴素 O(4^n) 算法。

for(int state=0;state<=T;state++)
    for(int i=0;i<=state;i++)
        if((state&i)==i) f[state]+=f[i];

solution 2

如果 i 每次枚举的都是 state 的子集,那就可以加快速度。时间复杂度 O(3^n),读者自证不难(二项式定理)。

for(int state=0;state<=T;state++)
{
    f[state]=a[0];
    for(int i=state;i>0;i=(i-1)&state)
       f[state]+=f[i];
}

solution 3

对于 state 的子集,即每一个 1 的位置可以选或不选,即可以对逐位dp。时间复杂度 O(n\times 2^n)。

for(int state=0;state<=T;state++)
{
    f[state][0]=a[state]+a[state^1]*(state&1);
    for(int i=1;i<n;i++)
        f[state][i]=f[state][i-1]+f[state^(1<<i)][i-1]*(state>>i&1);
}

solution 4

solution 3^{-空间}

for(int state=0;state<=T;state++) f[state]=a[state];
for(int i=0;i<n;i++)
    for(int state=0;state<=T;state++)
         f[state]+=f[state^(1<<i)];

例题

T1 超级好伙伴

有 n 个学生,编号 1 至 n,第i个学生的品质是 s_i。

对于学生 a 和学生 b 来说,如果 s_a\&s_b 等于0(其中 \& 就是二进制位操作的“与”),那么我们称 a 和 b 是"好伙伴"。

如果学生 a 有多个”好伙伴“,那么品质最好的那个就是 a 的”超级好伙伴"。

问题是: 对于任意的 i(1\leq i\leq n),输出第 i 个学生的“超级好伙伴"的品质,如果第 i 个学生没有“好伙伴"输出 -1。

input:

第一行,一个整数 n。1\leq n\leq 1000000。

第二行,n 个整数,第 i 个整数是 s_i, 1\leq s_i\leq 4000000.

output:

共 n 行,每行一个整数。

example1:

input:
2
90 36
ouput:
36 90

example2:

input:
4
3 6 3 6
output:
-1 -1 -1 -1

solution of T1

对于 a\&b=0,有 b\subseteq \sim a。子集dp即可。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL N=1e6+10,T=1<<22;
LL n,a[N],f[T];
int main()
{
    scanf("%lld",&n);
    for(int i=1;i<=n;i++) scanf("%lld",a+i);
    for(int i=0;i<T;i++) f[i]=-1;
    for(int i=1;i<=n;i++) f[a[i]]=a[i];
    for(int i=0;i<22;i++)
        for(int state=0;state<T;state++)
            if(state&(1<<i))
                f[state]=max(f[state^(1<<i)],f[state]);
    for(int i=1;i<=n;i++)
        printf("%lld ",f[a[i]^(T-1)]);
    return 0;
}

T2 箱子

有 n 个箱子,每个箱子里有 m 种玩具,每种玩具以 1-n 的数编号。具体来说,第 i 个箱子里有 k_i 个玩具。

你需要选出若干个箱子使得选中的箱子中包括所有种类的玩具,答案对 10^9+7 取模。

input:

第一行包含两个整数 n,m(1\leq n\leq 10^6,1\leq m\leq 20)。

接下来的 n 行,第 i 行第一个整数 k_i(0\leq k_i\leq m),紧接着 k_i 个正整数,表示第 i 个箱子里的玩具情况。

output:

输出方案数对 10^9+7 取模后的结果。

example1:

input:
3 3
3 1 2 3
3 1 2 3
3 1 2 3
output:
7

example2:

input:
3 3
1 1
1 2
1 3
output:
1

example3:

input:
4 5
2 2 3
2 1 2
4 1 2 3 5
4 1 2 4 5
output:
6

solution of T2

直接算很难,考虑容斥。令 f_{state} 为满足

对于第 i(0\leq i< m) 位,为 1 即不管;为 0 则要求这一位为 0。

的数的个数。

容易发现,满足这样的数一定时 state 的子集。子集dp即可。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL N=1e6+10,T=1<<20,P=1e9+7;
LL n,m,f[T],pw[N];
int main()
{
    scanf("%lld%lld",&n,&m);
    for(int i=1;i<=n;i++)
    {
        LL k,a,res=0;
        scanf("%lld",&k);
        while(k--)
        {
            scanf("%lld",&a);
            res|=1<<a-1;
        }
        f[res]++;
    }
    LL T=1<<m;
    for(int i=0;i<m;i++)
        for(int state=0;state<T;state++)
            if(state&(1<<i))
                f[state]=(f[state]+f[state^(1<<i)])%P;
    pw[0]=1;
    for(int i=1;i<=n;i++) pw[i]=pw[i-1]*2%P;
    LL ans=(pw[n]-1+P)%P;
    for(int state=0;state<T-1;state++)
    {
        LL si=1;
        for(int i=0;i<m;i++)
            if(!(state>>i&1))
                si=-si;
        ans=(ans+si*(pw[f[state]]-1+P)%P+P)%P;
    }
    printf("%lld",ans);
    return 0;
}

T3 回文子串

给出一个长度为 n 的字符串 S,求 S 有多少个不同的子串是回文串。

字符串S的每一个字符都是小写字母,而且是字母表的前17个小写字母,即 a 至 q。

注意:就算内容一样,但是位置不一样的子串也被认为是不同的子串,例如: aba 有4个不同的回文子串:a、b、a、aba。

再给出一个候选串 $T$,$T$ 是只包含字母表的前 $17$ 个小写字母,且 $T$ 没有重复字符。 对于字符串 $S$ 的每一个`?`, 你都可以用候选串T的任意一个字符去替换,显然,这样就能生成很多个不一样的 $S$。 任务是:求所有的S的所有回文子串的数量,答案模$998244353$。 例如: $S=$ `ab??`,$T=$`ab`。 那么可以生成4个不同的S,分别是:$S_1=$`abaa`,$S_2=$`abab`, $S_3=$`abba`, $S_4=$`abbb` 其中 $S_1$ 有 $6$ 个回文子串,$S_2$ 有 $8$ 个回文子串, $S_3$ 有 $6$ 个回文子串,$S_4$ 有 $7$ 个回文子串。 所以 答案$=6+8+6+7=27

input:

第一行,一个整数 n。 1\leq n\leq 1000。

第二行,一个长度为 n 的字符串 S。

第三行,一个整数 Q,表示有 Q 次询问,1\leq Q\leq 200000。

接下来有 Q 行,每 i 行是一个候选串 T_i,表示如果在候选串 T_i 的支持下,能够生成的所有字符串S的所有回文子串数量。

其中 T_i 的每个字符都是字母表的前 17 个小写字母,且没有重复字符。

注意:每个询问独立,并不是真正去替换 S,只是为了计算结果而已,即:每次询问的S都是一样的,都是从输入数据第二行读入的那个字符串 S。

output:

共 Q 行,每行一个整数。

example:

intput:
7
ab??aba
8
a
b
ab
abc
abcd
abcde
abcdef
abcdefg
output:
14
13
55
105
171
253
351
465

solution of T3

对每一个字串进行考虑贡献,同时其外的 ? 可以是 T 中的每一个字符,所以考虑当 T 的长度为 L 时,字串的贡献。

枚举回文中心,并考虑每次在两边拓展时对答案的贡献。令当前贡献为 x,上一次的贡献为 y。

case1: 加入字串的是一样的字母,例如 a 和 a,x=y。

case2: 加入字串的是不一样的字母,例如 a 和 b,x=0。

case3: 加入字串的的是一个问号和一个字母,例如 ? 和 a,x=y,但此时有一点小不同,因为有贡献当且仅当 T 中包含 a。记 lim 为当前 T 的限制,以二进制存储。

case4: 加入字串的是两个 ?,则 x=y\times|T|。

对于每一次拓展,都将当前的状态对 f 加入贡献。当询问时,对 lim 做子集dp。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL N=1e3+10,T=1<<17,P=998244353;
LL n,q,f[18][T];
char s[N],t[N];
LL ksm(LL a,LL b) { LL res=1; for(;b;b>>=1,a=a*a%P) if(b&1) res=res*a%P; return res; }
void upd(LL &a,LL b) { a=(a+b)%P; }
void init()
{
    LL qmtot=0;
    for(int i=1;i<=n;i++) qmtot+=(s[i]=='?');
    for(int L=1;L<=17;L++)
    {
        for(int mid=1;mid<=n;mid++)
        {
            LL h=mid,t=mid,res=ksm(L,qmtot),lim=0;
            while(true)
            {
                upd(f[L][lim],res);
                h--; t++;
                if(h<1||t>n) break;
                if(s[h]!=s[t])
                    if(s[h]=='?')
                    {
                        lim|=1<<s[t]-'a';
                        res=res*ksm(L,P-2)%P;
                    }
                    else if(s[t]=='?')
                    {
                        lim|=1<<s[h]-'a';
                        res=res*ksm(L,P-2)%P;
                    }
                    else res=0;
                if(s[h]==s[t]&&s[h]=='?')
                    res=res*ksm(L,P-2)%P;
            }
            h=mid+1,t=mid,res=ksm(L,qmtot),lim=0;
            while(true)
            {
                h--; t++;
                if(h<1||t>n) break;
                if(s[h]!=s[t])
                    if(s[h]=='?')
                    {
                        lim|=1<<s[t]-'a';
                        res=res*ksm(L,P-2)%P;
                    }
                    else if(s[t]=='?')
                    {
                        lim|=1<<s[h]-'a';
                        res=res*ksm(L,P-2)%P;
                    }
                    else res=0;
                if(s[h]==s[t]&&s[h]=='?')
                    res=res*ksm(L,P-2)%P;
                upd(f[L][lim],res);
            }
        }
        for(int i=0;i<17;i++)
            for(int state=0;state<T;state++)
                if(state&(1<<i))
                    upd(f[L][state],f[L][state^(1<<i)]);
    }
}
int main()
{
    scanf("%lld%s%lld",&n,s+1,&q);
    init();
    while(q--)
    {
        scanf("%s",t+1);
        LL len=strlen(t+1),lim=0;
        for(int i=1;i<=len;i++)
            lim|=1<<t[i]-'a';
        printf("%lld\n",f[len][lim]);
    }
    return 0;
}