[ABC309G] Ban Permutation
Problem Statement
Find the number, modulo $998244353$, of permutations $P=(P_1,P_2,\dots,P_N)$ of $(1,2,\dots,N)$ such that:
- $|P_i - i| \ge X$ for all integers $i$ with $1 \le i \le N$.
Constraints
- $1 \le N \le 100$
- $1 \le X \le 5$
- All input values are integers.
$X\le 5$,考虑状压。
但是 \(p_i-i\ge X\) ? 考虑容斥。
定义 \(dp_{i,j,s}\) 为目前选的集合为 \(s\),选到第 \(i\) 个数,目前有 \(j\) 个不满足要求。
考虑这个是否满足要求,如果不满足,那么记到状压里面,否则就先放着不管
在最后统计答案的时候, \(dp_{n,j,s}\) 中还有 \(n-j\) 个数没有安排好,乘以个 \((n-j)!\),同时这是一个二项式反演一样的容斥,所以乘上系数 \(C_{n,j}\times (-1)^j\)
#include<bits/stdc++.h>
using namespace std;
const int N=105,S=1025,P=998244353;
int dp[N][S][N],n,m,x,ans,f[N];
int main()
{
scanf("%d%d",&n,&x),--x;
m=x<<1|1;
dp[0][0][0]=1;
for(int i=f[0]=1;i<N;i++)
f[i]=1LL*f[i-1]*i%P;
for(int i=1;i<=n;i++)
{
for(int s=0;s<(1<<m);s++)
{
for(int j=0;j<=i;j++)
{
if(!(s>>(m-1)&1))
dp[i][s][j]=(dp[i-1][s<<1|1][j]+dp[i-1][s<<1][j])%P;
if(j)
{
for(int l=max(1-i,-x);l<=min(x,n-i);l++)
{
if(!(s>>l+x&1))
continue;
int ps=s^(1<<l+x);
if(!(ps>>(m-1)&1))
(dp[i][s][j]+=(dp[i-1][ps<<1|1][j-1]+dp[i-1][ps<<1][j-1])%P)%=P;
}
}
}
}
}
for(int i=0;i<(1<<m);i++)
for(int j=0;j<=n;j++)
(ans+=(j&1? P-1LL:1LL)*dp[n][i][j]%P*f[n-j]%P)%=P;
printf("%d",ans);
return 0;
}
[ABC309G] Ban Permutation的更多相关文章
- Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- [LeetCode] Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- Leetcode 60. Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- UVA11525 Permutation[康托展开 树状数组求第k小值]
UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ...
- Permutation test: p, CI, CI of P 置换检验相关统计量的计算
For research purpose, I've read a lot materials on permutation test issue. Here is a summary. Should ...
- Permutation
(M) Permutations (M) Permutations II (M) Permutation Sequence (M) Palindrome Permutation II
- Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
随机推荐
- Vuetify使用本地图标资源
Vuetify使用本地图标资源 修改/public/index.html 删除下面两行引用 <link rel="stylesheet" href="https:/ ...
- 给DataTable添加额外字段
//dt为DataTable dt.Columns.Add("字段名");//创建字段 //给新增字段赋值 foreach(DataRow item in dt.Rows) { i ...
- wget 命令的使用:HTTP文件下载、FTP文件下载--九五小庞
1. wget 命令简介与安装wget是用于在命令行终端下载网络文件的开源免费的命令工具,支持 HTTP/HTTPS.FTP/FTPS 协议的下载.wget 与 curl 相似,curl 可以理解为是 ...
- linux shell根据关键字用sed注释掉整行
一.将带有ab的行注释掉 # cat test # sed -i '/ab/s/^\(.*\)$/#\1/g' test ab是关键字 s是语法替换 ^是行首 $是行尾 \是转义符 数字1带表前述匹配 ...
- 【matplotlib基础】--结合地图
如果分析的数据与地域相关,那么,把分析结果结合地图一起展示的话,会让可视化的效果得到极大的提升. 比如,分析各省GDP数据,人口数据,用柱状图,饼图之类的虽然都可以展示分析结果,不过,如果能在全国的地 ...
- Java 多线程线程池的工作流程
1.在创建了线程池后,等待提交过来的任务请求. 2.当调用execute()方法添加一个请求任务时,线程池就会做如下判断: 2.1 如果正在运行的线程数量小于corePoolSize,那么马上创建线程 ...
- oracle 12C提示:ORA-28001口令已经失效
oracle 12C 提示口令已经失效,此用户是pdb用户,解决办法:1 系统管理员身份登陆 sqlplus / as sysdba 2 转到对应的pdb容器中 alter session set c ...
- 洛谷P2433 小学数学 N 合一
写完了这道题结果脑子断电把浏览器关了......打开一看 没保存 寄 传送门:[深基1-2]小学数学 N 合一 - 洛谷 第一题 第二题 第三题 这几道题没啥好说的,直接输出就彳亍了 cout < ...
- 【sqli-labs】学习--待续
预备知识: 数字型注入: 这种sql语句中处理的是整型,不需要使用单引号来闭合变量的值. 首先输入id=1',此时因为不是整型,sql语句会执行出错,抛出异常. 然后输入id=1 and 1=1,此时 ...
- 互联网那些技术 | 扒一扒互联网Markdown的那些事儿
最近感觉到 Markdown 似乎已成为各大社区的编辑器标配所支持的格式,侧面看来其设计之初的目标 " to be used as a format for writing for the ...