记录编号 |
606954 |
评测结果 |
AAAAAAAAAAAAAAAAAAAA |
题目名称 |
4178.排列 |
最终得分 |
100 |
用户昵称 |
梦那边的美好AC |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
0.598 s |
提交时间 |
2025-10-04 19:31:25 |
内存使用 |
5.41 MiB |
显示代码纯文本
#include <iostream>
using namespace std;
const int MOD = 1000000007;
int n, a[22], dp[1 << 21];
void check(int p){
int cnt = __builtin_popcount(p);
int lit = n - cnt + 1;
int t = 0;
for(int i = 0;i < n;i ++){
if(a[i] >= lit){
t |= (1 << i);
}
}
int c1 = 0, c2 = 0;
for(int T = 1;T <= cnt;T ++){
while(!(p & (1 << c1))) c1 ++;
while(!(t & (1 << c2))) c2 ++;
if(c1 < c2) return;
c1 ++, c2 ++;
}
for(int i = 0;i < n;i ++){
if(!(p & (1 << i))){
dp[p | (1 << i)] = (dp[p | (1 << i)] + dp[p]) % MOD;
}
}
}
int main(){
// freopen("changgao_perm.in", "r", stdin);
// freopen("changgao_perm.out", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for(int i = 0;i < n;i ++) cin >> a[i];
dp[0] = 1;
for(int p = 0;p < (1 << n);p ++){
if(dp[p]){
check(p);
}
}
cout << dp[(1 << n) - 1] << '\n';
return 0;
}