249 字
1 分钟
题解:AT_abc199_e [ABC199E] Permutation
思路
注意到 ,考虑状压。
设 表示当前已经选了 这个集合里的数,填到了排列的前 个位置,且满足所有条件的方案数。
那么状态转移就是枚举下一个位置放哪个还没用过的数。
关键在于什么时候检查条件。题目说“前 个位置中, 的数至多 个”,所以只有当 的时候才需要检查第 条条件。
检查方法就是数一下 里编号 的位有几个,超过 就说明当前状态不合法,直接跳过不往后转移。
时间复杂度为 。
Code
#include<bits/stdc++.h>using namespace std;#define int long longint n,m,x[105],y[105],z[105],dp[1<<18];signed main() { ios::sync_with_stdio(false),cin.tie(0),cout.tie(0); cin>>n>>m; for (int i=0;i<m;i++) cin>>x[i]>>y[i]>>z[i]; dp[0]=1; for (int msk=0;msk<(1<<n);msk++) { if (dp[msk]==0) continue; int p=__builtin_popcount(msk); bool flag=true; for (int i=0;i<m;i++) if (x[i]==p) { int cnt=0; for (int j=0;j<y[i];j++) if (msk&(1<<j)) cnt++; if (cnt>z[i]) { flag=false; break; } } if (!flag) continue; for (int j=0;j<n;j++) if (!(msk&(1<<j))) dp[msk|(1<<j)]+=dp[msk]; } cout<<dp[(1<<n)-1]; return 0;}题解:AT_abc199_e [ABC199E] Permutation
https://banglee13.github.io/posts/atabc199e/分享文章
生成精美分享图或复制链接,与更多人分享本文。
继续阅读
最后更新于 ,距今已过 250 天
部分内容可能已过时