Codeforces 340E Iahub and Permutations【思维+错排Dp】

351 篇文章 2 订阅

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个数,我们需要将所有的-1放置【1~N】中的一个数,使得最终是一个包含1~N所有元素的全排列,并且a【i】!=i;


思路:


很显然这是一个错排模型。

Hdu 2049就是这么一些个类型题。http://acm.hdu.edu.cn/showproblem.php?pid=2049


一、

求错排的方式是动态规划的去递推。

设定dp【i】表示的是已经错排了i个位子的方案数。


转移有两种方案:

我们把第i个元素放在位子k上。共有i-1种选择。

1.位子k上的元素放在位子i上,那么dp【i】=dp【i-2】*(i-1);

2.位子k上的元素不放在位子i上,那么dp【i】=dp【i-1】*(i-1);那么再接下来的下一步,第i个元素就相当于这个位子k上的元素了。


综上:dp【i】=(i-1)*(dp【i-1】+dp【i-2】);


二、那么回归这个问题上来。


①我们规定,一个位子上如果a【i】==-1&&数字i有填写过,那么这个位子我们设定为没有限制的位子。个数设定为x

②我们规定,一个位子上如果a【i】==-1&&数字i没有填写过。那么这个位子我们设定为有限制的位子。个数设定为y

③我们一开始先将没有限制的位子填好,如果存在x个位子,使得a【i】==-1&&数字i填写过,那么对应我们就有x个数字来填写这x个-1的位子。

那么一开始将这些没有限制的位子填好就有x!种方案。


④接下来设定dp【i】,也表示错排了i个元素的方案数。

那么dp【0】=x!.


同理对于错排有限制的位子上的转移就有:dp【i】=(i-1)*(dp【i-1】+dp【i-2】);

那么我们还可以使用无限制的位子上的元素来替换:dp【i】=x*dp【i-1】;

都是可行方案,那么过程维护统计一下即可。


Ac代码:


#include<stdio.h>
#include<string.h>
using namespace std;
#define ll __int64
int a[15000];
ll dp[150000];
const ll mod=1e9+7;
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        int tot=0;
        int x=0;
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            if(a[i]==-1)tot++;
        }
        for(int i=1;i<=n;i++)
        {
            if(a[i]!=-1)
            {
                if(a[a[i]]==-1)x++;
            }
        }
        int y=tot-x;
        dp[0]=1;
        for(ll i=1;i<=x;i++)
        {
            dp[0]*=i;
            dp[0]%=mod;
        }
        for(int i=1;i<=y;i++)
        {
            if(i-1>=0)dp[i]+=dp[i-1]*(i-1);
            if(i-1>=0)dp[i]+=x*dp[i-1];
            if(i-2>=0)dp[i]+=dp[i-2]*(i-1);
            dp[i]%=mod;
        }
        printf("%I64d\n",dp[y]);
    }
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值