QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#846970 | #7. 主旋律 | QZJ123456 | 100 ✓ | 478ms | 13696kb | C++14 | 2.0kb | 2025-01-07 16:34:43 | 2025-01-07 16:34:44 |
Judging History
answer
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const ll mod=1e9+7;
int n,m,e[1<<15|5][17],ct[1<<15];
ll f[1<<15|5][17],tmp[17],pw[505],fac[25],ifac[25],h[1<<15|5],g[1<<15|5];
ll ksm(ll a,ll m,ll p){
ll ans=1;
while(m){
if(m&1)ans=ans*a%mod;
a=a*a%mod;
m>>=1;
}
return ans;
}
ll Mod(ll v){
return v-=v>mod?mod:0;
}
typedef unsigned long long ull;
typedef __uint128_t L;
struct FastMod {
ull b, m;
FastMod(ull b) : b(b), m(ull((L(1) << 64) / b)) {}
ull r(ull a) {
ull q = (ull)((L(m) * a) >> 64);
ull r = a - q * b; // can be proven that 0 <= r < 2*b
return r >= b ? r - b : r;
}
};
FastMod F(2);
int popc[1<<15|5];
vector<int>vec[1<<15|5];
int main(){
F=FastMod(mod);
scanf("%d%d",&n,&m);
pw[0]=1;
for(int i=1;i<=m;i++)pw[i]=pw[i-1]*2%mod;
fac[0]=1;
for(int i=1;i<=n;i++)fac[i]=fac[i-1]*i%mod;
for(int i=0;i<=n;i++)ifac[i]=ksm(fac[i],mod-2,mod);
for(int s=1;s<(1<<n);s++)popc[s]=popc[s>>1]+(s&1);
for(int s=1;s<(1<<n);s++){
for(int i=1;i<=n;i++){
if((s>>(i-1))&1)vec[s].push_back(i);
}
}
while(m--){
int x,y;
scanf("%d%d",&x,&y);
e[1<<(y-1)][x]++;
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
for(int s=1;s<(1<<n);s++){
if((s>>(j-1))&1)e[s][i]+=e[s^(1<<(j-1))][i];
}
}
}
f[0][0]=g[0]=1;
for(int s=1;s<(1<<n);s++){
for(int s1=s;s1;s1=(s1-1)&s){
for(int i=0;i<=popc[s^s1];i++){
f[s][i+1]=F.r(f[s][i+1]+f[s^s1][i]*g[s1]);
}
}
memset(tmp,0,sizeof tmp);
for(int s1=s;s1;s1=(s1-1)&s){
int u=0;
for(auto i:vec[s1])u+=e[s^s1][i];
for(int i=1;i<=popc[s1];i++){
if(!f[s1][i])continue;
tmp[i]=F.r(tmp[i]+f[s1][i]*pw[u+ct[s^s1]]);
}
}
for(int i=1;i<=n;i++){
if(i&1)g[s]=(g[s]+tmp[i]*ifac[i])%mod;
else g[s]=(g[s]-tmp[i]*ifac[i]%mod+mod)%mod;
}
for(int i=1;i<=n;i++){
if((s>>(i-1))&1)ct[s]+=e[s][i];
}
h[s]=g[s];
g[s]=(pw[ct[s]]-g[s]+mod)%mod;
f[s][1]=g[s];
}
cout<<g[(1<<n)-1];
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Pretests
Final Tests
Test #1:
score: 10
Accepted
time: 1ms
memory: 6536kb
input:
5 15 4 3 4 2 2 5 2 1 1 2 5 1 3 2 4 1 1 4 5 4 3 4 5 3 2 3 1 5 3 1
output:
9390
result:
ok single line: '9390'
Test #2:
score: 10
Accepted
time: 0ms
memory: 8112kb
input:
5 18 4 3 4 2 2 5 2 1 1 2 5 1 3 2 4 1 1 4 5 4 3 4 5 3 2 3 1 5 3 1 1 3 5 2 2 4
output:
100460
result:
ok single line: '100460'
Test #3:
score: 10
Accepted
time: 1ms
memory: 8632kb
input:
8 35 5 1 8 7 7 8 7 6 6 1 2 5 6 5 8 2 7 2 7 5 3 1 6 3 2 3 5 2 8 5 8 3 6 8 2 1 1 6 2 6 7 3 2 4 3 5 3 2 3 7 7 1 8 4 3 4 3 6 6 4 2 7 4 6 6 7 7 4 8 1
output:
299463717
result:
ok single line: '299463717'
Test #4:
score: 10
Accepted
time: 1ms
memory: 8344kb
input:
8 40 5 1 8 7 7 8 7 6 6 1 2 5 6 5 8 2 7 2 7 5 3 1 6 3 2 3 5 2 8 5 8 3 6 8 2 1 1 6 2 6 7 3 2 4 3 5 3 2 3 7 7 1 8 4 3 4 3 6 6 4 2 7 4 6 6 7 7 4 8 1 1 2 4 8 5 8 4 3 5 7
output:
21156439
result:
ok single line: '21156439'
Test #5:
score: 10
Accepted
time: 1ms
memory: 8632kb
input:
8 45 5 1 8 7 7 8 7 6 6 1 2 5 6 5 8 2 7 2 7 5 3 1 6 3 2 3 5 2 8 5 8 3 6 8 2 1 1 6 2 6 7 3 2 4 3 5 3 2 3 7 7 1 8 4 3 4 3 6 6 4 2 7 4 6 6 7 7 4 8 1 1 2 4 8 5 8 4 3 5 7 2 8 1 5 3 8 1 3 4 1
output:
426670664
result:
ok single line: '426670664'
Test #6:
score: 10
Accepted
time: 2ms
memory: 7740kb
input:
10 65 5 10 1 8 7 8 6 2 5 7 9 2 4 7 3 7 1 6 3 10 7 9 8 4 7 1 5 2 1 7 4 2 8 3 8 1 3 9 8 2 2 10 4 3 9 10 5 3 3 8 3 4 6 10 4 8 4 5 5 8 9 5 9 6 10 2 10 5 6 1 2 1 9 4 7 10 5 6 10 7 10 8 5 9 9 7 9 8 4 10 8 9 7 2 2 7 10 1 7 3 6 8 7 6 9 1 6 5 2 4 6 3 2 9 8 10 10 9 8 5 4 1 6 9 2 3 1 3 1 9
output:
931896041
result:
ok single line: '931896041'
Test #7:
score: 10
Accepted
time: 0ms
memory: 8496kb
input:
10 70 5 10 1 8 7 8 6 2 5 7 9 2 4 7 3 7 1 6 3 10 7 9 8 4 7 1 5 2 1 7 4 2 8 3 8 1 3 9 8 2 2 10 4 3 9 10 5 3 3 8 3 4 6 10 4 8 4 5 5 8 9 5 9 6 10 2 10 5 6 1 2 1 9 4 7 10 5 6 10 7 10 8 5 9 9 7 9 8 4 10 8 9 7 2 2 7 10 1 7 3 6 8 7 6 9 1 6 5 2 4 6 3 2 9 8 10 10 9 8 5 4 1 6 9 2 3 1 3 1 9 5 4 1 5 5 1 10 4 10 6
output:
303656759
result:
ok single line: '303656759'
Test #8:
score: 10
Accepted
time: 478ms
memory: 13504kb
input:
15 130 7 10 9 12 4 6 1 10 14 9 4 8 8 9 4 3 15 9 3 9 1 8 2 15 8 4 13 7 3 5 14 13 6 2 14 6 8 3 4 2 8 13 9 2 6 13 12 11 6 4 11 8 15 5 3 8 10 8 15 7 15 6 12 15 8 12 13 9 12 9 8 15 11 6 6 7 10 4 2 8 11 12 7 9 7 12 14 1 5 8 10 9 3 7 7 13 11 9 11 10 1 5 1 3 2 1 2 7 10 1 10 15 7 14 5 6 6 1 15 10 5 15 15 8 5...
output:
717458968
result:
ok single line: '717458968'
Test #9:
score: 10
Accepted
time: 460ms
memory: 13696kb
input:
15 140 7 10 9 12 4 6 1 10 14 9 4 8 8 9 4 3 15 9 3 9 1 8 2 15 8 4 13 7 3 5 14 13 6 2 14 6 8 3 4 2 8 13 9 2 6 13 12 11 6 4 11 8 15 5 3 8 10 8 15 7 15 6 12 15 8 12 13 9 12 9 8 15 11 6 6 7 10 4 2 8 11 12 7 9 7 12 14 1 5 8 10 9 3 7 7 13 11 9 11 10 1 5 1 3 2 1 2 7 10 1 10 15 7 14 5 6 6 1 15 10 5 15 15 8 5...
output:
459157220
result:
ok single line: '459157220'
Test #10:
score: 10
Accepted
time: 463ms
memory: 13500kb
input:
15 150 7 10 9 12 4 6 1 10 14 9 4 8 8 9 4 3 15 9 3 9 1 8 2 15 8 4 13 7 3 5 14 13 6 2 14 6 8 3 4 2 8 13 9 2 6 13 12 11 6 4 11 8 15 5 3 8 10 8 15 7 15 6 12 15 8 12 13 9 12 9 8 15 11 6 6 7 10 4 2 8 11 12 7 9 7 12 14 1 5 8 10 9 3 7 7 13 11 9 11 10 1 5 1 3 2 1 2 7 10 1 10 15 7 14 5 6 6 1 15 10 5 15 15 8 5...
output:
663282473
result:
ok single line: '663282473'