题目链接:http://codeforces.com/contest/340/problem/E

E. Iahub and Permutations
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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

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

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

题意:

给出大小为n的序列,如果a[i] = k (1<=k<=n),则表明i位置被数字k占领了,如果a[i] = -1,则表明这个数字没有被占领。问:在这种情况下,有多少种错排方式?(题目输入保证有错排)

题解:

1.利用容斥原理计算出非法排列的个数, 非法排列即为至少有一个数是放在原位的, 即a[i] = i。

2.用全排列的个数减去非法排列的个数,即为答案。

容斥原理分析:

1.设m为空位数, k为可以放回原位的个数。

2.枚举可以放回原位的数的个数i,然后再对剩下可放的数进行排列。通式: C(k, i)*A(m-i, m-i):

2.1.当a需要放回原位时(其他有没放回原位不考虑), 那么剩下的数的排列有A(m-1, m-1); 对于 b、c等等, 也如此, 所以总数为C(k,1) * A(m-1, m-1); 根据容斥原理,奇数个时加上。

2.2.当a和b都需要放回原位时(其他有没放回原位不考虑), 那么剩下的数的排列有A(m-2, m-2);对于其他的两两组合也是一样, 所以总数为 C(k,2) * A(m-2, m-2); 根据容斥原理, 偶数个时减去。

2.3.  3个、4个、5个 …… k个。奇数个时加, 偶数个时减。

易错点:

1.凡是带有除法的式子, 都不能直接求模。

2.求模时, 若是加上负数, 需要: ans = (ans + mod)% mod 。

代码如下:

 #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const double eps = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int maxn = 2e3+; bool val[maxn], pos[maxn];
LL C[maxn][maxn], A[maxn]; void init()
{
A[] = ; C[][] = ;
for(int i = ; i<maxn; i++)
{
A[i] = (1LL*i*A[i-])%mod;
C[i][] = ;
for(int j = ; j<=i; j++)
C[i][j] = (C[i-][j-] + C[i-][j])%mod;
}
} int main()
{
init();
int n, m, k;
while(scanf("%d",&n)!=EOF)
{
for(int i = ; i<=n; i++)
{
int x;
scanf("%d",&x);
if(x!=-)
val[x] = pos[i] = ;
} k = m = ;
for(int i = ; i<=n; i++)
{
if(!pos[i]) m++;
if(!pos[i] && !val[i]) k++;
} LL ans = A[m];
for(int i = ; i<=k; i++)
{
LL tmp = (1LL*C[k][i]*A[m-i])%mod;
ans -= (i&)?tmp:-tmp; //容斥原理
ans = (ans+mod)%mod;
}
cout<<ans<<endl;
}
}

Codeforces Round #198 (Div. 2) E. Iahub and Permutations —— 容斥原理的更多相关文章

  1. Codeforces Round #198 (Div. 1) D. Iahub and Xors 二维树状数组*

    D. Iahub and Xors   Iahub does not like background stories, so he'll tell you exactly what this prob ...

  2. Codeforces Round #198 (Div. 2)A,B题解

    Codeforces Round #198 (Div. 2) 昨天看到奋斗群的群赛,好奇的去做了一下, 大概花了3个小时Ak,我大概可以退役了吧 那下面来稍微总结一下 A. The Wall Iahu ...

  3. Codeforces Round #485 (Div. 2) E. Petr and Permutations

    Codeforces Round #485 (Div. 2) E. Petr and Permutations 题目连接: http://codeforces.com/contest/987/prob ...

  4. Codeforces Round #198 (Div. 2)

    A.The Wall 题意:两个人粉刷墙壁,甲从粉刷标号为x,2x,3x...的小块乙粉刷标号为y,2y,3y...的小块问在某个区间内被重复粉刷的小块的个数. 分析:求出x和y的最小公倍数,然后做一 ...

  5. Codeforces Round #198 (Div. 2)E题解

    E. Iahub and Permutations Iahub is so happy about inventing bubble sort graphs that he's staying all ...

  6. Codeforces Round #198 (Div. 1 + Div. 2)

    A. The Wall 求下gcd即可. B. Maximal Area Quadrilateral 枚举对角线,根据叉积判断顺.逆时针方向构成的最大面积. 由于点坐标绝对值不超过1000,用int比 ...

  7. Codeforces Round #198 (Div. 2) D. Bubble Sort Graph (转化为最长非降子序列)

    D. Bubble Sort Graph time limit per test 1 second memory limit per test 256 megabytes input standard ...

  8. [置顶] Codeforces Round #198 (Div. 1)(A,B,C,D)

    http://codeforces.com/contest/341 赛后做的虚拟比赛,40分钟出了3题,RP爆发. A计数问题 我们可以对每对分析,分别对每对<a, b>(a走到b)进行统 ...

  9. Codeforces Round #198 (Div. 2) 340C

    C. Tourist Problem time limit per test 1 second memory limit per test 256 megabytes input standard i ...

随机推荐

  1. Java 8 Streams的简单使用方法

    Java 8 Streams的简单使用方法 package JDK8Test; import java.util.ArrayList; public class Main { public stati ...

  2. springboot多环境配置

    springboot多环境(dev.test.prod)配置 2017-07-17 10:33 1290人阅读 评论(0) 收藏 举报  分类: spring boot(6)  版权声明:本文为博主原 ...

  3. 邁向IT專家成功之路的三十則鐵律 鐵律二十二:IT人升遷之道-無為

    升遷管道是許多人求職時相當重要的考量之一,畢竟人除了很愛錢之外更愛顯赫的頭銜,然而在企業中越顯赫的頭銜,其背後通常有更多的罵名,因為許多人的高官厚爵都是踩著一群人的頭頂爬上去的,隨時哪一天跌了下來,都 ...

  4. Android应用开发-小巫CSDN博客客户端开发开篇

    2014年9月8日 八月十五 祝各位中秋节快乐 小巫断断续续花了几个星期的时间开发了这么一款应用——小巫CSDN博客,属于私人定制的这样的一款应用,整个客户端的数据全部来自本人博客,是通过爬取本人博客 ...

  5. Lucene 源码分析之倒排索引(一)

    倒排索引是 Lucene 的核心数据结构,该系列文章将从源码层面(源码版本:Lucene-7.3.0)分析.该系列文章将以如下的思路展开. 什么是倒排索引? 如何定位 Lucene 中的倒排索引? 倒 ...

  6. react 监听 移动端 手机键盘 enter 事件

    处理方法: (1)html书写 form标签中去掉action参数,定义onSubmit方法,如下所示: /** * 搜索框 组件 */ import React,{PureComponent} fr ...

  7. Unix系统介绍

    一.基础知识 操作系统 用户与计算机硬件之间的界面,是控制.管理计算机内各种硬件与软件资源.它是一个多用户.多任务.分时的操作系统. 对于分时系统:假如a进程需要16个时间片,现在根据优先级只分配了1 ...

  8. 【足迹C++primer】39、动态内存与智能指针(3)

    动态内存与智能指针(3) /** * 功能:动态内存与智能指针 * 时间:2014年7月8日15:33:58 * 作者:cutter_point */ #include<iostream> ...

  9. 基于Innobackupex的全备恢复

    对于MySQL数据库的热备,xtrabackup是大多数DBA朋友们的选择.xtrabackup内嵌了一个innobackupex可用于热备MySQL数据库.本文描写叙述了基于innobackupex ...

  10. Python+Selenium框架 ---一个类文件多个测试方法情况下测试固件的写法

    我们测试中,肯定需要,打开一个页面,然后测试这个页面的多个用例,才关闭这个页面,去测试其他页面,在unittest是有相关测试固件方法去支持这种行为.请看下面 # coding=utf-8 impor ...