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的更多相关文章

  1. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  2. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  3. [LeetCode] Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

  4. [LeetCode] Permutation Sequence 序列排序

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  5. [LeetCode] Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  6. 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 ...

  7. UVA11525 Permutation[康托展开 树状数组求第k小值]

    UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ...

  8. 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 ...

  9. Permutation

    (M) Permutations (M) Permutations II (M) Permutation Sequence (M) Palindrome Permutation II

  10. Next Permutation

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

随机推荐

  1. 「AntV」x6 框选添加右键菜单

    今天在群里有个小伙伴提出了这么个问题:如何在框选完成后给框选的区域添加一个右键菜单的功能,我看到了这个问题后也是有点懵,心里想着怎么还有这个需求,直接快捷键删除不是更好吗,谁知这位小伙伴也是这么写的, ...

  2. 《Web安全基础》03. SQL 注入

    @ 目录 1:简要 SQL 注入 2:MySQL 注入 2.1:信息获取 2.2:跨库攻击 2.3:文件读写 2.4:常见防护 3:注入方法 3.1:类型方法明确 3.2:盲注 3.3:编码 3.4: ...

  3. 《小白WEB安全入门》03. 漏洞篇

    @ 目录 SQL注入和简单绕过原理 什么是SQL 什么是SQL注入 XSS漏洞原理 什么是XSS XSS分类 NOSQL注入 什么是NOSQL CSRF原理 什么是CSRF 网络摄像头入侵原理 什么是 ...

  4. Flink-读Kafka写Hive表

    1. 目标 使用Flink读取Kafka数据并实时写入Hive表. 2. 环境配置 EMR环境:Hadoop 3.3.3, Hive 3.1.3, Flink 1.16.0   根据官网描述: htt ...

  5. Python 有趣的模块之pynupt——通过pynput控制鼠标和键盘

    写在前面 Python中有许多有趣和强大的模块,其中一个非常有趣的模块就是pynupt.pynupt是基于pynput模块的一个封装,用于控制鼠标和键盘.它可以实现自动化操作和游戏外挂等功能. 本文将 ...

  6. orale命令6 rman备份

    RMAN:使用ramn进行备份和恢复,rman不依赖系统操作命令,在数据块级别做备份.块级别备份,能只备份变化后的块,实现增量备份.而且不会备份空的块.好处:1.能实现增量备份2.只备份有数据的块,不 ...

  7. Maximum Diameter 题解

    Maximum Diameter 题目大意 定义长度为 \(n\) 的序列 \(a\) 的权值为: 所有的 \(n\) 个点的第 \(i\) 个点的度数为 \(a_i\) 的树的直径最大值,如果不存在 ...

  8. 从0到1实现 OpenTiny 组件库跨框架技术

    本文分享自华为云社区<从0到1实现 OpenTiny 组件库跨框架技术>,作者:华为云社区精选 . 在华为云<DTSE Tech Talk>技术直播第44期<0基础玩转 ...

  9. 一键修改IP多实例

    一键修改IP多实例-v1 #!/bin/bash #2020-03-01 #auto change ip #By Flyaway ###################### grep "d ...

  10. GameFramework摘录 - 1. ReferencePool

    GameFramework是一个结构很优秀的Unity游戏框架,但意图似乎在构建可跨引擎的框架?对要求不高的小型个人(不专业)开发来说有些设计过度了,但其中的设计精华很值得学习. 首先来说一下其中的R ...