[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 ...
随机推荐
- Web安全漏洞解决方案
1.已解密的登录请求 推理: AppScan 识别了不是通过 SSL 发送的登录请求. 测试请求和响应: 1.1.1 产生的原因 登录接口,前端传入的密码参数没有经过md5的加密就直接传给了后端 1. ...
- ZooKeeper核心概念和架构
ZooKeeper简介 ZooKeeper是一个分布式应用程序协调服务,主要用于解决分布式集群中应用系统的一致性问题.它能提供类似文件系统的目录节点树方式的数据存储,主要用途是维护和监控所存数据的状态 ...
- Longest Divisors Interval
Smiling & Weeping ----总有一个人, 一直住在心底, 却消失在生活里. Given a positive integer n, find the maximum size ...
- 探索 Java 线程的创建
by emanjusaka from https://www.emanjusaka.top/archives/7 彼岸花开可奈何 本文欢迎分享与聚合,全文转载请留下原文地址. 前言 在并发编程中我 ...
- 正则表达式快速入门二 :python re module 常用API介绍
python regex module re 使用 reference regex module in python import re re.search re.search(regex, subj ...
- apollo多环境部署
一.环境准备 jdk : 1.8+ mysql 5.6.5+ 二.安装包下载 https://github.com/ctripcorp/apollo/releases 下载如下三个压 ...
- MPI转以太网Plus模块Modbus连接两台变频器通信案例
MPI转以太网Plus模块Modbus主站连接两台变频器通信案例 MPI转以太网Plus模块连接200PLC无需编程实现Modbus主从站功能与2台变频器modbus通信:以下就是MPI转以太网模块作 ...
- 利用python将数据写入CSV文件中
利用python将数据写入CSV文件中 全部代码如下: import csv # 1.创建文件对象 f = open('cav_file.csv', 'w', encoding='utf-8', ne ...
- Oracle:查询表的统计信息,手动收集统计信息
在Oracle中,存在执行计划不准的情况,怀疑表的统计信息是否收集,需要以下操作:select table_name,num_rows,blocks,last_analyzed from user_t ...
- Python 列表操作指南3
示例,将新列表中的所有值设置为 'hello': newlist = ['hello' for x in fruits] 表达式还可以包含条件,不像筛选器那样,而是作为操纵结果的一种方式: 示例,返回 ...