CodeForces 340E Iahub and Permutations
容斥原理,组合数。
找出有$cnt$个数字还有没放,那么总方案数就是$cnt!$。
总方案数里面包含了正确的和非正确的,我们需要将非正确的删去。
先删去$1$个数字$a[i]=i$的情况,发现会多删,要加回两个数字$a[i]=i$的情况,发现会多加......就是一个容斥原理的过程。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c = getchar(); x = ;while(!isdigit(c)) c = getchar();
while(isdigit(c)) { x = x * + c - ''; c = getchar(); }
} const int maxn=;
LL c[maxn][maxn],f[maxn];
LL mod=1e9+;
int n,a[maxn],h[maxn]; LL MOD(LL a)
{
if(a>=) return a%mod;
LL ff=(-a)/mod+; a=a+ff*mod;
return a%mod;
} int main()
{
for(int i=;i<=;i++) c[i][]=;
for(int i=;i<=;i++)
for(int j=;j<=i;j++)
c[i][j]=(c[i-][j-]+c[i-][j])%mod;
f[]=; for(int i=;i<=;i++) f[i]=(LL)i*f[i-]%mod; scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i]!=-) h[a[i]]=;
} int cnt=; for(int i=;i<=n;i++) if(a[i]==-) cnt++; int sum=;
for(int i=;i<=n;i++)
{
if(h[i]) continue;
if(a[i]!=-) continue;
sum++;
} LL ans=f[cnt]; LL d=-;
for(int i=;i<=sum;i++)
{
LL tmp=MOD(d*c[sum][i]*f[cnt-i]);
ans=(ans+tmp)%mod; d=-d;
} printf("%lld\n",ans);
return ;
}
CodeForces 340E Iahub and Permutations的更多相关文章
- 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 错排dp
Iahub and Permutations 题解: 令 cnt1 为可以没有限制位的填充数字个数. 令 cnt2 为有限制位的填充数字个数. 那么:对于cnt1来说, 他的值是cnt1! 然后我们对 ...
- codeforces 341C Iahub and Permutations(组合数dp)
C. Iahub and Permutations time limit per test 1 second memory limit per test 256 megabytes input sta ...
- 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 ...
- cf-341C Iahub and Permutations
C. Iahub and Permutations time limit per test 1 second memory limit per test 256 megabytes input sta ...
- Iahub and Permutations(codeforces 314c)
题意:给出一组排列,某些位置不知道(-1),要求求出有多少种还原方式,使得所有a[i]!=i /* 这是一道关于排列的动态规划,这种体大都可以当作棋盘来做,如果把i这个数放到第j个位置,那么就将棋盘的 ...
- Codeforces340 E. Iahub and Permutations
Codeforces题号:#340E 出处: Codeforces 主要算法:思维+DP 难度:4.8 题意: 有一个长度为$n$的排列(即各元素互不相同),其中有一些为-1.现要求将数填到这些-1上 ...
- Codeforces Round #337 Alphabet Permutations
E. Alphabet Permutations time limit per test: 1 second memory limit per test: 512 megabytes input: ...
- Codeforces 463D Gargari and Permutations
http://codeforces.com/problemset/problem/463/D 题意:给出k个排列,问这k个排列的最长公共子序列的长度. 思路:只考虑其中一个的dp:f[i]=max(f ...
随机推荐
- 【C#基础知识】静态构造函数,来源于一道面试题的理解
看到园友的一道面试题,很好奇,测试了一下结果. public class A { public static int X=B.Y ; public A() { ++X; } } public clas ...
- General Structure of Quartz.NET and How To Implement It
General Structure of Quartz.NET and How To Implement It General Structure of Quartz.NET and How To ...
- MongoDB学习2
MongoDB学习(翻译2) C#驱动之LINQ教程 介绍 本教程涵盖了1.8发布版本对linq查询的支持. 开始本教程之前,你应该至少阅读下C#驱动教程关于C#驱动的介绍 快速开始 首先,添加下面命 ...
- Redis安装介绍
Redis安装介绍 一.Linux版本及配置 1. Linux版本:Red Hat Enterprise Linux 6虚拟机 2. 配置: 内存:1G:CPU:1核:硬盘:20G 二.Redis ...
- 关于SqlDataAdapter的使用
原文传送门 如果使用SqlDataAdapter来查询数据返回给DataSet或者DataTable时需要注意以下几点:1.如果SqlDataAdapter的SelectCommand的连接并没有打开 ...
- 限制某个进程只能在某个CPU上运行
首先可以调用GetSystemInfo查看有多少个CPU,再通过调用: BOOL WINAPI SetProcessAffinityMask( __in HANDLE hProcess, __in D ...
- MAC上的包管理利器
Homebrew- MAC上的包管理利器 2013-07-01 16:25 by 黄博文, 76 阅读, 0 评论, 收藏, 编辑 包管理器是神马东西?让我们看看wikipedia上的介绍. In s ...
- C#中 如何执行带GO的sql 语句
C#中是不允许执行带GO的sql 语句的, 如何做呢? 思路就是将带GO的sql语句转化为分段执行, 但在同一事务内执行. 扩展方法是个很不错的主意, 但是尽量不要影响原来的cmd的一些东东, 如 c ...
- Linux登录ssh携带密码
最近在使用ssh,但是每次都需要输入密码,想简单一步搞定,Google了一下,发现了一个比较笨的但实用的方法 1.安装 sshpass sudo apt-get install sshpass 2.携 ...
- linux下搭建svn本地服务器
在linux下搭建svn本地服务器可以很好的管理自己的代码,具体过程如下: # mkdir svn_local # cd svn_local # svnadmin create led_diplay ...