Codeforces 340E Iahub and permutations (wrong row or allowance)

Source: Internet
Author: User

Reprint Please specify source: 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's she's no more important for Iahub. When Iahub goes away, Iahubina comes to his office and sabotage he work.

The girl finds an important permutation for the. The permutation contains  n  distinct integers  a 1,  a 2, ...,   a n   (1≤ a i n ). She replaces some of permutation elements with-1 value as a revenge.

When the Iahub finds out of his important permutation was broken, he tries to recover it. The only thing he remembers on the permutation is it didn ' t has any fixed point. A fixed point for a permutation was an element ak which have value equal to K ( a k = k). Your job is to proof to iahub this trying to recover it isn't a good idea. Output the number of permutations which could be originally Iahub ' s important permutation, modulo 1000000007 (10 9 + 7).

Input

The first line contains integer n (2≤ n ≤2000). On the second line, there is n integers, representing Iahub ' s important permutation after Iahubina replaces Some values with-1.

It ' s guaranteed that there is no fixed points in the given permutation. Also, the given sequence contains at least both numbers-1 and each positive number occurs in the sequence at most once. It's guaranteed that there are at least one suitable permutation.

Output

Output A single integer, the number of ways Iahub could recover his permutation, modulo 1000000007 (9 + 7). /c8>

Sample Test (s) input
5
-1-1 4 3-1
Output
2
Note

For the first Test example there is permutations with no fixed points is [2, 5, 4, 3, 1] and [5, 1, 4, 3, 2]. Any other permutation would has at least one fixed point.

There is a sequence that asks you to change one of the 1 into a number from 1 to N, making it an arrangement of 1 to N, but requires that I be not put I, asking for several alternatives.

Analysis:

A typical mismatch problem, considering that some parts of its own position have been placed, but its own is not used, that is, the score is unrestricted arrangement.

Whether a part of the number is its own position or 1, but it is already in use, the number of these numbers must be the same as the previous number, and such numbers have been placed and are not considered.

Part of the number is that its own position has been placed, itself has been used, so this kind of number will not have an impact on the arrangement, also do not have to consider.

The rest of the score is either its own position or 1, which itself has not yet been used, i.e. the number of this part is a restricted arrangement.

Thus, we only need the first and fourth class numbers.

Suppose that the method for placing J with a restricted number is dp[j], with a total of TX unlimited numbers.

1. When placing a restricted number in the TX position corresponding to an unlimited number ... Think about it, this is equivalent to placing an unrestricted number in the position of a restricted number, then the original limit of the number becomes an unrestricted number, that is, the number of the limit is reduced by 1, and the number of unlimited number is unchanged, there are tx*dp[j-1] species. .

2. When a restricted number is placed on a limited number of j-1, and a[i]=j,a[j]=i, then there is a total of (j-1) *dp[j-2].

3. When placing a restricted number in the j-1 of a limited number of positions, and a[i]=j,a[j]! =i; (j-1) *dp[j-1].

So dp[j]=tx*dp[j-1]+ (j-1) *dp[j-2]+ (j-1) *dp[j-1];

Then, when the rest of the TX is placed, it is the factorial of TX.

Then, there is no then, the AC ...

======================

Of course, this problem can also be used to do, consider the number of a[i]==i, the total number to remove these are the answer.

1 //#####################2 //Author:fraud3 //Blog:http://www.cnblogs.com/fraud/4 //#####################5#include <iostream>6#include <sstream>7#include <ios>8#include <iomanip>9#include <functional>Ten#include <algorithm> One#include <vector> A#include <string> -#include <list> -#include <queue> the#include <deque> -#include <stack> -#include <Set> -#include <map> +#include <cstdio> -#include <cstdlib> +#include <cmath> A#include <cstring> at#include <climits> -#include <cctype> - using namespacestd; - #defineXinf Int_max - #defineINF 0X3FFFFFFF - #defineMP (x, y) make_pair (x, y) in #definePB (x) push_back (x) - #defineREP (x,n) for (int x=0; x<n; X + +) to #defineREP2 (X,L,R) for (int x=l; x<=r; X + +) + #defineDEP (x,r,l) for (int x=r; x>=l; x--) - #defineCLR (a,x) memset (a,x,sizeof (A)) the #defineIT iterator *typedefLong Longll; $typedef pair<int,int>PII;Panax Notoginsengtypedef vector<pii>VII; -typedef vector<int>VI; the Constll MOD =1000000007 ; + inta[10010]; All b[ .]; the BOOLvis[ .]; +ll dp[ .]; - intMain () $ { $Ios::sync_with_stdio (false); -     intN; -Cin>>N; theb[0]=1; -      for(intI=1; i<=n;i++) cin>>A[i];Wuyi      for(LL i=1;i< .; i++) b[i]= (b[i-1]*i)%MOD; the     inttx=0, ty=0; -      for(intI=1; i<=n;i++){ Wu         if(a[i]!=-1) vis[a[i]]=1; -     } About      for(intI=1; i<=n;i++){ $         if(a[i]==-1){ -             if(Vis[i]) tx++; -             Elsety++; -         } A     } +dp[0]=1; thedp[1]=tx*dp[0]%MOD; -      for(intI=2; i<=ty;i++){ $Dp[i]= (tx*dp[i-1]%mod+ (I-1) *dp[i-1]%mod+ (I-1) *dp[i-2]%MOD)%MOD; the     } thecout<<dp[ty]*b[tx]%mod<<Endl; the      the      -      in          the              the     return 0; About}
code June

Codeforces 340E Iahub and permutations (wrong row or allowance)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.