loj152(子集卷积模板)

题目链接

https://loj.ac/problem/152

题解

做子集卷积的时候主要是对集合的交和并都有要求,所以很难找到合适的变换。。然而如果我们记 $a’_{S,i}=[|S|=i]a_S$ 的话,就能将并为空的条件去掉了。。然后剩下或的条件就直接上 $FMT$ 即可。。

然后在乘积的时候可以交换求和顺序,让内存访问连续,效果显著。。




代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
*         ┏┓    ┏┓
*         ┏┛┗━━━━━━━┛┗━━━┓
*         ┃       ┃  
*         ┃   ━    ┃
*         ┃ >   < ┃
*         ┃       ┃
*         ┃... ⌒ ...  ┃
*         ┃ ┃
*         ┗━┓ ┏━┛
*          ┃ ┃ Code is far away from bug with the animal protecting          
*          ┃ ┃ 神兽保佑,代码无bug
*          ┃ ┃           
*          ┃ ┃       
*          ┃ ┃
*          ┃ ┃           
*          ┃ ┗━━━┓
*          ┃ ┣┓
*          ┃ ┏┛
*          ┗┓┓┏━━━━━━━━┳┓┏┛
*           ┃┫┫ ┃┫┫
*           ┗┻┛ ┗┻┛
*/

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<stack>
#include<cmath>
#include<set>
#include<bitset>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 1048576
#define nm 21
#define pi 3.1415926535897931
using namespace std;
const ll inf=1e9+9;
ll read(){
ll x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return f*x;
}



inline void reduce(ll&x){x+=x>>63&inf;}

int n,m,cnt[NM];
ll a[nm][NM],b[nm][NM],c[nm][NM];

void ifmt(ll*a){
for(int i=0;succ(i)<n;i++)
inc(j,0,n-1)if(j>>i&1)
reduce(a[j]-=a[j^1<<i]);
}
void fmt(ll*a){
for(int i=0;succ(i)<n;i++)
inc(j,0,n-1)if(j>>i&1)
reduce(a[j]+=a[j^1<<i]-inf);
}


int main(){
m=read();n=1<<m;
inc(i,0,n-1)cnt[i]=__builtin_popcount(i);
inc(i,0,n-1)a[cnt[i]][i]=read();
inc(i,0,n-1)b[cnt[i]][i]=read();
inc(i,0,m)fmt(a[i]),fmt(b[i]);
inc(j,0,m)inc(k,0,j)inc(i,0,n-1)(c[j][i]+=a[k][i]*b[j-k][i])%=inf;
inc(i,0,m)ifmt(c[i]);
inc(i,0,n-1)printf("%lld%c",c[cnt[i]][i]," \n"[i==n-1]);
return 0;
}