QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#480563 | #7412. Counting Cactus | strlen_s_ | WA | 1ms | 3872kb | C++14 | 2.9kb | 2024-07-16 16:36:27 | 2024-07-16 16:36:28 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=15,M=(1<<13)+5,mod=998244353,inv2=499122177;
int a[N];
int n,m,ans;
int g[M],f[M];
int col[N],b[N];
int pt[M];
int A[N][N];
int ksm(int a,int b){
int res=1;
while(b){
if(b&1)res=1ll*res*a%mod;
a=1ll*a*a%mod,b>>=1;
}
return res;
}
int det(int cnt){
int res=1;
for(int i=1;i<=cnt;i++){
int p=0;
for(int j=i;j<=cnt;j++)
if(A[j][i]){p=j;break;}
if(p==0)return 0;
if(p!=i){
res=mod-res;
for(int j=i;j<=cnt;j++)swap(A[p][j],A[i][j]);
}
res=1ll*res*A[i][i]%mod;
int inv=ksm(A[i][i],mod-2);
for(int j=1;j<=cnt;j++){
if(j==i)continue;
int xs=1ll*A[j][i]*inv%mod;
for(int k=i;k<=cnt;k++)
A[j][k]=(A[j][k]+mod-1ll*A[i][k]*xs%mod)%mod;
}
}
return res;
}
void work(int cnt){
int res=1;
for(int i=1;i<=cnt;i++)res=1ll*res*g[col[i]]%mod;
if(!res)return;
for(int i=1;i<cnt;i++){
int sd=0;
for(int j=1;j<=n;j++)if(col[i]>>(j-1)&1)sd+=pt[b[j]^(b[j]&col[i])];
A[i][i]=sd;
for(int j=i+1;j<cnt;j++){
sd=0;
for(int k=1;k<=n;k++)if(col[i]>>(k-1)&1)sd+=pt[(b[k]^(b[k]&col[i]))&col[j]];
A[i][j]=A[j][i]=(mod-sd)%mod;
}
}
ans=(ans+1ll*res*det(cnt-1)%mod)%mod;
}
void dfs(int x,int cl,int st,int cnt){
if(x>n){
if(!g[col[cl]])return;
work(cl);
return;
}
if(st>n){
if(g[col[cl]])dfs(x,cl+1,1,0);
return;
}
if(!cnt){
for(;a[st];st++);
a[st]=cl;col[cl]|=(1<<st-1);
dfs(x+1,cl,st+1,1);
a[st]=0;col[cl]^=(1<<st-1);
return;
}
if(a[st]){
dfs(x,cl,st+1,cnt);
return;
}
dfs(x,cl,st+1,cnt);
a[st]=cl;col[cl]|=(1<<st-1);
dfs(x+1,cl,st+1,cnt+1);
a[st]=0;col[cl]^=(1<<st-1);
}
int dp[M][N];
int rec[N],tot;
int calc(int s){
int res=0;tot=0;
for(int i=1;i<=n;i++)if(s>>(i-1)&1)rec[++tot]=i;
if(tot==1)return 1;
if(tot==2)return 0;
for(int i=0;i<(1<<tot);i++)
for(int j=1;j<=tot;j++)
dp[i][j]=0;
dp[1][1]=1;
for(int i=1;i<(1<<tot);i++){
for(int j=1;j<=tot;j++){
if((!(i>>(j-1)&1))||(!dp[i][j]))continue;
for(int k=1;k<=tot;k++){
if((i>>(k-1)&1)||(!(b[rec[j]]&(1<<rec[k]-1))))continue;
if((i|(1<<k-1))==(1<<tot)-1){
if(b[rec[1]]&(1<<rec[k]-1))res=(res+dp[i][j])%mod;
continue;
}
dp[i|(1<<k-1)][k]=(dp[i|(1<<k-1)][k]+dp[i][j])%mod;
}
}
}
return 1ll*res*inv2%mod;
}
signed main(){
cin>>n>>m;
for(int i=1,x,y;i<=m;i++){
cin>>x>>y;
b[x]|=(1<<y-1);
b[y]|=(1<<x-1);
}
for(int i=1;i<(1<<n);i++)g[i]=f[i]=calc(i),pt[i]=__builtin_popcount(i);
for(int i=1;i<(1<<n);i++){
int x=((1<<n)-1)^i;
if(pt[i]==1)continue;
for(int k=1;k<=n;k++){
if(!(i>>(k-1)&1))continue;
for(int j=x;j>i;j=x&(j-1))g[i|j]=(g[i|j]+1ll*g[i]*f[j|(1<<(k-1))]%mod)%mod;
}
}
dfs(1,1,1,0);
cout<<ans<<'\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3528kb
input:
3 3 1 2 2 3 3 1
output:
4
result:
ok 1 number(s): "4"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3612kb
input:
5 0
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 1ms
memory: 3540kb
input:
8 9 1 5 1 8 2 4 2 8 3 4 3 6 4 7 5 7 6 8
output:
35
result:
ok 1 number(s): "35"
Test #4:
score: 0
Accepted
time: 0ms
memory: 3648kb
input:
6 8 5 6 2 5 3 5 1 5 1 2 3 4 1 6 1 3
output:
38
result:
ok 1 number(s): "38"
Test #5:
score: 0
Accepted
time: 0ms
memory: 3648kb
input:
1 0
output:
1
result:
ok 1 number(s): "1"
Test #6:
score: 0
Accepted
time: 0ms
memory: 3576kb
input:
2 0
output:
0
result:
ok 1 number(s): "0"
Test #7:
score: 0
Accepted
time: 1ms
memory: 3640kb
input:
2 1 1 2
output:
1
result:
ok 1 number(s): "1"
Test #8:
score: 0
Accepted
time: 0ms
memory: 3576kb
input:
3 0
output:
0
result:
ok 1 number(s): "0"
Test #9:
score: 0
Accepted
time: 0ms
memory: 3588kb
input:
3 1 2 3
output:
0
result:
ok 1 number(s): "0"
Test #10:
score: 0
Accepted
time: 0ms
memory: 3612kb
input:
3 2 1 2 2 3
output:
1
result:
ok 1 number(s): "1"
Test #11:
score: 0
Accepted
time: 0ms
memory: 3576kb
input:
3 3 1 2 1 3 2 3
output:
4
result:
ok 1 number(s): "4"
Test #12:
score: 0
Accepted
time: 0ms
memory: 3592kb
input:
4 0
output:
0
result:
ok 1 number(s): "0"
Test #13:
score: 0
Accepted
time: 0ms
memory: 3636kb
input:
4 1 1 4
output:
0
result:
ok 1 number(s): "0"
Test #14:
score: 0
Accepted
time: 0ms
memory: 3628kb
input:
4 2 1 4 1 2
output:
0
result:
ok 1 number(s): "0"
Test #15:
score: 0
Accepted
time: 0ms
memory: 3632kb
input:
4 3 1 3 2 3 2 4
output:
1
result:
ok 1 number(s): "1"
Test #16:
score: 0
Accepted
time: 0ms
memory: 3644kb
input:
4 4 2 3 2 4 3 4 1 3
output:
4
result:
ok 1 number(s): "4"
Test #17:
score: 0
Accepted
time: 0ms
memory: 3632kb
input:
4 5 2 3 2 4 1 3 3 4 1 2
output:
13
result:
ok 1 number(s): "13"
Test #18:
score: 0
Accepted
time: 0ms
memory: 3648kb
input:
4 6 3 4 1 2 1 3 2 3 1 4 2 4
output:
31
result:
ok 1 number(s): "31"
Test #19:
score: 0
Accepted
time: 0ms
memory: 3804kb
input:
5 1 2 3
output:
0
result:
ok 1 number(s): "0"
Test #20:
score: 0
Accepted
time: 0ms
memory: 3636kb
input:
5 2 1 3 1 2
output:
0
result:
ok 1 number(s): "0"
Test #21:
score: 0
Accepted
time: 0ms
memory: 3844kb
input:
5 3 2 4 3 4 1 5
output:
0
result:
ok 1 number(s): "0"
Test #22:
score: 0
Accepted
time: 0ms
memory: 3576kb
input:
5 4 3 5 4 5 2 3 3 4
output:
0
result:
ok 1 number(s): "0"
Test #23:
score: 0
Accepted
time: 0ms
memory: 3636kb
input:
5 5 1 5 4 5 3 5 1 3 2 4
output:
4
result:
ok 1 number(s): "4"
Test #24:
score: 0
Accepted
time: 0ms
memory: 3872kb
input:
5 6 1 5 4 5 1 4 2 3 1 3 3 5
output:
13
result:
ok 1 number(s): "13"
Test #25:
score: 0
Accepted
time: 0ms
memory: 3580kb
input:
5 7 4 5 2 4 2 3 1 5 1 3 1 2 2 5
output:
41
result:
ok 1 number(s): "41"
Test #26:
score: -100
Wrong Answer
time: 0ms
memory: 3640kb
input:
5 8 4 5 2 3 3 5 1 2 2 4 2 5 1 4 1 5
output:
89
result:
wrong answer 1st numbers differ - expected: '90', found: '89'