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

代码中直接把第一维压掉了 

#include<cstdio>
using namespace std;
typedef long long ll;
const int N=;
const ll mod=1e9+;
int n,X,Y,a[N];bool flag[N];
ll f[N];
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
if(~a[i]) flag[a[i]]=;
}
for(int i=;i<=n;i++){
if(!(~a[i])){
if(flag[i]) X++;
else Y++;
}
}
f[]=;
for(int i=;i<=X;i++) f[]=f[]*i%mod;
for(int i=;i<=Y;i++){
f[i]=(X+i-)*f[i-]%mod;
if(i>){
f[i]=(f[i]+(i-)*f[i-])%mod;
}
}
printf("%I64d",f[Y]);
return ;
}

cf-341C Iahub and Permutations的更多相关文章

  1. codeforces 341C Iahub and Permutations(组合数dp)

    C. Iahub and Permutations time limit per test 1 second memory limit per test 256 megabytes input sta ...

  2. codeforces 340E Iahub and Permutations(错排or容斥)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Iahub and Permutations Iahub is so happy ...

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

  4. CodeForces 340E Iahub and Permutations 错排dp

    Iahub and Permutations 题解: 令 cnt1 为可以没有限制位的填充数字个数. 令 cnt2 为有限制位的填充数字个数. 那么:对于cnt1来说, 他的值是cnt1! 然后我们对 ...

  5. CodeForces 340E Iahub and Permutations

    容斥原理,组合数. 找出有$cnt$个数字还有没放,那么总方案数就是$cnt!$. 总方案数里面包含了正确的和非正确的,我们需要将非正确的删去. 先删去$1$个数字$a[i]=i$的情况,发现会多删, ...

  6. CF341C. Iahub and Permutations [DP 排列]

    http://codeforces.com/contest/341/problem/C 题意: 有一个长度为n的排列a,其中有一些位置被替换成了-1.你需要尝试恢复这个排列,将-1替换回数字.求有多少 ...

  7. Codeforces340 E. Iahub and Permutations

    Codeforces题号:#340E 出处: Codeforces 主要算法:思维+DP 难度:4.8 题意: 有一个长度为$n$的排列(即各元素互不相同),其中有一些为-1.现要求将数填到这些-1上 ...

  8. CF 463D Gargari and Permutations [dp]

    给出一个长为n的数列的k个排列(1 ≤ n ≤ 1000; 2 ≤ k ≤ 5).求这个k个数列的最长公共子序列的长度 dp[i]=max{dp[j]+1,where j<i 且j,i相应的字符 ...

  9. CF 1093E Intersection of Permutations——CDQ分治

    题目:http://codeforces.com/contest/1093/problem/E 只能想到转化成查询一个区间里值在一个范围里的数的个数…… 没有想到这样适合用主席树套树状数组维护.不过据 ...

随机推荐

  1. 爬虫学习笔记(五) Beautiful Soup使用

    上篇博客说了正则表达式,但是正则学起来比较费劲,写的时候也不好写,这次说下Beautiful Soup怎么用,这个模块是用来解析html的,它操作很简单,用起来比较方便,比正则学习起来简单多了. 这是 ...

  2. USACO 4.1.1 麦香牛块 Beef McNuggets

    题目大意 给你\(n\)个数\(a_1, a_2 ... a_n\), 要你求最大的正整数\(m\)使得方程\(a_1 x_1 + a_2 x_2 + ... + a_n x_n = m\)无非负整数 ...

  3. 363. Max Sum of Rectangle No Larger Than K

    /* * 363. Max Sum of Rectangle No Larger Than K * 2016-7-15 by Mingyang */ public int maxSumSubmatri ...

  4. 发现一个直播录制工具you-get

    地址:https://github.com/soimort/you-get 截至到今天,支持的平台如下: Site URL Videos? Images? Audios? YouTube https: ...

  5. ios大文件存储

    I am using Erica Sadun's method of Asynchronous Downloads (link here for the project file: download) ...

  6. http各类攻击及tcpcopy工具

    1.专业的还得ixia.Spirent TestCenter等软硬件一体的 2.一般的使用软件的,安装在linux上使用 参考: 1.http://blog.csdn.net/wuzhimang/ar ...

  7. JavDroider的作品展示

    好久没有写博客了,很懊悔,尽管说实习和项目那边的任务有点多,可是我想每天抽出时间出来写一篇文章总结一下当天所习所得并不困难! 好了,今天以一篇个人作品介绍来又一次开启我的博客~ 实习单位的门户站点 一 ...

  8. maven springmvc-hibernate搭建以及源码下载

    见:https://blog.csdn.net/fengshizty/article/details/43635305 下载源码参见:http://www.cnblogs.com/ljy2013/p/ ...

  9. 决策树之 C4.5

    C4.5 是对 ID3 的一个优化,它依据信息增益率来进行属性选择. 关于决策树.请參见:http://blog.csdn.net/bone_ace/article/details/46299681 ...

  10. C++中的static关键字的总结(转)

    C++的static有两种用法:面向过程程序设计中的static和面向对象程序设计中的static.前者应用于普通变量和函数,不涉及类:后者主要说明static在类中的作用. 1.面向过程设计中的st ...