【题解】CF#285 E-Positions in Permutations
挺有收获的一道题ヾ(◍°∇°◍)ノ゙
恰好为 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的更多相关文章
- Codeforces 285 E. Positions in Permutations
\(>Codeforces \space 285 E. Positions in Permutations<\) 题目大意 : 定义一个长度为 \(n\) 的排列中第 \(i\) 个元素是 ...
- 【CF285E】Positions in Permutations(动态规划,容斥)
[CF285E]Positions in Permutations(动态规划,容斥) 题面 CF 洛谷 题解 首先发现恰好很不好算,所以转成至少,这样子只需要确定完一部分数之后剩下随意补. 然后套一个 ...
- CF 715 E. Complete the Permutations
CF 715 E. Complete the Permutations 题目大意:给定两个排列\(p,q\)的一部分.定义两个排列\(p,q\)的距离为使用最少的交换次数使得\(p_i=q_i\).对 ...
- CodeForces - 285E: Positions in Permutations(DP+组合数+容斥)
Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive in ...
- 竞赛题解 - CF Round #524 Div.2
CF Round #524 Div.2 - 竞赛题解 不容易CF有一场下午的比赛,开心的和一个神犇一起报了名 被虐爆--前两题水过去,第三题卡了好久,第四题毫无头绪QwQ Codeforces 传送门 ...
- CF285 E Positions in Permutations——“恰好->大于”的容斥和允许“随意放”的dp
题目:http://codeforces.com/contest/285/problem/E 是2018.7.31的一场考试的题,当时没做出来. 题解:http://www.cnblogs.com/y ...
- 最近两场比赛 CF 285 & TC 646
Codeforces 285 这场rating又掉了,好在只掉了十多. 题目比较水,但是我比赛时居然只艰辛地过了前两道. 504A 由于图是森林,所以一定有度为1的点,把这些点删了后图还是森林.然后就 ...
- 题解——CF Manthan, Codefest 18 (rated, Div. 1 + Div. 2) T5(思维)
还是dfs? 好像自己写的有锅 过不去 看了题解修改了才过qwq #include <cstdio> #include <algorithm> #include <cst ...
- CF 1093 E. Intersection of Permutations
E. Intersection of Permutations 链接 题意: 给定两个序列,询问第一个排列的[l1,r1]和第二个排列[l2,r2]中有多少个共同的数,支持在第二个排列中交换两个数. ...
随机推荐
- 厦门Uber优步司机奖励政策(12月14日到12月20日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- day 5 飞机发射子弹 难点??
1.效果图 2.飞机发出子弹 #-*- coding:utf-8 -*- import pygame import time from pygame.locals import * class Her ...
- MySQL高级-性能分析Explain
1.使用Explain关键字可以模拟优化器执行SQL查询语句,从而知道MySQL是如何处理你的SQL语句的.分析你的查询语句或是表结构的性能瓶颈 . 2.执行方法:Explain + SQL语句 解释 ...
- libevent学习三(Getting an event_base)
1.一个event_base持有了一系列的事件,并监控和决定哪些事件需要激活, 2.每一个event_base背后都有一个支持其工作的方法(诸如select,poll,epoll,kquene...) ...
- MySQL☞substr函数
substr函数:截取字符串 格式如下: select substr(参数1,参数2,参数3) from 表名 参数1:列名/字符串 参数2:起始位置,如果为正数,就表示从正数的位置往下截取字符 ...
- system_Class类说明文档
system_Class类是FastCMS系统必须的,全局对象system是system_Class的实例,其主要包含二类操作: 1.token 操作: token可以存储当前访客的私有信息,取代se ...
- pxe+kickstart无人值守安装
常用软件安装及使用目录 第1章 以前是怎么安装系统的 l 光盘(ISO文件,光盘的镜像文件)===>每一台物理机都得给一个光驱,如果用外置光驱的话,是不是每台机器都需要插一下 l U盘:ISO镜 ...
- eg_1
1. 编写一个程序,输出一个字符串中的大写英文字母个数,小写英文字母个数以及非英文字母个数. 第一种方法: public class Test { public static void main(St ...
- II 3.1 连接到服务器
II 3.1 连接到服务器 package socket; import java.io.IOException; import java.io.InputStream; import java.ne ...
- 九度oj 题目1495:关键点
题目描述: 在一个无权图中,两个节点间的最短距离可以看成从一个节点出发到达另一个节点至少需要经过的边的个数. 同时,任意两个节点间的最短路径可能有多条,使得从一个节点出发可以有多条最短路径可以选择,并 ...