板子
如果想求
怎么办?其中 。
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
对于 的子集,即每一个 的位置可以选或不选,即可以对逐位dp。时间复杂度 。
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 超级好伙伴
有 个学生,编号 至 ,第i个学生的品质是 。
对于学生 和学生 来说,如果 等于0(其中 就是二进制位操作的“与”),那么我们称 和 是”好伙伴”。
如果学生 有多个”好伙伴“,那么品质最好的那个就是 的”超级好伙伴”。
问题是: 对于任意的 ,输出第 个学生的“超级好伙伴”的品质,如果第 个学生没有“好伙伴”输出 。
input:
第一行,一个整数 。。
第二行, 个整数,第 个整数是 , .
output:
共 行,每行一个整数。
example1:
input:
2
90 36
ouput:
36 90example2:
input:
4
3 6 3 6
output:
-1 -1 -1 -1solution of T1
对于 ,有 。子集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 箱子
有 个箱子,每个箱子里有 种玩具,每种玩具以 的数编号。具体来说,第 个箱子里有 个玩具。
你需要选出若干个箱子使得选中的箱子中包括所有种类的玩具,答案对 取模。
input:
第一行包含两个整数 。
接下来的 行,第 行第一个整数 ,紧接着 个正整数,表示第 个箱子里的玩具情况。
output:
输出方案数对 取模后的结果。
example1:
input:
3 3
3 1 2 3
3 1 2 3
3 1 2 3
output:
7example2:
input:
3 3
1 1
1 2
1 3
output:
1example3:
input:
4 5
2 2 3
2 1 2
4 1 2 3 5
4 1 2 4 5
output:
6solution of T2
直接算很难,考虑容斥。令 为满足
对于第 位,为 即不管;为 则要求这一位为 。
的数的个数。
容易发现,满足这样的数一定时 的子集。子集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 回文子串
给出一个长度为 的字符串 ,求 有多少个不同的子串是回文串。
字符串S的每一个字符都是小写字母,而且是字母表的前17个小写字母,即 a 至 q。
注意:就算内容一样,但是位置不一样的子串也被认为是不同的子串,例如: aba 有4个不同的回文子串:a、b、a、aba。
还可能包含?字符。
再给出一个候选串 , 是只包含字母表的前 个小写字母,且 没有重复字符。
对于字符串 的每一个?, 你都可以用候选串T的任意一个字符去替换,显然,这样就能生成很多个不一样的 。
任务是:求所有的S的所有回文子串的数量,答案模。
例如: ab??,ab。
那么可以生成4个不同的S,分别是:abaa,abab, abba, abbb
其中 有 个回文子串, 有 个回文子串, 有 个回文子串, 有 个回文子串。
所以 答案
input:
第一行,一个整数 。 。
第二行,一个长度为 的字符串 。
第三行,一个整数 ,表示有 次询问,。
接下来有 行,每 行是一个候选串 ,表示如果在候选串 的支持下,能够生成的所有字符串S的所有回文子串数量。
其中 的每个字符都是字母表的前 个小写字母,且没有重复字符。
注意:每个询问独立,并不是真正去替换 ,只是为了计算结果而已,即:每次询问的S都是一样的,都是从输入数据第二行读入的那个字符串 。
output:
共 行,每行一个整数。
example:
intput:
7
ab??aba
8
a
b
ab
abc
abcd
abcde
abcdef
abcdefg
output:
14
13
55
105
171
253
351
465solution of T3
对每一个字串进行考虑贡献,同时其外的 ? 可以是 中的每一个字符,所以考虑当 的长度为 时,字串的贡献。
枚举回文中心,并考虑每次在两边拓展时对答案的贡献。令当前贡献为 ,上一次的贡献为 。
case1: 加入字串的是一样的字母,例如 a 和 a,。
case2: 加入字串的是不一样的字母,例如 a 和 b,。
case3: 加入字串的的是一个问号和一个字母,例如 ? 和 a,,但此时有一点小不同,因为有贡献当且仅当 中包含 a。记 为当前 的限制,以二进制存储。
case4: 加入字串的是两个 ?,则 。
对于每一次拓展,都将当前的状态对 加入贡献。当询问时,对 做子集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;
}