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

  恰好为 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. EDM站点

    设计邮件模版 http://templates.mailchimp.com/

  2. 用ext_skel,实现一个PHP扩展,添加到PHP并调用

    1 创建函数定义文件 #mkdir /home/phpext #vi mydefined.skel string get_text(string str) 2 根据README所提供的信息创建预定义文 ...

  3. 文件包含漏洞(RFI)

    1文件包含漏洞简介 include  require  include_once   require_once RFI综述 RFI是Remote File Inclusion的英文缩写,直译过来就是远 ...

  4. 2019年1月23日,好像是这个日子,RF发布了 1.7.3.1 支持python3.6以上了,安装成功。

    安装步骤:(win10 家庭版 64) 1.安装Python3.7.2,记得勾选添加Path 2.pip install robotframework 3.pip install wxPython 4 ...

  5. Selenium(Python)PageObject页面对象

    使用PageObject页面对象的好处是, 当页面元素的位置发生改变时, 只需要去修改Xpath或者ID, 而不用去修改测试用例本身: 本次的思路是: 1.常用方法类 2.页面对象类 3.测试用例类 ...

  6. javascript常用对象方法

    concat:连接产生一个新数组 [1,2].concat([3,4])     >> [1, 2, 3, 4] filter:返回符合条件的一个新数组 [1,2,3,4,5].filte ...

  7. JVM--内存模型与线程

    一.硬件与效率的一致性 计算机的存储设备与处理器的运算速度存在几个数量级的差距,现在计算机系统不得不在内存和处理器之间增加一层高速缓存(cache)来作为缓冲.将运算需要的数据复制到缓存中,让运算能够 ...

  8. [SHELL]输出目录下所有的可执行文件,批量创建用户

    #!/bin/bash IFS=: for folder in $PATH #PATH变量分隔符为: do echo $folder echo ------------------ for file ...

  9. [Data Structures and Algorithms - 1] Introduction & Mathematics

    References: 1. Stanford University CS97SI by Jaehyun Park 2. Introduction to Algorithms 3. Kuangbin' ...

  10. JQuery+ajax数据加载..........

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...