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+;
const int N = 1e5 + ;
LL dp[N];
int fvis[N];
int vis[N];
int cnt1, cnt2;
int main(){
int n, t;
scanf("%d", &n);
for(int i = ; i <= n; ++i){
scanf("%d", &t);
if(t == -) continue;
vis[t] = ;
fvis[i] = ;
}
for(int i = ; i <= n; ++i){
if(vis[i] + fvis[i] == ) ;
else if(!vis[i] && fvis[i]) cnt1++;
else if(vis[i] + fvis[i] == ) cnt2++;
}
dp[] = ;
// cout << cnt1 << " " << cnt2 << endl;
for(int i = ; i <= cnt1; ++i) dp[] = dp[] * i % mod;
for(int i = ; i <= cnt2; ++i){
dp[i] = cnt1 * dp[i-] % mod + (i-) * dp[i-] % mod;
if(i >= ) dp[i] += (i-) * dp[i-] % mod;
dp[i] %= mod;
// cout << dp[i] << endl;
}
cout << dp[cnt2] << endl;
return ;
}

CodeForces 340E Iahub and Permutations 错排dp的更多相关文章

  1. codeforces 340E Iahub and Permutations(错排or容斥)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Iahub and Permutations Iahub is so happy ...

  2. CodeForces 340E Iahub and Permutations

    容斥原理,组合数. 找出有$cnt$个数字还有没放,那么总方案数就是$cnt!$. 总方案数里面包含了正确的和非正确的,我们需要将非正确的删去. 先删去$1$个数字$a[i]=i$的情况,发现会多删, ...

  3. codeforces 341C Iahub and Permutations(组合数dp)

    C. Iahub and Permutations time limit per test 1 second memory limit per test 256 megabytes input sta ...

  4. 容斥原理--计算错排的方案数 UVA 10497

    错排问题是一种特殊的排列问题. 模型:把n个元素依次标上1,2,3.......n,求每一个元素都不在自己位置的排列数. 运用容斥原理,我们有两种解决方法: 1. 总的排列方法有A(n,n),即n!, ...

  5. Codeforces 888D Almost Identity Permutations:错排公式

    题目链接:http://codeforces.com/problemset/problem/888/D 题意: 给定n,k,问你有多少种1到n的排列,满足至少有n-k个a[i] == i. (4 &l ...

  6. Codeforces 888D: Almost Identity Permutations(错排公式,组合数)

    A permutation \(p\) of size \(n\) is an array such that every integer from \(1\) to \(n\) occurs exa ...

  7. Codeforces Round #198 (Div. 2) E. Iahub and Permutations —— 容斥原理

    题目链接:http://codeforces.com/contest/340/problem/E E. Iahub and Permutations time limit per test 1 sec ...

  8. hdu2068-RPG的错排-(dp递推式)

    去年看错排公式,死都看不懂,基础扎实之后再来看就略懂了. 公式: dp[ n ] = ( n-1 ) * ( dp[n-1] + dp[n-2] ) 解析公式:比如有n个元素,各对应n个正确位置,dp ...

  9. HDU 2048 神、上帝以及老天爷(递归,错排,dp,概率)

    中文题,错排,求概率,不解释,核心思路同 HDU 1465 错排简单思路可看:http://www.cnblogs.com/laiba2004/p/3235934.html //错排,但是我之前叫了几 ...

随机推荐

  1. Scala的常用小技巧

    1."RichString.java".stripSuffix(".java") == "RichString" "http:// ...

  2. Python实现批量处理扫描特定目录

    ## 简述在渗透测试中遇到相同CMS站点时,搞定一个站点,相当于拿了一个站群的通用漏洞,所以我们首先需要标注站点的CMS类型,根据要求编写如下脚本 ## 要求1.访问特定目录,如:站点特定 /cmsa ...

  3. Idea搭建Spring+SpringMvc+Mybatis框架集成项目

    1.新建maven项目 2.创建多模块 每个模块配置如父模块一样,除视图层 (视图层配置) 最后 common-通过模块,不依赖任何模块,有各种项目所需要用到的工具类 model- POJO.VO.D ...

  4. css常用代码块

    顶部固定导航栏 | css position: fixed; top: 0; left: 0; z-index: 9999; width: 100%; height: 48px; border-top ...

  5. 【POJ - 3616】Milking Time(动态规划)

    Milking Time 直接翻译了 Descriptions 贝茜是一个勤劳的牛.事实上,她如此​​专注于最大化她的生产力,于是她决定安排下一个N(1≤N≤1,000,000)小时(方便地标记为0. ...

  6. 记一次mysql主从同步因断电产生的不能同步问题 1236 1032

    背景: 项目新上线一个月,qa需要测试断电服务拉起,服务拉起成功后,发现mysql主从异常,以下是发现的问题以及解决方案 问题1: Slave_IO_Running:  No 一方面原因是因为网络通信 ...

  7. c# http Post Get 方法

    /// <summary> /// get方式访问webapi /// </summary> /// <param name="url">< ...

  8. bytedance专题

    一 挑战字符串 1 无重复字符的最长子串(见leetcode bug free) 2 最长公共前缀(见leetcode bug free) 3 字符串的排列 给定两个字符串 s1 和 s2,写一个函数 ...

  9. 《机器学习基石》---VC维

    1 VC维的定义 VC维其实就是第一个break point的之前的样本容量.标准定义是:对一个假设空间,如果存在N个样本能够被假设空间中的h按所有可能的2的N次方种形式分开,则称该假设空间能够把N个 ...

  10. 我的第一个CAD程序

    [步骤1]新建项目 启动Visual Studio 2010,然后选择一个C#类库,设置好名称和保存位置,点击[确定] [步骤2]添加引用文件AcMgd.dll和AcDbMgd.dll 首次使用时,[ ...