loj2340(欧拉回路+状压DP+子集卷积)

题目链接

https://loj.ac/problem/2340

题解

很容易想到一个状压,设 $d[S]$ 为 $S$ 状态的方案数

然后转移需要预处理合法的州,不存在欧拉回路或者不连通均可,其价值设为 $f[S]$ ,那么有

这是个简单的子集卷积。。由于点数是可以逐渐增加的所以可以在求完当前的卷积之后乘上系数。。




代码

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
*         ┏┓    ┏┓
*         ┏┛┗━━━━━━━┛┗━━━┓
*         ┃       ┃  
*         ┃   ━    ┃
*         ┃ >   < ┃
*         ┃       ┃
*         ┃... ⌒ ...  ┃
*         ┃ ┃
*         ┗━┓ ┏━┛
*          ┃ ┃ 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 22
#define nm 2097152
#define pi 3.1415926535897931
using namespace std;
const ll inf=998244353;
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;}
inline ll qpow(ll x,ll t){
ll s=1;
for(;t;t>>=1,x=x*x%inf)if(t&1)s=s*x%inf;
return s;
}
int n,m,p,b[NM],tot,a[NM],_x,_y,fa[NM];
ll f[NM][nm],d[NM][nm],c[nm];

void fmt(ll*a,int f=0){
if(f)f=-1;else f=1;
for(int i=0;succ(i)<tot;i++)
inc(j,0,tot-1)if(j>>i&1)
a[j]+=f*a[j^1<<i];
inc(i,0,tot-1)reduce(a[i]%=inf);
}
int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}

int main(){
n=read();m=read();p=read();
inc(i,1,m){_x=read();_y=read();b[_x]|=succ(_y-1);b[_y]|=succ(_x-1);}
inc(i,1,n)a[i]=read();
tot=succ(n);
inc(i,1,tot-1){
bool _f=false;
int pos;
inc(j,1,n)fa[j]=j;
inc(j,1,n)if(i&succ(j-1)){
pos=j;
int t=b[j]&i;
if(__builtin_popcount(t)&1)_f=true;
inc(k,1,n)if(succ(k-1)&t){
int x=find(j),y=find(k);
if(x==y)continue;
fa[x]=y;
}
c[i]+=a[j];
}
inc(j,1,n)if(i&succ(j-1))if(find(j)!=find(pos))_f=true;
if(p==0)c[i]=1;
if(p==2)c[i]=c[i]*c[i]%inf;
if(_f)f[__builtin_popcount(i)][i]=c[i];
}
inc(i,0,tot)c[i]=qpow(c[i],inf-2);
inc(i,0,n)fmt(f[i]);
d[0][0]=1;
inc(i,1,n){
fmt(d[i-1]);
inc(j,1,i)inc(k,0,tot-1)(d[i][k]+=f[j][k]*d[i-j][k])%=inf;
fmt(d[i],1);
inc(k,0,tot-1)d[i][k]=d[i][k]*c[k]%inf;
}
printf("%lld\n",d[n][tot-1]);
return 0;
}