codeforces 340E Iahub and Permutations(错排or容斥)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
Iahub is so happy about inventing bubble sort graphs that he's staying all day long at the office and writing permutations. Iahubina is angry that she is no more important for Iahub. When Iahub goes away, Iahubina comes to his office and sabotage his research work.
The girl finds an important permutation for the research. The permutation contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ n). She replaces some of permutation elements with -1 value as a revenge.
When Iahub finds out his important permutation is broken, he tries to recover it. The only thing he remembers about the permutation is it didn't have any fixed point. A fixed point for a permutation is an element ak which has value equal to k (ak = k). Your job is to proof to Iahub that trying to recover it is not a good idea. Output the number of permutations which could be originally Iahub's important permutation, modulo 1000000007 (109 + 7).
The first line contains integer n (2 ≤ n ≤ 2000). On the second line, there are n integers, representing Iahub's important permutation after Iahubina replaces some values with -1.
It's guaranteed that there are no fixed points in the given permutation. Also, the given sequence contains at least two numbers -1 and each positive number occurs in the sequence at most once. It's guaranteed that there is at least one suitable permutation.
Output a single integer, the number of ways Iahub could recover his permutation, modulo 1000000007 (109 + 7).
5
-1 -1 4 3 -1
2
For the first test example there are two permutations with no fixed points are [2, 5, 4, 3, 1] and [5, 1, 4, 3, 2]. Any other permutation would have at least one fixed point.
有一个序列,要求你将其中的-1换成1到n中的一个数,使得其成为1到n的一个排列,但要求第i为不得放i,问有几种替换的方法。
分析:
一个典型的错排问题,考虑到有部分数其本身的位置已被摆放,但其本身还没有用到,即这部分数是无限制的排列。
有一部分数是其本身的位置还是-1,但其本身已被使用,这些数的数目与上一种数必定是相同的,这类数已被放置,也无需考虑。
有一部分数是其本身的位置已被摆放,其本身也已被使用,故这类数不会对排列产生影响,也不必考虑。
剩下一部分数是其本身的位置还是-1,其本身也还未被用到,即这一部分的数是有限制的排列。
从而,我们只需要第一类数和第四类数。
假设摆放j个有限制排列的数时的方法为dp[j],设总共有tx个无限制的数。
1.把一个有限制的数摆放到无限制的数所对应的tx个位置上时。。。仔细想想,这就相当于把一个无限制的数摆放到有限制的数的位置,则原来的那个有限制的数变成了无限制的数,即有限制的数减少了1,而无限制的数的数目不变,则有tx*dp[j-1]种。。
2.把一个有限制的数摆放到j-1个有限制的数位置上时,且a[i]=j,a[j]=i;则共有(j-1)*dp[j-2]。
3.把一个有限制的数摆放到j-1个个有限制的数位置上时,且a[i]=j,a[j]!=i;则共有(j-1)*dp[j-1]。
所以dp[j]=tx*dp[j-1]+(j-1)*dp[j-2]+(j-1)*dp[j-1];
然后,在把剩下的tx个摆好,就是tx的阶乘。
然后,就没然后了,就AC了。。。
======================
当然这道题也可以用容斥做,考虑a[i]==i的个数,总数去掉这些就是答案了。
//#####################
//Author:fraud
//Blog: http://www.cnblogs.com/fraud/
//#####################
#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
const ll MOD = ;
int a[];
ll b[];
bool vis[];
ll dp[];
int main()
{
ios::sync_with_stdio(false);
int n;
cin>>n;
b[]=;
for(int i=;i<=n;i++)cin>>a[i];
for(ll i=;i<;i++)b[i]=(b[i-]*i)%MOD;
int tx=,ty=;
for(int i=;i<=n;i++){
if(a[i]!=-)vis[a[i]]=;
}
for(int i=;i<=n;i++){
if(a[i]==-){
if(vis[i])tx++;
else ty++;
}
}
dp[]=;
dp[]=tx*dp[]%MOD;
for(int i=;i<=ty;i++){
dp[i]=(tx*dp[i-]%MOD+(i-)*dp[i-]%MOD+(i-)*dp[i-]%MOD)%MOD;
}
cout<<dp[ty]*b[tx]%MOD<<endl; return ;
}
代码君
codeforces 340E Iahub and Permutations(错排or容斥)的更多相关文章
- CodeForces 340E Iahub and Permutations 错排dp
Iahub and Permutations 题解: 令 cnt1 为可以没有限制位的填充数字个数. 令 cnt2 为有限制位的填充数字个数. 那么:对于cnt1来说, 他的值是cnt1! 然后我们对 ...
- HDU 1465 不容易系列之一 (错排公式+容斥)
题目链接 Problem Description 大家常常感慨,要做好一件事情真的不容易,确实,失败比成功容易多了! 做好"一件"事情尚且不易,若想永远成功而总从不失败,那更是难上 ...
- CodeForces 340E Iahub and Permutations
容斥原理,组合数. 找出有$cnt$个数字还有没放,那么总方案数就是$cnt!$. 总方案数里面包含了正确的和非正确的,我们需要将非正确的删去. 先删去$1$个数字$a[i]=i$的情况,发现会多删, ...
- 【CF285E】Positions in Permutations(动态规划,容斥)
[CF285E]Positions in Permutations(动态规划,容斥) 题面 CF 洛谷 题解 首先发现恰好很不好算,所以转成至少,这样子只需要确定完一部分数之后剩下随意补. 然后套一个 ...
- codeforces 341C Iahub and Permutations(组合数dp)
C. Iahub and Permutations time limit per test 1 second memory limit per test 256 megabytes input sta ...
- CF285 E Positions in Permutations——“恰好->大于”的容斥和允许“随意放”的dp
题目:http://codeforces.com/contest/285/problem/E 是2018.7.31的一场考试的题,当时没做出来. 题解:http://www.cnblogs.com/y ...
- codeforces 439 E. Devu and Birthday Celebration 组合数学 容斥定理
题意: q个询问,每一个询问给出2个数sum,n 1 <= q <= 10^5, 1 <= n <= sum <= 10^5 对于每一个询问,求满足下列条件的数组的方案数 ...
- Codeforces 838A - Binary Blocks(二维前缀和+容斥)
838A - Binary Blocks 思路:求一下前缀和,然后就能很快算出每一小正方块中1的个数了,0的个数等于k*k减去1的个数,两个的最小值就是要加进答案的值. 代码: #include< ...
- Codeforces 585E. Present for Vitalik the Philatelist(容斥)
好题!学习了好多 写法①: 先求出gcd不为1的集合的数量,显然我们可以从大到小枚举计算每种gcd的方案(其实也是容斥),或者可以直接枚举gcd然后容斥(比如最大值是6就用2^cnt[2]-1+3^c ...
随机推荐
- FCKeditor插件开发实例:uploadify多文件上传插件
FCKeditor是一个专门使用在网页上属于开放源代码的所见即所得文字编辑器.它志于轻量化,不需要太复杂的安装步骤即可使用.它可和PHP.JavaScript.ASP.ASP.NET.ColdFusi ...
- JS+css滑动菜单简单实现
JS+css滑动菜单 制作一个简单的滑动菜单,当鼠标指向菜单标题时,滑出二级菜单.移开时二级菜单隐藏.目标很简单,实践时有一些细节需要注意,比如鼠标移向二级菜单的 过程中,二级菜单消失了.还有定位出错 ...
- windows安装Apache HTTP服务器报错:无法启动,因为应用程序的并行配置不正确
Apache HTTP服务器安装后报:无法启动,因为应用程序的并行配置不正确-(已解决) 0条评论 [摘要:本创做品,出自 “深蓝的blog” 专客,迎接转载,转载时请务必说明出处,不然有权穷究版 ...
- iOS基本的发短信和打电话调用
电话.短信是手机的基础功能,iOS中提供了接口,让我们调用.这篇文章简单的介绍一下iOS的打电话.发短信在程序中怎么调用. 1.打电话 [[UIApplication sharedApplicatio ...
- BZOJ 1063 道路设计NOI2008
http://www.lydsy.com/JudgeOnline/problem.php?id=1063 题意:给你一棵树,也有可能是不连通的,把树分成几个链,求每个点到根经过的最大链数最小,而且要输 ...
- 《Programming WPF》翻译 第7章 5.可视化层编程
原文:<Programming WPF>翻译 第7章 5.可视化层编程 形状元素能提供一种便利的方式与图形一起工作,在一些情形中,添加表示绘图的元素到UI树中,可能是比它的价值更加麻烦.你 ...
- .NET 使用unity实现依赖注入
原文地址:http://www.cnblogs.com/wujy/p/3317795.html 一:理论部分 依赖注入:这是 Ioc 模式的一种特殊情况,是一种基于改变对象的行为而不改变类的内部的接口 ...
- ArcGIS API for Silverlight学习笔记
ArcGIS API for Silverlight学习笔记(一):为什么要用Silverlight API(转) 你用上3G手机了吗?你可能会说,我就是喜欢用nokia1100,ABCDEFG跟我都 ...
- 匹配“is outside location”
<pre name="code" class="html">is outside location 怎么匹配? . 匹配除换行外的所有单个字符,通常 ...
- HTTP请求&&响应
在视频上截的图....俗话说好记性不如烂笔头,所以就保留下来 请求: 响应: 状态码: 请求头和响应头的解释: