CodeForces 340E Iahub and Permutations 错排dp
题解:
令 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的更多相关文章
- codeforces 340E Iahub and Permutations(错排or容斥)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Iahub and Permutations Iahub is so happy ...
- CodeForces 340E Iahub and Permutations
容斥原理,组合数. 找出有$cnt$个数字还有没放,那么总方案数就是$cnt!$. 总方案数里面包含了正确的和非正确的,我们需要将非正确的删去. 先删去$1$个数字$a[i]=i$的情况,发现会多删, ...
- codeforces 341C Iahub and Permutations(组合数dp)
C. Iahub and Permutations time limit per test 1 second memory limit per test 256 megabytes input sta ...
- 容斥原理--计算错排的方案数 UVA 10497
错排问题是一种特殊的排列问题. 模型:把n个元素依次标上1,2,3.......n,求每一个元素都不在自己位置的排列数. 运用容斥原理,我们有两种解决方法: 1. 总的排列方法有A(n,n),即n!, ...
- Codeforces 888D Almost Identity Permutations:错排公式
题目链接:http://codeforces.com/problemset/problem/888/D 题意: 给定n,k,问你有多少种1到n的排列,满足至少有n-k个a[i] == i. (4 &l ...
- Codeforces 888D: Almost Identity Permutations(错排公式,组合数)
A permutation \(p\) of size \(n\) is an array such that every integer from \(1\) to \(n\) occurs exa ...
- 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 ...
- hdu2068-RPG的错排-(dp递推式)
去年看错排公式,死都看不懂,基础扎实之后再来看就略懂了. 公式: dp[ n ] = ( n-1 ) * ( dp[n-1] + dp[n-2] ) 解析公式:比如有n个元素,各对应n个正确位置,dp ...
- HDU 2048 神、上帝以及老天爷(递归,错排,dp,概率)
中文题,错排,求概率,不解释,核心思路同 HDU 1465 错排简单思路可看:http://www.cnblogs.com/laiba2004/p/3235934.html //错排,但是我之前叫了几 ...
随机推荐
- Chrome 跨域 disable-web-security 关闭安全策略
谷歌浏览器暂时关闭跨域. 当遇到以下情况,则可以简单的使用 关闭Chrome 安全策略跨域 开发时跨域,上线后,部署在一个域名下没有跨域问题 开发时,临时解决跨域问题 只有开发时用这个,其他时候,就不 ...
- Core CLR 自定义的Host官方推荐的一种形式(第一种)
.Net Core CLR提供两种Host API访问 托管代码的形式,按照微软官方的说法,一种是通过CoreClr.DLL来直接调用托管生成的DLL程序集,另外一种是通过CoreClr里面的C导出函 ...
- SWT 注意事项
一:GridData (1) 将 GridData 的 widthHint 设置为0,可以解决控件大小会随着这其默认值长度大小而改变的问题.
- Java 设计模式 – Observer 观察者模式
目录 [隐藏] 1 代码 1.1 观察者接口: 1.2 被观察者: 1.3 观众类 : 1.4 电影类: 1.5 效果如下: 代码 说明都在注释: 观察者接口: package ObserverMod ...
- spark 入门教程合集
看到一篇不错的 spark 入门教程的合集,在此记录一下 http://www.cnblogs.com/shishanyuan/p/4699644.html
- Activiti6系列(2)- 运行和编译
前言 Activiti6.0在官网已经无法下载了,需要在Github上下载. 下载地址: https://github.com/Activiti/Activiti/releases/download/ ...
- HTML/CSS:block,inline和inline-block概念和区别
总体概念 block和inline这两个概念是简略的说法,完整确切的说应该是 block-level elements (块级元素) 和 inline elements (内联元素).block元素通 ...
- java中什么是继承笔记
继承 怎样实现继承:1,先提取共有的属性和方法,放到一个类里,这个叫父类.基类.超类 2.编写子类 修饰符 class 子类名 extends 父类名 好处:提高代码的复用性 子类怎么去 ...
- Cookie&Session
Cookie&Session 背景:Cookie和Session的原理.作用及如何设置和相关面试. 一.诞生背景 HTTP是无状态的,即服务器无法知道两个请求是否来自同一个浏览器,也就是服务器 ...
- 一段代码分清global和nonlocal
废话不多说,直接代码啊~~~ a=999 b=99999 def test1(): a=888 b=88888 print('a={}'.format(a)) print('b={}'.format( ...