codeforces 341C Iahub and Permutations(组合数dp)
1 second
256 megabytes
standard input
standard output
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~n个数字,要求错排,不过-1说明这个数字还不确定,但是给定了非-1数字,说明这个数字已经定了下来。开始很莽撞的直接用错排数计算。。。实际上不然,填数字的时候,有一些是可以乱填没有限制的,因为这些数字已经被开始填了,有一些则有限制,比如给两组数据。
-1 -1 4 3 -1
5
-1 -1 2 5 -1
显然所有已经填好的位置可以不管,我们只看 a[i] = -1 的位置。
x 表示目前有多少个位置使得a[i] = -1 且数字 i 已经被填在某个位置上,称为无限制位置。
y 表示目前有多少个位置使得a[i] = -1 且数字 i 没有被填在某个位置上,称为有限制位置。
那么,最一开始的时候,我们先把 y 个有限制位置抛弃不看。
因为a[i] = -1 且 i 也没有出现在任何位置上,所以忽略不看不会有影响。
现在我们先把 x 个无限制的位置填好。
由于有 x 个数的 a[i] = -1 但是数字 i 已经被使用了。所以也一定有 x 个数的a[i] != -1但是数字 i 还没被用。
就把这 x 个没使用的数字放在 x 个被填的位置上。方法数是 x! 。
现在我们把 y 个无限制的位置一个接一个的加进来
用 d[i] 表示已经有多少个无限制的位置被加进来,且不违反错排的规则。
显然d[0] = x ! 。我们所要求的就是d[y].
前面都比较好理解,主要就是下面这一部分,比较难理解了。
为了更好的理解,我拿个例子辅助说明。
如果此时我们把第 i 个有限制的位置加进来。分以下几种情况:
1)我们从 x 个无限制的的位置中找一个 j ,令a[i] = a[j],a[j] = i。规约到d[i - 1]的方案数。
-1 -1 2 5 -1
2)我们从i - 1个有限制的位置中找一个位置 j ,令a[i] = j,a[j] = i。规约到d[i - 2]的方案数。
-1 -1 3 4 -1
3)我们从i - 1个有限制的位置中找一个位置 j ,令a[i] = j,但是a[j] != i。规约到d[i - 1]的方案数。也就相当于由原来的 d[j] != j 限制变为了d[j] != i,其它限制不变。
-1 -1 3 4 -1
AC代码:
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
using namespace std;
int mod=1e9+7;
__int64 d[2002];
int a[2002]; __int64 cal(int p)
{
__int64 ans=1;
int i;
for(i=2;i<=p;i++)
ans=(ans*i)%mod;
return ans;
} int main()
{
int n,x,y;
while(~scanf("%d",&n))
{
int cnt=0,t=0; //cnt记录可以填的个数,t记录填的数字被占用的个数
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i]==-1)
cnt++;
}
for(int i=1;i<=n;i++)
{
if(a[i]>0)
{
if(a[a[i]]==-1)
t++;
}
} x=t; //x是无限制的个数
y=cnt-t;
d[0]=cal(x); //d[0]=x!
for(int i=1;i<=y;i++)
{
d[i]=((x+i-1)*d[i-1])%mod;
if(i>1)
d[i]=(d[i]+(i-1)*d[i-2])%mod;
}
printf("%I64d\n",d[y]);
}
return 0;
} /*
5
-1 -1 4 3 -1
5
-1 -1 4 -1 -1
5
-1 -1 2 5 -1
*/
codeforces 341C Iahub and Permutations(组合数dp)的更多相关文章
- CodeForces 340E Iahub and Permutations 错排dp
		Iahub and Permutations 题解: 令 cnt1 为可以没有限制位的填充数字个数. 令 cnt2 为有限制位的填充数字个数. 那么:对于cnt1来说, 他的值是cnt1! 然后我们对 ... 
- codeforces 340E Iahub and Permutations(错排or容斥)
		转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Iahub and Permutations Iahub is so happy ... 
- CodeForces 340E Iahub and Permutations
		容斥原理,组合数. 找出有$cnt$个数字还有没放,那么总方案数就是$cnt!$. 总方案数里面包含了正确的和非正确的,我们需要将非正确的删去. 先删去$1$个数字$a[i]=i$的情况,发现会多删, ... 
- Codeforces Round #198 (Div. 2) E. Iahub and Permutations —— 容斥原理
		题目链接:http://codeforces.com/contest/340/problem/E E. Iahub and Permutations time limit per test 1 sec ... 
- 【bzoj4517】[Sdoi2016]排列计数  组合数+dp
		题目描述 求有多少种长度为 n 的序列 A,满足以下条件: 1 ~ n 这 n 个数在序列中各出现了一次 若第 i 个数 A[i] 的值为 i,则称 i 是稳定的.序列恰好有 m 个数是稳定的 满足条 ... 
- cf-341C Iahub and Permutations
		C. Iahub and Permutations time limit per test 1 second memory limit per test 256 megabytes input sta ... 
- [Codeforces 865C]Gotta Go Fast(期望dp+二分答案)
		[Codeforces 865C]Gotta Go Fast(期望dp+二分答案) 题面 一个游戏一共有n个关卡,对于第i关,用a[i]时间通过的概率为p[i],用b[i]通过的时间为1-p[i],每 ... 
- [CodeForces - 1225E]Rock Is Push 【dp】【前缀和】
		[CodeForces - 1225E]Rock Is Push [dp][前缀和] 标签:题解 codeforces题解 dp 前缀和 题目描述 Time limit 2000 ms Memory ... 
- [Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT)
		[Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT) 题面 给出一个\(n\)个点\(m\)条边的有向图(可能有环),走每条边需要支付一个价格\(c_i ... 
随机推荐
- (转)JavaWeb学习总结(十三)——使用Session防止表单重复提交
			如何防止表单重复提交 在平时开发中,如果网速比较慢的情况下,用户提交表单后,发现服务器半天都没有响应,那么用户可能会以为是自己没有提交表单,就会再点击提交按钮重复提交表单,我们在开发中必须防止表单重复 ... 
- oc总结
			OC10天大纲 一.类和对象 1.什么是类? 同一种对象的抽象就是类. 2.什么是对象? 世界上的任何事物都可以称为对象,每个对象都有他自己的属性和行为. 3.如何创建一个类(请把一个.h和一个.m粘 ... 
- C# 深入浅出 委托与事件
			C#中的委托和事件的概念接触很久了,但是一直以来总没有特别透彻的感觉,现在我在这里总结一下: 首先我们要知道委托的由来,为什么要使用委托了? 我们先看一个例子: 假设我们有这样一个需求,需要计算在不同 ... 
- 一起学makefile
			Unix.Linux必学知识哈哈,网上看到一哥们写得挺好挺详细的,直接复制地址就分享哈哈哈. 跟我一起写 Makefile(一) 概述 跟我一起写 Makefile(二) make是如何工作的 跟我一 ... 
- memset 初始化数组
			memset是初始化一段内存区域的函数,其头文件是<string.h>,以前经常使用出现错误,整理一下. C++ Reference对于memset的定义为: void * memset ... 
- Ubuntu12.04安装java以及Eclipse和Tomcat
			阔别已久的Java,现在捡起来偶感觉亚历山大啊,就单单一个环境的安装就搞得我焦头烂额啊.真后悔当初学习Java的时候没有记录下来这一门槛——环境的搭建,要知道学好一门语言,Develop Enviro ... 
- (转载)图解Linux系统的系统架构
			我以下图为基础,说明Linux的架构(architecture).(该图参考<Advanced Programming in Unix Environment>) 最内层是硬件,最外层是用 ... 
- 对于方法 String.Contains,只支持可在客户端上求值的参数。
			var ProjectLevel_XJJS = "06,07,08,09"; p.Where(e =>ProjectLevel_XJJS.Contains(e.LevelCo ... 
- OOCSS学习(二)
			OOCSS —— 面向对象CSS 5.CSS团队精神:CSS最佳团队开发 在本文中,你将学习书写CSS的最佳实践来帮助你避免不一致和冗余;实际上,这样制定标准,简化了团队开发的工作. 1)结构化 (根 ... 
- Codeforces 301_div.2_Ice Cave(BFS走冰块)
			Ice Cave Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Descripti ... 
