容斥原理,组合数。

找出有$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的更多相关文章

  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 错排dp

    Iahub and Permutations 题解: 令 cnt1 为可以没有限制位的填充数字个数. 令 cnt2 为有限制位的填充数字个数. 那么:对于cnt1来说, 他的值是cnt1! 然后我们对 ...

  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. 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 ...

  5. cf-341C Iahub and Permutations

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

  6. Iahub and Permutations(codeforces 314c)

    题意:给出一组排列,某些位置不知道(-1),要求求出有多少种还原方式,使得所有a[i]!=i /* 这是一道关于排列的动态规划,这种体大都可以当作棋盘来做,如果把i这个数放到第j个位置,那么就将棋盘的 ...

  7. Codeforces340 E. Iahub and Permutations

    Codeforces题号:#340E 出处: Codeforces 主要算法:思维+DP 难度:4.8 题意: 有一个长度为$n$的排列(即各元素互不相同),其中有一些为-1.现要求将数填到这些-1上 ...

  8. Codeforces Round #337 Alphabet Permutations

    E. Alphabet Permutations time limit per test:  1 second memory limit per test:  512 megabytes input: ...

  9. Codeforces 463D Gargari and Permutations

    http://codeforces.com/problemset/problem/463/D 题意:给出k个排列,问这k个排列的最长公共子序列的长度. 思路:只考虑其中一个的dp:f[i]=max(f ...

随机推荐

  1. .Net Attribute详解(一)

    .Net Attribute详解(一) 2013-11-27 08:10 by JustRun, 1427 阅读, 14 评论, 收藏, 编辑 Attribute的直接翻译是属性,这和Property ...

  2. mac 下安装oh my zsh

    1.直接从github上下载 git clone git://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh  2.拷贝到账户目录下 cp ~/. ...

  3. pydev去掉右边的预览栏minimap

    eclipse pydev: preferences > PyDev > Editor > Overview Ruler Minimap

  4. SVN版本冲突解决详解

    SVN版本冲突解决详解 分类: SVN(SubVersion)2009-11-23 15:45 27014人阅读 评论(12) 收藏 举报 svnsubversion服务器文档工作c 版本冲突原因: ...

  5. Linux 中 crontab 详解及示例

    cron是一个linux下的定时执行工具,可以在无需人工干预的情况下运行作业.由于Cron 是Linux的内置服务,但它不自动起来,可以用以下的方法启动.关闭这个服务: /sbin/service c ...

  6. GC算法精解(五分钟让你彻底明白标记/清除算法)

    GC算法精解(五分钟让你彻底明白标记/清除算法) 相信不少猿友看到标题就认为LZ是标题党了,不过既然您已经被LZ忽悠进来了,那就好好的享受一顿算法大餐吧.不过LZ丑话说前面哦,这篇文章应该能让各位彻底 ...

  7. Twitter Storm:单机环境的安装与配置

    Twitter Storm:单机环境的安装与配置 好久没写博客了,这一段时间一直被导师push着做毕业设计.由于目前的方向偏向于图像识别检索,毕设打算做一个基于分布式计算平台的图像检索系统,查阅相关资 ...

  8. MongoDB深圳用户组线下活动召集

    MongoDB线下用户组是由全世界MongoDB爱好者发起的不定期线下交流活动.目前全球有100多个MongoDB用户组,3万5千多爱好者参与.用户组活动的形式通常会有一到两个MongoDB相关的技术 ...

  9. HDU 2040 亲和数

    Problem Description 古希腊数学家毕达哥拉斯在自然数研究中发现,220的所有真约数(即不是自身的约数)之和为: 1+2+4+5+10+11+20+22+44+55+110=284. ...

  10. 百度地图API的自动定位路线查询

    功能如下:打开时自动定位到当前位置(浏览器可能会屏蔽自动定位功能,建议手机查看,或直接打开地址:http://1.jingcode.applinzi.com/test2.html),输入目的地点击搜索 ...