转载请注明出处: http://www.cnblogs.com/fraud/           ——by fraud

Iahub and Permutations

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).

Input

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

Output a single integer, the number of ways Iahub could recover his permutation, modulo 1000000007 (109 + 7).

Sample test(s)
input
5-1 -1 4 3 -1

output
2

Note

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的个数,总数去掉这些就是答案了。

 1 //#####################
 2 //Author:fraud
 3 //Blog: http://www.cnblogs.com/fraud/
 4 //#####################
 5 #include <iostream>
 6 #include <sstream>
 7 #include <ios>
 8 #include <iomanip>
 9 #include <functional>
10 #include <algorithm>
11 #include <vector>
12 #include <string>
13 #include <list>
14 #include <queue>
15 #include <deque>
16 #include <stack>
17 #include <set>
18 #include <map>
19 #include <cstdio>
20 #include <cstdlib>
21 #include <cmath>
22 #include <cstring>
23 #include <climits>
24 #include <cctype>
25 using namespace std;
26 #define XINF INT_MAX
27 #define INF 0x3FFFFFFF
28 #define MP(X,Y) make_pair(X,Y)
29 #define PB(X) push_back(X)
30 #define REP(X,N) for(int X=0;X<N;X++)
31 #define REP2(X,L,R) for(int X=L;X<=R;X++)
32 #define DEP(X,R,L) for(int X=R;X>=L;X--)
33 #define CLR(A,X) memset(A,X,sizeof(A))
34 #define IT iterator
35 typedef long long ll;
36 typedef pair<int,int> PII;
37 typedef vector<PII> VII;
38 typedef vector<int> VI;
39 const ll MOD =1000000007 ;
40 int a[10010];
41 ll b[2010];
42 bool vis[2010];
43 ll dp[2010];
44 int main()
45 {
46     ios::sync_with_stdio(false);
47     int n;
48     cin>>n;
49     b[0]=1;
50     for(int i=1;i<=n;i++)cin>>a[i];
51     for(ll i=1;i<2010;i++)b[i]=(b[i-1]*i)%MOD;
52     int tx=0,ty=0;
53     for(int i=1;i<=n;i++){
54         if(a[i]!=-1)vis[a[i]]=1;
55     }
56     for(int i=1;i<=n;i++){
57         if(a[i]==-1){
58             if(vis[i])tx++;
59             else ty++;
60         }
61     }
62     dp[0]=1;
63     dp[1]=tx*dp[0]%MOD;
64     for(int i=2;i<=ty;i++){
65         dp[i]=(tx*dp[i-1]%MOD+(i-1)*dp[i-1]%MOD+(i-1)*dp[i-2]%MOD)%MOD;
66     }
67     cout<<dp[ty]*b[tx]%MOD<<endl;
68
69
70
71
72
73     return 0;
74 }

代码君

转载于:https://www.cnblogs.com/fraud/p/4376994.html

codeforces 340E Iahub and Permutations(错排or容斥)相关推荐

  1. CodeForces 1139D Steps to One(概率dp 容斥/莫比乌斯反演)

    题目链接https://codeforces.com/contest/1139/problem/D 题意:给定一个m,每次在1-m中随机取一个数放到容器中,当容器的gcd为1时停止,求期望步数,用分数 ...

  2. “玲珑杯”线上赛 Round #17 河南专场 B:震惊,99%+的中国人都会算错的问题(容斥计算)...

    传送门 题意 略 分析 是一道稍微变形的容斥题目,容斥一般的公式 \[ans=\sum_iAi-\sum_{i<j}{Ai∩Aj}+\sum_{i<j<k}{Ai∩Aj∩Ak}+.. ...

  3. Codeforces1600数学[CodeForces - 958E1[平面几何+暴力]CodeForces - 888D [组合数+错排问题]]

    A - Guard Duty (easy) CodeForces - 958E1 题目大意:给你n个基地和m个飞船,每个基地都要分配一共飞船,每个飞船都要在一共基地,任意两台飞船到基地得直线路径上不能 ...

  4. 错排、卡特兰数、斯特林数小结

    一. 错排 1.计算公式: 1) D[n] = (n-1)*(D[n-1]+D[n-2]) ,n>=2, D[0] = 1, D[1] = 0 . 解释:对于第n个要加入错排的数,它可以和已经错 ...

  5. 转载:关于错排的相关知识

    转载:关于错排的相关知识 杭电2048相关知识充电 转自:错排公式 分类: 数论 关于程序2012-06-08 19:07 335人阅读 评论(0) 收藏 举报 n2 错排问题 错排问题 就是一种递推 ...

  6. hdu2068RPG的错排

    Problem Description 今年暑假杭电ACM集训队第一次组成女生队,其中有一队叫RPG,但做为集训队成员之一的野骆驼竟然不知道RPG三个人具体是谁谁.RPG给他机会让他猜猜,第一次猜:R ...

  7. 洛谷 P3182 [HAOI2016]放棋子(错排问题)

    题面 luogu 题解 裸的错排问题 错排问题 百度百科:\(n\)个有序的元素应有\(n!\)个不同的排列,如若一个排列使得所有的元素不在原来的位置上,则称这个排列为错排:有的叫重排.如,1 2的错 ...

  8. hdu1465 不容易系列之一(错排问题)

     Problem Description 大家常常感慨,要做好一件事情真的不容易,确实,失败比成功容易多了! 做好"一件"事情尚且不易,若想永远成功而总从不失败,那更是难上加难 ...

  9. 浅谈错排公式的推导及应用

    近期学弟在HDU刷题时遇到了关于错排公式的一些问题,我作为过来人就写这篇博客来指导他们~~~ 错排的定义:一段序列中一共有n个元素,那么可知这些元素一共有n!种排列方法.假如在进行排列时,原来所有的元 ...

  10. HDU2049 组合数学 错排公式

    国庆期间,省城HZ刚刚举行了一场盛大的集体婚礼,为了使婚礼进行的丰富一些,司仪临时想出了有一个有意思的节目,叫做"考新郎",具体的操作是这样的:  首先,给每位新娘打扮得几乎一模一 ...

最新文章

  1. 为智能手机VR体验而生,ARM公布最新处理器架构
  2. RecyclerView缓存机制(回收些啥?)
  3. codeforces B. Friends and Presents(二分+容斥)
  4. 美化UI合约区块链学习版系统+交易大厅
  5. IO Visor Project Use Cases
  6. Android Studio优秀插件汇总
  7. C#数学计算包 Math.NET
  8. 推流和拉流的概念以及RTMP和HLS协议
  9. MongoDB Sharding 机制分析
  10. Android解决异常apk on device '0292bea1': Unable to open sync connection!
  11. python网页教程_python网页教程
  12. 顺序表-有序顺序表的归并算法(新建表+小的先放+余下归并)
  13. 音频编解码算法库 (可支持g711u,g711a,g729,g722,opus等)
  14. 经验模态分解python_EMD经验模态分解
  15. 解决SpringCloud客户端启动报错:“Field XXX required a bean of type XXX that could not be found”
  16. 看了扎克伯格的Avatar,我更想在VR里当大猩猩
  17. lic库的学习与使用流程(一般的库的使用流程)编译运行含有外包库程序的操作export和-L -I
  18. 高智商与低智商的区别_体内平衡与智力的定义
  19. TemplateDoesNotExist错误之伤
  20. Windows10:耳机插到前面板上没声音?

热门文章

  1. ArcEngine中拓扑的使用
  2. Java 验证二代身份证号码是否正确
  3. java连接Neo4j服务器
  4. 别总抱怨自己怀才不遇,告诉你将才与帅才的12个差别!
  5. Java 阻塞队列实现原理分析
  6. Linux内核4.14 LTS发布:那些最新最好的功能特性
  7. 教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神
  8. 摩托android one手机图片,Motorola One都说外观像iPhone,但实际却不一样!
  9. java 浅堆 深堆_【深入浅出-JVM】(57):深堆、浅堆
  10. 【2019/3/23】周进度报告