记录编号 |
606962 |
评测结果 |
AAAAAAAAAAAAAAAAAAAA |
题目名称 |
4178.排列 |
最终得分 |
100 |
用户昵称 |
梦那边的美好TE |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
0.973 s |
提交时间 |
2025-10-04 19:45:59 |
内存使用 |
7.81 MiB |
显示代码纯文本
#include <iostream>
#include <cstdio>
using namespace std;
const int M=(1<<20);
const int mod=1e9+7;
int n,cnt[M],dp[M],a[21],S,s1,s2;
bool check(int p){
s1=0,s2=0;
for(int k=0;k<n;k++){
if((p>>k)&1)s1++;
if((S>>k)&1)s2++;
if(s2>s1)return 1;
}
return 0;
}
int main(){
freopen("changgao_perm.in","r",stdin);
freopen("changgao_perm.out","w",stdout);
scanf("%d",&n);
for(int i=0;i<n;i++)scanf("%d",a+i);
for(int i=0;i<(1<<n);i++)cnt[i]=cnt[i>>1]+(i&1);
dp[0]=1;
for(int i=1;i<=n;i++){
for(int j=0;j<n;j++)if(a[j]==i)S|=(1<<j);
for(int j=0;j<(1<<n);j++){
if(cnt[j]!=i||check(j))continue;
for(int k=0;k<n;k++){
if((j>>k)&1){
dp[j]+=dp[j^(1<<k)];
dp[j]%=mod;
}
}
}
}
printf("%d\n",dp[(1<<n)-1]);
return 0;
}