子集DP(SOS DP)
TallBanana · · 算法·理论
板子
如果想求
怎么办?其中
solution 1
根据定义,有朴素
for(int state=0;state<=T;state++)
for(int i=0;i<=state;i++)
if((state&i)==i) f[state]+=f[i];
solution 2
如果
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
对于
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 超级好伙伴
有
对于学生
如果学生
问题是: 对于任意的
input:
第一行,一个整数
第二行,
output:
共
example1:
input:
2
90 36
ouput:
36 90
example2:
input:
4
3 6 3 6
output:
-1 -1 -1 -1
solution of T1
对于
#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 箱子
有
你需要选出若干个箱子使得选中的箱子中包括所有种类的玩具,答案对
input:
第一行包含两个整数
接下来的
output:
输出方案数对
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
直接算很难,考虑容斥。令
对于第
i(0\leq i< m) 位,为1 即不管;为0 则要求这一位为0 。
的数的个数。
容易发现,满足这样的数一定时
#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 回文子串
给出一个长度为
字符串S的每一个字符都是小写字母,而且是字母表的前17个小写字母,即 a 至 q。
注意:就算内容一样,但是位置不一样的子串也被认为是不同的子串,例如: aba 有4个不同的回文子串:a、b、a、aba。
input:
第一行,一个整数
第二行,一个长度为
第三行,一个整数
接下来有
其中
注意:每个询问独立,并不是真正去替换
output:
共
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
对每一个字串进行考虑贡献,同时其外的 ? 可以是
枚举回文中心,并考虑每次在两边拓展时对答案的贡献。令当前贡献为
case1: 加入字串的是一样的字母,例如 a 和 a,
case2: 加入字串的是不一样的字母,例如 a 和 b,
case3: 加入字串的的是一个问号和一个字母,例如 ? 和 a,a。记
case4: 加入字串的是两个 ?,则
对于每一次拓展,都将当前的状态对
#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;
}