题目

刷水题涨信心

显然这是个广义容斥,我们现在算一下至少有\(i\)个完美数的方案数就好了

这\(1000\)的数据范围显然在暗示\(n^2\)的dp

我们注意到这个条件大概就是\(P_i=i-1\)或\(P_i=i+1\),于是我们可以想象成左右两边各\(n\)个点去完成一个一一匹配

设\(dp[i][j][k][p]\)表示左边第\(i\)个数已经匹配完了,一共形成了\(j\)对完美数,\(k\)表示右边对应的第\(i\)个位置的使用状态\(0/1\),\(p\)表示右边第\(i+1\)个数的使用状态

转移显然

代码

#include<cstdio>
#define re register
const int mod=1e9+7;
const int maxn=1005;
int n,m,tot;
int dp[maxn][maxn][2][2],ans[maxn];
int c[maxn][maxn],fac[maxn];
int main() {
scanf("%d%d",&n,&m);fac[0]=1;
for(re int i=0;i<=n;i++) c[i][0]=c[i][i]=1;
for(re int i=1;i<=n;i++) fac[i]=1ll*fac[i-1]*i%mod;
for(re int i=2;i<=n;i++)
for(re int j=1;j<i;j++) c[i][j]=(c[i-1][j-1]+c[i-1][j])%mod;
dp[1][0][0][0]=1,dp[1][1][0][1]=1;
for(re int i=1;i<n;i++)
for(re int j=0;j<=i;j++)
for(re int k=0;k<2;k++)
for(re int p=0;p<2;p++) {
if(!dp[i][j][k][p]) continue;
dp[i+1][j+1][p][1]=(dp[i+1][j+1][p][1]+dp[i][j][k][p])%mod;
if(!k) dp[i+1][j+1][p][0]=(dp[i+1][j+1][p][0]+dp[i][j][k][p])%mod;
dp[i+1][j][p][0]=(dp[i+1][j][p][0]+dp[i][j][k][p])%mod;
}
for(re int i=0;i<=n;i++)
ans[i]=(dp[n][i][0][0]+dp[n][i][1][0])%mod;
for(re int j=m;j<=n;j++)
tot=(tot+1ll*((j-m)&1?-1:1)*c[j][m]*fac[n-j]%mod*ans[j]%mod)%mod;
printf("%d\n",(tot+mod)%mod);
return 0;
}

【CF285E】Positions in Permutations的更多相关文章

  1. 【CF285E】Positions in Permutations(动态规划,容斥)

    [CF285E]Positions in Permutations(动态规划,容斥) 题面 CF 洛谷 题解 首先发现恰好很不好算,所以转成至少,这样子只需要确定完一部分数之后剩下随意补. 然后套一个 ...

  2. 【CF715E】Complete the Permutations(容斥,第一类斯特林数)

    [CF715E]Complete the Permutations(容斥,第一类斯特林数) 题面 CF 洛谷 给定两个排列\(p,q\),但是其中有些位置未知,用\(0\)表示. 现在让你补全两个排列 ...

  3. 【CF715E】Complete the Permutations 第一类斯特林数

    题目大意 有两个排列 \(p,q\),其中有一些位置是空的. 你要补全这两个排列. 定义 \(s(p,q)\) 为 每次交换 \(p\) 中的两个数,让 \(p=q\) 的最小操作次数. 求 \(s( ...

  4. 【cdq分治】【CF1093E】 Intersection of Permutations

    传送门 果然前两天写完咕咕咕那个题的题解以后博客就开始咕咕咕了-- Description 给定整数 \(n\) 和两个 \(1~\sim~n\) 的排列 \(A,B\). \(m\) 个操作,操作有 ...

  5. 【LeetCode】Permutations 解题报告

    全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...

  6. 【CF736D】Permutations 线性代数+高斯消元

    [CF736D]Permutations 题意:有一个未知长度为n的排列和m个条件,第i个条件$(a_i,b_i)$表示第$a_i$个位置上的数可以为$b_i$.保证最终合法的排列的个数是奇数.现在有 ...

  7. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  8. 【UVA 11077】 Find the Permutations (置换+第一类斯特林数)

    Find the Permutations Sorting is one of the most used operations in real life, where Computer Scienc ...

  9. 【题解】POJ2279 Mr.Young′s Picture Permutations dp

    [题解]POJ2279 Mr.Young′s Picture Permutations dp 钦定从小往大放,然后直接dp. \(dp(t1,t2,t3,t4,t5)\)代表每一行多少人,判断边界就能 ...

随机推荐

  1. OC开发系列-成员变量的作用域

    成员变量的作用域 OC中成员变量有四种作用域,同时每一种作用域对应着响应的关键字. * @private:自能在当前类的实现@implementation中访问 * @protected: 可以在当前 ...

  2. 笔记23 搭建Spring MVC

    搭建一个最简单的SpringMVC示例 1.配置DispatcherServlet DispatcherServlet是Spring MVC的核心.在这里请求会第一次 接触到框架,它要负责将请求路由到 ...

  3. 前端常用的库和实用技术之JavaScript高级函数

    1.惰性载入函数 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  4. Redis问题整理

    Redis问题总结 1.单点登录的两个项目cookie不一致 由于在配置自定义Cookie的时候 @Bean("shiroCookie") public SimpleCookie ...

  5. CSIC_716_20191104【流程控制语句】

    流程控制语句 if 语法结构 if 逻辑判断为真 : xxxxxx else: xxxxx while 语法结构  (continue.break) while 逻辑判断为真: xxxxxxx con ...

  6. css---5 only-child or nth-of-type

    1  _nth-child系列 :nth-child(index) <!DOCTYPE html> <html lang="en"> <head> ...

  7. bzoj2322 梦想封印

    题意和题解见思路索引. 标程及易错点: #include<bits/stdc++.h> using namespace std; typedef long long ll; ll read ...

  8. bzoj1433: [ZJOI2009]假期的宿舍 [二分图][二分图最大匹配]

    Description Input Output Sample Input 1 3 1 1 0 0 1 0 0 1 1 1 0 0 1 0 0 Sample Output ˆ ˆ HINT 对于30% ...

  9. 关于C++ const 的全面总结 分类: ubuntu 2014-12-03 21:03 72人阅读 评论(0) 收藏

    C++中的const关键字的用法非常灵活,而使用const将大大改善程序的健壮性,本人根据各方面查到的资料进行总结如下,期望对朋友们有所帮助. Const 是C++中常用的类型修饰符,常类型是指使用类 ...

  10. python数据池,python3编码str转bytes,encode

    一.python2 python3的区别 默认编码:2--ASCII码  3---UTF-8 print:python2 可以不需要加括号(),python3必须加括号 python2中有range, ...