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

  恰好为 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. ORB-SLAM(九)LocalMapping

    LocalMapping作用是将Tracking中送来的关键帧放在mlNewKeyFrame列表中:处理新关键帧,地图点检查剔除,生成新地图点,Local BA,关键帧剔除.主要工作在于维护局部地图, ...

  2. PHP数组中插入元素

    1. array_unshift()数组头插入新元素 $fruits = array('apple','pear','banana','orange'); array_unshift($fruits, ...

  3. 【JUC源码解析】AQS

    简介 AQS,也即AbstractQueuedSynchronizer,抽象队列同步器,提供了一个框架,可以依赖它实现阻塞锁和相关同步器.有两种类型,独占式(Exclusive)和共享式(Share) ...

  4. APP产品设计流程图

    产品设计流程(toB) 工作有半个月了,遇到了很多问题,也在不断学习和充实自己,让自己的工作变得更加清晰和流程化,所以整理了这么个设计流程. 收集整理一切有用或则以后可能会用的文档. 从文档里面提炼用 ...

  5. MySQL日期函数、时间函数总结(MySQL 5.X)

    一.获得当前日期时间函数 1.1 获得当前日期+时间(date + time)函数:now() select now(); # :: 除了 now() 函数能获得当前的日期时间外,MySQL 中还有下 ...

  6. jmeter 函数助手

    1.选项,函数助手对话框,打开函数助手 2.使用方法 输入参数,点击生成,可以直接使用(Name of variable in which to store the result (optional) ...

  7. TPO-10 C2 Return a literature book

    TPO-10 C2 Return a literature book 第 1 段 1.Listen to a conversation between a student and an employe ...

  8. Unity Lighting - Choosing a Lighting Technique 选择照明技术(一)

      Choosing a Lighting Technique 选择照明技术 https://unity3d.com/cn/learn/tutorials/topics/graphics/choosi ...

  9. Consul 简介

    Consul包含很多组件,总体来数,Consul是一种服务发现和配置工具. 服务发现:一个客户端提供自己的服务,例如api服务,或者mysql服务,另一个客户端就可以利用Consul通过DNS或者ht ...

  10. 了解Python控制流语句——if语句

    控制流 截止到现在,在我们所看过的程序中,总是有一系列语句从上到下精确排列,并交由 Python 忠实地执行.如果你想改变这一工作流程,应该怎么做?就像这样的情况:你需要程序作出一些决定,并依据不同的 ...