挺有收获的一道题ヾ(◍°∇°◍)ノ゙

  恰好为 m ,这个限制仿佛不是很好处理。一般而言,我所了解的恰好为 k 的条件,不是用组合数 / dp状态转移 / 斜率二分就只剩下容斥了。我们可以先处理出 num[i] 表示至少有 i 个完美位置的方案数,之后再容斥得到 ans[m] (恰好为 m 个)。如何获得 num 数组?建立dp状态为 f[i][j][p][q], (其中p, q为01状态)表示dp到第 i 个位置,已经出现了 j 个完美的位置,且 i 和 i + 1 是否被用过。转移的时候分情况讨论:1.当前位置不成为完美的位置,直接忽略;2.当前位置填 i - 1 成为一个完美的位置;3.当前位置填 i + 1 成为一个完美的位置。之后把忽略掉的数乘上排列数即可。

  这个状态并不是很好想到,但我们主要要明确:第 i 个位置是否成为完美的位置,仅仅与 i - 1 和 i + 1 有关,而也仅有 i + 1 对于后一个位置存在影响。忽略的数字我们可以直接跳过不算,因为当前不用这个数字 i , i 在之后也无法再影响到完美数的形成。至于容斥,我还是只会 \(n^{2}\) 的由至少到恰好的递推……这个 O(n) 的全背背式子吧 :(

 \(ans[m] = num[m] - C(m + 1, m) * num[m + 1] ... * (-1)^{n - m} * C(n, m) * num[n]\)

#include <bits/stdc++.h>
using namespace std;
#define maxn 1500
#define int long long
#define mod 1000000007
int n, K, f[maxn][maxn][][], num[maxn];
int ans[maxn], fac[maxn], C[maxn][maxn]; int read()
{
int x = , k = ;
char c; c = getchar();
while(c < '' || c > '') { if(c == '-') k = -; c = getchar(); }
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * k;
} void Up(int &x, int y) { x = (x + y) % mod; }
void pre()
{
fac[] = ; for(int i = ; i < maxn; i ++) fac[i] = fac[i - ] * i % mod;
for(int i = ; i < maxn; i ++) C[i][] = ;
for(int i = ; i < maxn; i ++)
for(int j = ; j < maxn; j ++)
C[i][j] = (C[i - ][j - ] + C[i - ][j]) % mod;
} signed main()
{
pre(); n = read(), K = read();
f[][][][] = ;
for(int i = ; i < n; i ++)
{
for(int j = ; j <= i; j ++)
for(int p = ; p <= ; p ++)
for(int q = ; q <= ; q ++)
{
Up(f[i + ][j][q][], f[i][j][p][q]);
if(!p) Up(f[i + ][j + ][q][], f[i][j][p][q]);
if(i < n - ) Up(f[i + ][j + ][q][], f[i][j][p][q]);
}
}
for(int i = ; i <= n; i ++)
{
for(int p = ; p <= ; p ++)
for(int q = ; q <= ; q ++)
Up(num[i], f[n][i][p][q]);
num[i] = num[i] * fac[n - i] % mod;
}
ans[K] = num[K];
for(int i = K + , t = -; i <= n; i ++, t *= -)
Up(ans[K], (t * C[i][K] * num[i] % mod) + mod);
/*for(int i = n; i >= K; i --)
{
int t = num[i];
for(int j = n; j > i; j --)
t = (t - (ans[j] * C[j][i]) % mod + mod) % mod;
ans[i] = t;
}*/
printf("%lld\n", ans[K]);
return ;
}

【题解】CF#285 E-Positions in Permutations的更多相关文章

  1. Codeforces 285 E. Positions in Permutations

    \(>Codeforces \space 285 E. Positions in Permutations<\) 题目大意 : 定义一个长度为 \(n\) 的排列中第 \(i\) 个元素是 ...

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

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

  3. CF 715 E. Complete the Permutations

    CF 715 E. Complete the Permutations 题目大意:给定两个排列\(p,q\)的一部分.定义两个排列\(p,q\)的距离为使用最少的交换次数使得\(p_i=q_i\).对 ...

  4. CodeForces - 285E: Positions in Permutations(DP+组合数+容斥)

    Permutation p is an ordered set of integers p1,  p2,  ...,  pn, consisting of n distinct positive in ...

  5. 竞赛题解 - CF Round #524 Div.2

    CF Round #524 Div.2 - 竞赛题解 不容易CF有一场下午的比赛,开心的和一个神犇一起报了名 被虐爆--前两题水过去,第三题卡了好久,第四题毫无头绪QwQ Codeforces 传送门 ...

  6. CF285 E Positions in Permutations——“恰好->大于”的容斥和允许“随意放”的dp

    题目:http://codeforces.com/contest/285/problem/E 是2018.7.31的一场考试的题,当时没做出来. 题解:http://www.cnblogs.com/y ...

  7. 最近两场比赛 CF 285 & TC 646

    Codeforces 285 这场rating又掉了,好在只掉了十多. 题目比较水,但是我比赛时居然只艰辛地过了前两道. 504A 由于图是森林,所以一定有度为1的点,把这些点删了后图还是森林.然后就 ...

  8. 题解——CF Manthan, Codefest 18 (rated, Div. 1 + Div. 2) T5(思维)

    还是dfs? 好像自己写的有锅 过不去 看了题解修改了才过qwq #include <cstdio> #include <algorithm> #include <cst ...

  9. CF 1093 E. Intersection of Permutations

    E. Intersection of Permutations 链接 题意: 给定两个序列,询问第一个排列的[l1,r1]和第二个排列[l2,r2]中有多少个共同的数,支持在第二个排列中交换两个数. ...

随机推荐

  1. [转帖]localhost与127.0.0.1的区别

    localhost与127.0.0.1的区别 https://www.cnblogs.com/hqbhonker/p/3449975.html 前段时间用PG的时候总有问题 当时没有考虑 localh ...

  2. 【白书训练指南】(UVa10755)Garbage Heap

    先po代码,之后把我那几个不太明了的知识点讲讲,巩固以下.三维的扫描线算法想要掌握还真是有一定的难度的. 代码 #include <iostream> #include <cstri ...

  3. Linux命令应用大词典-第39章 网络安全

    39.1 rtacct:网络统计工具 39.2 nmap:报告远程主机特征 39.3 tcpdump:实现网络数据采集分析 39.4 iptstate:显示IP表状态表条目 39.5 nstat:监控 ...

  4. Unity编辑器 - 鼠标悬停在控件上时改变鼠标样式

    Unity编辑器 - 鼠标悬停在控件上时改变鼠标样式 摘自Unity文档 EditorGUIUtility.AddCursorRect public static void AddCursorRect ...

  5. 前端开发工程师 - 05.产品前端架构 - 协作流程 & 接口设计 & 版本管理 & 技术选型 &开发实践

    05.产品前端架构 第1章--协作流程 WEB系统 角色定义 协作流程 职责说明 第2章--接口设计 概述 接口规范 规范应用 本地开发 第3章--版本管理 见 Java开发工程师(Web方向) - ...

  6. leetcode-组合总数IV(动态规划)

    377. 组合总和 Ⅳ 给定一个由正整数组成且不存在重复数字的数组,找出和为给定目标正整数的组合的个数. 示例: nums = [1, 2, 3] target = 4   所有可能的组合为: (1, ...

  7. java length属性、length()、size()

    length属性 length是属性,用于说明数组的长度. String []list={"wo","shi","shuaibi"}; Sy ...

  8. tomcat下载、安装

    下载 官网地址:https://tomcat.apache.org/download-80.cgi 安装 直接安装即可.安装完毕后Tomcat的目录结构如下: bin:脚本目录 ​ 启动脚本:star ...

  9. [leetcode-744-Find Smallest Letter Greater Than Target]

    Given a list of sorted characters letters containing only lowercase letters, and given a target lett ...

  10. 调试Python的方式

    调试Python有如下几种方式: 1 使用print语句 2 使用IDE的debuggers 3 使用命令行调试器pdb,这是Python的一个标准库,类似gdb 4 使用-i命令行选项.在使用命令行 ...