QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#350252 | #8325. 重塑时光 | lgvc | 65 | 854ms | 4252kb | C++14 | 2.9kb | 2024-03-10 16:06:05 | 2024-03-10 16:06:05 |
Judging History
answer
#include <bits/stdc++.h>
#define MOD 1000000007
int N,M,K,la[20],nii=1,ans,p[20],co[20],iv[20],v[20],aq,vv[20][20],s[20],dp[33333];
inline int pw(int a,int b) {
int as=1;
while(b) {
if(b&1) as=1ll*as*a%MOD;
a=1ll*a*a%MOD;b>>=1;
}
return as;
}
inline int ni(int a) {
return pw(a,MOD-2);
}
int t[20],vq,pa[20],qq[20][20],dg[20];
inline int fr(int x) {
while(x!=pa[x]) x=pa[x]=pa[pa[x]];
return x;
}
inline int qd(int N) {
bool zt=0;
memset(dg,0,sizeof(dg));
memset(qq,0,sizeof(qq));
for(int i=N;i>=1;i--) {
for(int j=i+1;j<=N;j++) {
if(vv[i][j]&&co[i]!=co[j]) {
qq[co[i]][co[j]]=1;
}
}
}
for(int i=1;i<=K+1;i++) for(int j=1;j<=K+1;j++) dg[j]+=qq[i][j];
std::queue<int> q;
for(int i=1;i<=K+1;i++) if(!dg[i]) q.push(i);
int zz=0;
while(!q.empty()) {
int a=q.front();q.pop();zz++;
for(int j=1;j<=N;j++) {
if(qq[a][j]) {
dg[j]--;
if(!dg[j]) q.push(j);
}
}
}
if(zz!=K+1) return 1;
return 0;
}
inline int chk(void) {
for(int i=1;i<=N;i++) pa[i]=i;
for(int i=1;i<=N;i++) t[i]=(1<<i-1);
for(int i=N;i>=1;i--) {
for(int j=i+1;j<=N;j++) {
if(vv[i][j]) {
if(co[j]==co[i]) {
if(fr(j)!=fr(i)) t[fr(i)]|=t[fr(j)],pa[fr(j)]=fr(i);
}
}
}
}
memset(v,0,sizeof(v));
for(int i=1;i<=N;i++) {
v[co[i]]++;
}
std::sort(v+1,v+K+2);
int ans=1ll*p[K+1]*nii%MOD*iv[N]%MOD*p[K]%MOD,la=1;
for(int i=1;i<=N;i++) if(pa[i]==i) ans=1ll*ans*dp[t[i]]%MOD,ans=1ll*ans*iv[__builtin_popcount(t[i])]%MOD;
for(int i=2;i<=K+1;i++) {
if(v[i]!=v[i-1]) {
la=1;
} else {
la++;
if(v[i]==0) ans=1ll*ans*iv[la]%MOD*p[la-1]%MOD;
}
}
for(int i=1;i<=K+1;i++) ans=1ll*ans*p[v[i]]%MOD;
return ans;
}
std::unordered_map<long long,int> asq;
int dfs(int n,int cl) {
if(cl>K+1) return 0;
if(qd(n-1)) return 0;
if(n==N+1) {return chk();}
long long hsh=n;
if(vq) {
int v2[20];memset(v2,0,sizeof(v2));
for(int i=1;i<n;i++) {
v2[i]=co[i];
}
std::sort(v2+1,v2+n);
for(int i=1;i<n;i++) {
hsh=(hsh*137ll+v2[i])%10000000000000007ll;
}
if(asq.find(hsh)!=asq.end()) return asq[hsh];
}
la[cl+1]=n;co[n]=cl+1;
int ans=dfs(n+1,cl+1);
for(int i=1;i<=cl;i++) {
int tp=la[i];la[i]=n;co[n]=i;ans+=dfs(n+1,cl);la[i]=tp;
ans%=MOD;
}
if(vq) asq[hsh]=ans;return ans;
}
signed main(void) {
scanf("%d %d %d",&N,&M,&K);
if(M==0) {
printf("1");
return 0;
}
bool zz=0;
for(int i=1;i<=M;i++) {
int a,b;
scanf("%d %d",&a,&b);
vv[a][b]=1;
if(a!=1) zz=1;
}
for(int i=N+1;i<=N+K;i++) nii=1ll*nii*i%MOD;
p[0]=iv[0]=1;
for(int i=1;i<=N+1;i++) p[i]=1ll*p[i-1]*i%MOD,iv[i]=ni(p[i]);
nii=ni(nii);
dp[0]=1;
for(int i=1;i<(1<<N);i++) {
for(int j=1;j<=N;j++) {
if((i>>j-1)&1) {
bool zt=0;
for(int k=j+1;k<=N;k++) {
if(((i>>k-1)&1)&&vv[j][k]) zt=1;
}
if(!zt) dp[i]=(dp[i]+dp[i-(1<<j-1)])%MOD;
}
}
}
if(zz==0&&M==N-1) {
vq=1;
}
printf("%d",dfs(1,0));
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 5
Accepted
time: 1ms
memory: 3760kb
input:
3 2 0 1 2 1 3
output:
333333336
result:
ok single line: '333333336'
Test #2:
score: 5
Accepted
time: 0ms
memory: 3808kb
input:
5 7 5 1 4 2 3 1 2 4 5 2 5 2 4 1 5
output:
895039689
result:
ok single line: '895039689'
Test #3:
score: 5
Accepted
time: 17ms
memory: 4012kb
input:
13 12 13 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13
output:
76923078
result:
ok single line: '76923078'
Test #4:
score: 5
Accepted
time: 39ms
memory: 4252kb
input:
14 13 14 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14
output:
535714290
result:
ok single line: '535714290'
Test #5:
score: 5
Accepted
time: 3ms
memory: 3912kb
input:
14 13 0 2 9 1 2 1 3 3 8 6 11 2 7 1 5 5 12 2 13 3 14 3 10 3 6 2 4
output:
700595243
result:
ok single line: '700595243'
Test #6:
score: 0
Time Limit Exceeded
input:
14 13 14 9 11 2 9 5 10 4 6 10 13 2 3 2 7 4 12 2 4 3 8 7 14 3 5 1 2
output:
result:
Test #7:
score: 5
Accepted
time: 0ms
memory: 3760kb
input:
13 0 13
output:
1
result:
ok single line: '1'
Test #8:
score: 5
Accepted
time: 27ms
memory: 3864kb
input:
14 91 14 3 4 2 10 3 10 1 13 6 8 5 6 10 13 7 8 4 9 4 7 3 7 13 14 2 12 1 3 6 9 9 14 1 10 2 9 7 11 9 11 3 12 8 10 4 13 5 9 11 12 5 14 8 12 8 13 5 13 1 5 6 11 9 13 2 5 1 14 7 14 4 14 3 5 5 11 6 12 1 2 7 10 1 4 1 8 6 10 3 8 6 13 10 14 7 12 10 11 2 13 8 11 11 13 6 7 10 12 3 14 4 5 1 6 2 14 5 10 8 14 4 8 1...
output:
4362623
result:
ok single line: '4362623'
Test #9:
score: 5
Accepted
time: 4ms
memory: 3764kb
input:
9 15 9 1 2 3 5 2 8 4 8 1 4 2 5 2 7 4 5 1 9 6 8 6 9 1 3 3 7 5 9 5 8
output:
426526937
result:
ok single line: '426526937'
Test #10:
score: 5
Accepted
time: 5ms
memory: 3716kb
input:
9 14 9 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 4 5 6 8 4 7 6 7 3 8 4 6
output:
214820829
result:
ok single line: '214820829'
Test #11:
score: 5
Accepted
time: 1ms
memory: 3904kb
input:
13 68 0 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 3 4 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 12 4 13 5 6 5 7 5 8 5 9 5 10 5 11 5 12 5 13 6 7 6 8 6 9 6 10 6 11 6 12 6 13 7 8 7 10 7 11 7 12 7 13 8 13 9 10 9 11 9 12 9 13...
output:
65784302
result:
ok single line: '65784302'
Test #12:
score: 5
Accepted
time: 854ms
memory: 3836kb
input:
13 39 13 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 3 4 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 4 8 4 9 4 10 4 12 4 13 5 6 5 7 5 10 5 11 5 13 6 7 6 11 6 13 8 9 8 12 8 13 11 13
output:
361557272
result:
ok single line: '361557272'
Test #13:
score: 0
Time Limit Exceeded
input:
14 11 14 2 13 4 11 4 14 5 14 6 9 6 14 7 11 9 14 10 12 10 14 12 14
output:
result:
Test #14:
score: 0
Time Limit Exceeded
input:
14 46 14 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 2 9 2 10 2 11 2 13 2 14 3 4 3 6 3 10 3 11 3 12 3 13 3 14 4 6 4 10 4 11 4 12 4 13 4 14 5 7 5 8 5 9 5 11 5 13 5 14 7 8 7 9 7 14 10 11 10 13 10 14 11 13 11 14 13 14
output:
result:
Test #15:
score: 5
Accepted
time: 580ms
memory: 3852kb
input:
14 70 14 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 2 3 2 4 2 5 2 7 2 8 2 9 2 10 2 11 2 12 2 13 2 14 3 11 3 13 3 14 4 5 4 7 4 8 4 10 4 11 4 12 4 13 4 14 5 7 5 8 5 10 5 12 5 13 5 14 6 7 6 8 6 9 6 10 6 11 6 12 6 13 6 14 7 8 7 10 7 12 7 13 7 14 8 10 8 12 8 13 8 14 9 11 9 12 9 13 9 14 10 1...
output:
209616080
result:
ok single line: '209616080'
Test #16:
score: 5
Accepted
time: 524ms
memory: 3860kb
input:
14 69 14 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 2 3 2 5 2 6 2 7 2 9 2 10 2 11 2 12 2 13 2 14 3 5 3 6 3 7 3 9 3 10 3 11 3 12 3 13 3 14 4 7 4 8 4 9 4 10 4 11 4 12 4 13 4 14 5 6 5 7 5 9 5 10 5 11 5 12 5 13 5 14 6 7 6 9 6 10 6 11 6 12 6 13 6 14 7 9 7 10 7 11 7 12 7 13 7 14 8 12 8 13 8 14 9 10 ...
output:
701202127
result:
ok single line: '701202127'
Test #17:
score: 0
Time Limit Exceeded
input:
14 15 14 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 4 6 5 9
output:
result:
Test #18:
score: 0
Time Limit Exceeded
input:
15 17 15 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 1 15 3 13 3 15 13 15
output:
result:
Test #19:
score: 0
Time Limit Exceeded
input:
15 17 15 1 2 1 3 1 4 1 5 1 6 1 7 1 12 1 15 7 12 7 15 8 15 9 15 10 15 11 15 12 15 13 15 14 15
output:
result:
Test #20:
score: 0
Time Limit Exceeded
input:
15 16 15 1 2 1 3 1 10 2 10 3 10 4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 12 4 13 4 14 4 15