zoukankan      html  css  js  c++  java
  • cf-341C Iahub and Permutations

    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=2005;
    const ll mod=1e9+7;
    int n,X,Y,a[N];bool flag[N];
    ll f[N];
    int main(){
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
            if(~a[i]) flag[a[i]]=1;
        }
        for(int i=1;i<=n;i++){
            if(!(~a[i])){
                if(flag[i]) X++;
                else Y++;
            }
        }
        f[0]=1;
        for(int i=1;i<=X;i++) f[0]=f[0]*i%mod;
        for(int i=1;i<=Y;i++){
            f[i]=(X+i-1)*f[i-1]%mod;
            if(i>1){
                f[i]=(f[i]+(i-1)*f[i-2])%mod;
            }
        }
        printf("%I64d",f[Y]);
        return 0;
    }
  • 相关阅读:
    Portal 并查集(去重)
    Constructing Roads 最小生成树(prime()),注意边的处理方式
    More is better 并查集最原始 最有效的形式,用rank数组,(结构体)
    【集训试题】SiriusRen的卡牌 set
    【集训试题】exam 信心考 最小割
    [leetcode] Binary Tree Pruning
    [leetcode] Daily Temperatures
    [leetcode] Sort Characters By Frequency
    [leetcode] Subdomain Visit Count
    [leetcode] Number of Boomerangs
  • 原文地址:https://www.cnblogs.com/shenben/p/6391041.html
Copyright © 2011-2022 走看看