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. 如何对MongoDB进行测试

    一.环境搭建 关于环境搭建,最好的搭建方式,当然是脚本一键式搭建 我这里是centos6 x64版本的linux上进行构建,这个linux版本现在应该是大部分的主流服务器的标配版本 下面是安装脚本的编 ...

  2. 提取protobuf定义文件结构

    先安装protobuf的js支持包 npm install protobufjs test.proto文件如下所示 syntax = "proto3"; package Test; ...

  3. KRPano动态热点专用素材图50多个,加动态热点使用方法

    KRPano动态热点专用素材是一种特定形式的序列图,该序列图要求帧的水平和垂直的具体位置必须准确,否则图的动作将会出现错乱,KRPano不支持动态图.目前网上比较匮乏动态热点素材,在此亲手整理制作了5 ...

  4. Lua5.3 笔记

    Lua5.3 笔记 最近用skynet,sproto通讯,完全看不懂通讯二进制是怎么写的,发现都是string这个,string那个,完全理解不来. 于是查了一下string.pack的api,和之前 ...

  5. 四千行代码写的桌面操作系统GrapeOS完整代码开源了

    简介 学习操作系统原理最好的方法是自己写一个简单的操作系统. GrapeOS是一个非常简单的x86多任务桌面操作系统,源代码只有四千行,非常适合用来学习操作系统原理. 源码地址:https://git ...

  6. @RequiredArgsConstructor和@Authwired

    我们在java后端书写接口时,对service层成员变量的注入和使用有以下两种实现方式: 1) @RequiredArgsConstructor import lombok.RequiredArgsC ...

  7. selenium库浅析

    selenium库浅析 基于4.3 pip install selenium安装好后,在sitepackages下 2个主要的目录,common和webdriver 1- common 该目录一共就一 ...

  8. Informix 4gl错误代码信息和更正

    (一)Informix信息和更正 出版日期:6 1996 年 年 1 11 月 0 成功. 操作成功.当 SQL 语句成功地执行时,数据库服务器把这个 SQLCODE 值返回给应用程序. 100 没有 ...

  9. NW排错

    fist date VM备份失败时: NW server上(linux): > nsradmin >p type : nsr recover > cd /nsr/logs >n ...

  10. Nginx惊群现象的两种解决办法

    惊群现象: 惊群现象是指由多个worker进程监听同一个Socket事件时,当事件发生时,相关的所有进程被惊醒,但最终只能有一个进程对该事件进行处理,其他进程会重新休眠,从而导致系统资源的浪费和系统性 ...