ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#203980 | #3572. 排列数数 | smallstone | 100 | 1712ms | 66796kb | C++11 | 1.2kb | 2024-03-31 11:00:43 | 2024-03-31 12:02:18 |
answer
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
//using LL = __int128
const int maxn = (1 << 23) + 5;
#define mod 1000000007ll
ll n, m, p[30], p1[30];
ll f[maxn];
vector<int> g[30];
bool vis[30][30];
void add(int u, int v){
if(vis[v][u]){
cout << 0;
exit(0);
}
if(!vis[u][v])
g[u].push_back(v), vis[u][v] = true;
}
bool check(){
for(int i = 1 ; i <= n ; i++){
for(auto v : g[i])
if(p[i] >= p[v])
return false;
}
return true;
}
int main(){
//cout << sizeof(f);
cin >> n >> m;
for(int i = 1 ; i <= m ; i++){
int u, v;
cin >> u >> v;
add(u, v);
p1[u] |= (1 << v);
}
if(n <= 10){
for(int i = 1 ; i <= n ; i++)
p[i] = i;
ll ans = 0;
do{
ans += check();
}while(next_permutation(p + 1, p + 1 + n));
cout << ans;
exit(0);
}
f[0] = 1;
for(int s = 0 ; s < (1 << (n + 1)) ; s += 2){
//if(s == 2)
//cout << (!(s >> (1 - 1)) & 1);
for(int i = 1 ; i <= n ; i++)
if(((p1[i] & s) == p1[i]) && (!((s >> i) & 1))){
f[s | (1 << i)] = (f[s | (1 << i)] + f[s]) % mod;
//cout << i << ":" << f[s] << " " << s << " " << f[s | (1 << (i - 1))] << " " << (s | (1 << (i - 1))) << "\n";
}
}
cout << f[(1 << (n + 1)) - 2] << "\n";
return 0;
}
详细
小提示:点击横条可展开更详细的信息
Test #1:
score: 10
Accepted
time: 32ms
memory: 1260kb
input:
10 20 2 6 1 7 1 8 4 7 8 7 4 7 7 5 3 5 3 1 6 7 2 10 10 5 9 8 8 6 8 7 6 10 7 5 3 10 4 10 8 7
output:
210
result:
ok single line: '210'
Test #2:
score: 10
Accepted
time: 50ms
memory: 1264kb
input:
10 50 8 7 9 7 9 6 9 10 7 5 6 2 3 1 8 5 2 5 9 10 6 2 4 7 8 5 7 2 10 7 9 8 9 7 3 2 6 2 1 4 8 4 7 2 9 5...
output:
9
result:
ok single line: '9'
Test #3:
score: 10
Accepted
time: 6ms
memory: 1256kb
input:
9 3 6 7 6 5 9 1
output:
60480
result:
ok single line: '60480'
Test #4:
score: 10
Accepted
time: 47ms
memory: 1260kb
input:
10 400 7 6 4 2 6 3 6 3 4 2 1 8 10 4 1 10 10 6 4 2 10 9 10 8 10 4 5 4 1 2 9 3 5 4 4 6 10 3 10 2 8 4 9...
output:
1
result:
ok single line: '1'
Test #5:
score: 10
Accepted
time: 44ms
memory: 1264kb
input:
10 10 2 9 5 10 4 10 9 8 3 6 1 2 5 3 3 2 3 8 3 6
output:
4470
result:
ok single line: '4470'
Test #6:
score: 10
Accepted
time: 528ms
memory: 66776kb
input:
22 3 6 11 1 12 17 8
output:
700330084
result:
ok single line: '700330084'
Test #7:
score: 10
Accepted
time: 122ms
memory: 66792kb
input:
22 372 11 2 11 4 15 9 21 19 21 12 19 6 9 8 21 20 4 10 5 11 5 4 6 10 20 6 22 5 3 20 1 9 3 19 11 6 16 ...
output:
108
result:
ok single line: '108'
Test #8:
score: 10
Accepted
time: 322ms
memory: 66792kb
input:
22 40 13 17 8 2 15 1 8 18 19 13 16 20 16 15 5 6 21 14 21 13 11 3 5 8 10 9 11 1 8 17 12 21 11 4 1 21 ...
output:
291292779
result:
ok single line: '291292779'
Test #9:
score: 10
Accepted
time: 206ms
memory: 61804kb
input:
22 100 1 22 13 15 1 9 14 16 12 21 6 2 4 15 2 15 10 15 20 6 6 5 8 3 2 15 16 15 9 21 11 2 8 15 13 16 1...
output:
13220416
result:
ok single line: '13220416'
Test #10:
score: 10
Accepted
time: 355ms
memory: 66796kb
input:
22 30 4 16 1 16 18 1 3 2 5 10 21 16 4 16 11 8 8 7 20 21 19 7 6 21 19 7 1 19 5 19 21 11 10 11 10 8 19...
output:
231868313
result:
ok single line: '231868313'
Extra Test:
score: 0
Extra Test Passed