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

本文探讨了一个典型的错排问题,通过动态规划方法解决了一项挑战性的编程任务。任务要求在给定部分缺失的排列中,寻找所有可能的排列方式,确保每个位置的数值都不等于其索引值。

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]);
    }
}




当前提供的引用内容并未提及关于Codeforces比赛M1的具体时间安[^1]。然而,通常情况下,Codeforces的比赛时间会在其官方网站上提前公布,并提供基于不同时区的转换工具以便参赛者了解具体开赛时刻。 对于Codeforces上的赛事而言,如果一场名为M1的比赛被计划举行,则它的原始时间一般按照UTC(协调世界时)设定。为了得知该场比赛在UTC+8时区的确切开始时间,可以遵循以下逻辑: - 前往Codeforces官网并定位至对应比赛页面。 - 查看比赛所标注的标准UTC起始时间。 - 将此标准时间加上8小时来获取对应的北京时间(即UTC+8)。 由于目前缺乏具体的官方公告链接或者确切日期作为依据,无法直接给出Codeforces M1比赛于UTC+8下的实际发生时段。建议定期访问Codeforces平台查看最新动态更新以及确认最终版程表信息。 ```python from datetime import timedelta, datetime def convert_utc_to_bj(utc_time_str): utc_format = "%Y-%m-%dT%H:%M:%SZ" bj_offset = timedelta(hours=8) try: # 解析UTC时间为datetime对象 utc_datetime = datetime.strptime(utc_time_str, utc_format) # 转换为北京时区时间 beijing_time = utc_datetime + bj_offset return beijing_time.strftime("%Y-%m-%d %H:%M:%S") except ValueError as e: return f"误:{e}" # 示例输入假设某场Codeforces比赛定于特定UTC时间 example_utc_start = "2024-12-05T17:35:00Z" converted_time = convert_utc_to_bj(example_utc_start) print(f"Codeforces比赛在北京时间下将是:{converted_time}") ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值