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. 深入了解API接口技术及其应用

    尊敬的读者们,大家好!在互联网时代,API(Application Programming Interface)接口已经成为开发者们连接各种应用.获取数据的重要工具.今天,我们将深入探讨API接口技术 ...

  2. RK3568开发笔记(七):在宿主机ubuntu上搭建Qt交叉编译开发环境,编译一个Demo,目标板运行Demo测试

    前言   在之前的博文中已经搭建好了一个比较完善的ubuntu宿主机,都很完善了但是发现没有Qt交叉编译开发环境,所以还需要搭建一套Qt交叉编译开发环境.   补充说明   本篇是基于<RK35 ...

  3. Springboot多种字段copy工具比较

    结论:推荐使用spring自带的copy工具,不能copy的手动set 1.springboot自带的BeanUtils.copyProperties package com.admin; impor ...

  4. MySQL实战实战系列 04 深入浅出索引(上)

    提到数据库索引,我想你并不陌生,在日常工作中会经常接触到.比如某一个 SQL 查询比较慢,分析完原因之后,你可能就会说"给某个字段加个索引吧"之类的解决方案.但到底什么是索引,索引 ...

  5. Solution -「THUPC 2019」Duckchess

    Description Link. 大模拟是不可能给你概括题意的. Solution (据说鸭棋题解用这个标题很吉利)(这里是被点名批评的 长度 19k 的打法)(先说好代码里 Chinglish 满 ...

  6. 常用设计模式(Java)

    目录 设计模式引入 1. 什么是设计模式 2. 学习设计模式的意义 3. 设计模式的基本要素 4. OOP七大原则 1.单例模式 1. 饿汉式单例 2. 懒汉式单例 3. 内部类实现单例 4. 反射会 ...

  7. C中code关键字

    单片机C语言code是什么作用? code的作用是告诉单片机,我定义的数据要存储在ROM(程序存储区)里面,写入后就不能再更改,其实是相当与汇编里面的寻址MOVC(好像是),因为C语言中没办法详细描述 ...

  8. 第一次git上传的完整流程

    第一次git上传的完整流程 使用git简单命令上传代码push到远程仓库 + 简单介绍了一个.git文件结构. 代码上传到gitee和github流程一样的,不过你上传到github可能网不行失败,所 ...

  9. 使用 TensorFlow 进行机器学习

    使用 TensorFlow 进行机器学习 这是使用 TensorFlow 进行机器学习的官方代码存储库. 使用 TensorFlow(Google 最新.最好的机器学习库)开始进行机器学习. 概括 第 ...

  10. 数字时代的自我呈现:探索个人形象打造的创新工具——FaceChain深度学习模型工具

    数字时代的自我呈现:探索个人形象打造的创新工具--FaceChain深度学习模型工具 1.介绍 FaceChain是一个可以用来打造个人数字形象的深度学习模型工具.用户仅需要提供最低一张照片即可获得独 ...