码迷,mamicode.com
首页 > 其他好文 > 详细

CodeForces 340E Iahub and Permutations 错排dp

时间:2019-05-13 22:50:18      阅读:98      评论:0      收藏:0      [点我收藏+]

标签:turn   ++i   const   get   std   alt   isp   ace   perm   

Iahub and Permutations

题解:

令 cnt1 为可以没有限制位的填充数字个数。

令 cnt2 为有限制位的填充数字个数。

 

那么:对于cnt1来说, 他的值是cnt1!

然后我们对cnt2进行dp。

对于任意一个新加进来的数字,我们可以令一个一个没有限制位数放在这里, 那么新加进来的数字 ≈ 没有限制位, 他的方案为 i-1 * dp[i-1]

, 然后我们如果把这个数字放到有限制位的数来说, 那么他的转移方程就和错排一样了。

代码:

技术图片
#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL _INF = 0xc0c0c0c0c0c0c0c0;
const LL mod =  (int)1e9+7;
const int N = 1e5 + 100;
LL dp[N];
int fvis[N];
int vis[N];
int cnt1, cnt2;
int main(){
    int n, t;
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i){
        scanf("%d", &t);
        if(t == -1) continue;
        vis[t] = 1;
        fvis[i] = 1;
    }
    for(int i = 1; i <= n; ++i){
        if(vis[i] + fvis[i] == 2) ;
        else if(!vis[i] && fvis[i]) cnt1++;
        else if(vis[i] + fvis[i] == 0) cnt2++;
    }
    dp[0] = 1;
//    cout << cnt1 << " " << cnt2 << endl;
    for(int i = 1; i <= cnt1; ++i) dp[0] = dp[0] * i % mod;
    for(int i = 1; i <= cnt2; ++i){
        dp[i] = cnt1 * dp[i-1] % mod + (i-1) * dp[i-1] % mod;
        if(i >= 2) dp[i] += (i-1) * dp[i-2] % mod;
        dp[i] %= mod;
//        cout << dp[i] << endl;
    }
    cout << dp[cnt2] << endl;
    return 0;
}
View Code

 

CodeForces 340E Iahub and Permutations 错排dp

标签:turn   ++i   const   get   std   alt   isp   ace   perm   

原文地址:https://www.cnblogs.com/MingSD/p/10859300.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!