【CF285E】Positions in Permutations
刷水题涨信心
显然这是个广义容斥,我们现在算一下至少有\(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的更多相关文章
- 【CF285E】Positions in Permutations(动态规划,容斥)
[CF285E]Positions in Permutations(动态规划,容斥) 题面 CF 洛谷 题解 首先发现恰好很不好算,所以转成至少,这样子只需要确定完一部分数之后剩下随意补. 然后套一个 ...
- 【CF715E】Complete the Permutations(容斥,第一类斯特林数)
[CF715E]Complete the Permutations(容斥,第一类斯特林数) 题面 CF 洛谷 给定两个排列\(p,q\),但是其中有些位置未知,用\(0\)表示. 现在让你补全两个排列 ...
- 【CF715E】Complete the Permutations 第一类斯特林数
题目大意 有两个排列 \(p,q\),其中有一些位置是空的. 你要补全这两个排列. 定义 \(s(p,q)\) 为 每次交换 \(p\) 中的两个数,让 \(p=q\) 的最小操作次数. 求 \(s( ...
- 【cdq分治】【CF1093E】 Intersection of Permutations
传送门 果然前两天写完咕咕咕那个题的题解以后博客就开始咕咕咕了-- Description 给定整数 \(n\) 和两个 \(1~\sim~n\) 的排列 \(A,B\). \(m\) 个操作,操作有 ...
- 【LeetCode】Permutations 解题报告
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
- 【CF736D】Permutations 线性代数+高斯消元
[CF736D]Permutations 题意:有一个未知长度为n的排列和m个条件,第i个条件$(a_i,b_i)$表示第$a_i$个位置上的数可以为$b_i$.保证最终合法的排列的个数是奇数.现在有 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【UVA 11077】 Find the Permutations (置换+第一类斯特林数)
Find the Permutations Sorting is one of the most used operations in real life, where Computer Scienc ...
- 【题解】POJ2279 Mr.Young′s Picture Permutations dp
[题解]POJ2279 Mr.Young′s Picture Permutations dp 钦定从小往大放,然后直接dp. \(dp(t1,t2,t3,t4,t5)\)代表每一行多少人,判断边界就能 ...
随机推荐
- MD5/SHA1/Hmac_SHA1
1.MD5 #import <CommonCrypto/CommonDigest.h> + (NSString *) md5:(NSString *) input { const char ...
- npm 安装vue 报错Failed at the chromedriver@2.46.0 install script 'node install.js'
原因一般是下载源被封了,我们连接淘宝的下载源下载: npm install chromedriver --chromedriver_cdnurl=http://cdn.npm.taobao.org/d ...
- pdfkit
官方文档 0.准备 需要引入两个包,首先要npm install pdfkit安装pdfkit包 const PDF = require('pdfkit'); const fs = require(' ...
- 学习修复Laravel The only supported ciphers are AES-128-CBC and AES-256-CBC
The only supported ciphers are AES-128-CBC and AES-256-CBC 在项目中,删除了 .env的APP_KEY的值,再运行 php artisan k ...
- CF459E Pashmak and Graph (Dag dp)
传送门 解题思路 \(dag\)上\(dp\),首先要按照边权排序,然后图都不用建直接\(dp\)就行了.注意边权相等的要一起处理,具体来讲就是要开一个辅助数组\(g[i]\),来避免同层转移. 代码 ...
- Super OJ 序列计数
题意: 给出序列 a1,a2,--an(0≤ai≤109),求三元组(ai,aj,ak)(1≤i<j<k≤n)满足 ai<aj>ak 的数量. 分析: 开两个\(BIT\),分 ...
- work-record:20190618
ylbtech-work-record:20190618 1.返回顶部 1.1. -- formId记录表 -- select * from record_form_id; -- drop table ...
- IntelliJ IDEA更换主题样式分享
原文地址:https://blog.csdn.net/liu865033503/article/details/79481785 .自定义主题样式网址:http://www.riaway.com/in ...
- Assert(断言) 的用法
Assert Assert是断言的意思,头文件为assert.h, assert是一个宏 功 能: 测试一个条件并可能使程序终止 用 法: void assert(int test); 在单元测试中经 ...
- 17-7-es6作用域
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...