E. Iahub and Permutations

Iahub is so happy about inventing bubble sort graphs that he's staying all day long at the office and writing permutations. Iahubina is angry that she is no more important for Iahub. When Iahub goes away, Iahubina comes to his office and sabotage his research work.

The girl finds an important permutation for the research. The permutation contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ n). She replaces some of permutation elements with -1 value as a revenge.

When Iahub finds out his important permutation is broken, he tries to recover it. The only thing he remembers about the permutation is it didn't have any fixed point. A fixed point for a permutation is an element ak which has value equal to k (ak = k). Your job is to proof to Iahub that trying to recover it is not a good idea. Output the number of permutations which could be originally Iahub's important permutation, modulo 1000000007 (109 + 7).

题意:给定一个数列,如果是-1则代表需要填,否则是一个固定数,

在所有-1处填入数字,使得得到的数列为n的一个排列,且各个位置的数与该位置的坐标编号不相同,求mod(1e9 + 7)意义下的方案数

似乎是道没什么新意的组合题,非常容易的想出随便容斥一下就好了?
显然发现排列这个性质十分弱,但编号不同的性质非常的强
考虑从排列入手,对于一个排列,我们去处理编号的问题
先不考虑编号问题,那么排列数实际上就是(-1的个数)k!,然后我们考虑去掉有一个编号重复的情况,这样有两个编号重复的情况就会被多减,然后加回去...以此类推大力容斥
对于处理有p个编号重复的情况,实际上就是选出个编号的方案数f*(k - p)!
考虑细节,需要先知道有哪些位置可以重复,由于题目求方案数的特性,我们不用在意哪个位置可以重复,只要考虑有多少个位置可以重复,这个东西可以非常快速的预处理O(n)出,再考虑选择方案数的问题,这个东西很显然是个组合数,预处理一下就好了,
最后O(n^2)容斥求解就好了

#include <bits/stdc++.h>
using namespace std; const long long Yn = 1e9 + ; bool flag[], num[];
long long power[], s[], C[]; long long Pow(long long a, long long b, long long mod) {
long long ans = ;
while (b) {
if (b & ) (ans *= a) %= mod;
b /= ;
(a *= a) %= mod;
}
return ans;
} int main() { long long ans = ;
int n, sum = , sum1 = ;
cin >> n;
memset(flag, ,sizeof flag);
for (int i = ; i <= n; ++i) {
cin >> s[i];
if (s[i] > )
flag[s[i]] = ;
else sum ++, num[i] = ;
} for (int i = ; i <= n; ++i)
if ((!flag[i]) && num[i]) sum1 ++; power[] = ;
for (int i = ; i <= n; ++i)
power[i] = (power[i - ] * i) % Yn; C[] = ;
for (long long i = ; i <= sum1; ++i) {
(C[i] = C[i - ] * (sum1 - i + )) %= Yn;
(C[i] *= Pow(i, Yn - , Yn)) %= Yn;
} int fff = ;
for (int i = ; i <= sum1; ++i)
(ans += (fff * power[sum - i] % Yn * C[i] % Yn + Yn)) %= Yn, fff *= -; cout << (ans % Yn + Yn) % Yn << endl; return ; }

Codeforces Round #198 (Div. 2)E题解的更多相关文章

  1. Codeforces Round #198 (Div. 2)A,B题解

    Codeforces Round #198 (Div. 2) 昨天看到奋斗群的群赛,好奇的去做了一下, 大概花了3个小时Ak,我大概可以退役了吧 那下面来稍微总结一下 A. The Wall Iahu ...

  2. # Codeforces Round #529(Div.3)个人题解

    Codeforces Round #529(Div.3)个人题解 前言: 闲来无事补了前天的cf,想着最近刷题有点点怠惰,就直接一场cf一场cf的刷算了,以后的题解也都会以每场的形式写出来 A. Re ...

  3. Codeforces Round #557 (Div. 1) 简要题解

    Codeforces Round #557 (Div. 1) 简要题解 codeforces A. Hide and Seek 枚举起始位置\(a\),如果\(a\)未在序列中出现,则对答案有\(2\ ...

  4. Codeforces Round #540 (Div. 3) 部分题解

    Codeforces Round #540 (Div. 3) 题目链接:https://codeforces.com/contest/1118 题目太多啦,解释题意都花很多时间...还有事情要做,就选 ...

  5. Codeforces Round #538 (Div. 2) (A-E题解)

    Codeforces Round #538 (Div. 2) 题目链接:https://codeforces.com/contest/1114 A. Got Any Grapes? 题意: 有三个人, ...

  6. Codeforces Round #531 (Div. 3) ABCDEF题解

    Codeforces Round #531 (Div. 3) 题目总链接:https://codeforces.com/contest/1102 A. Integer Sequence Dividin ...

  7. Codeforces Round #527 (Div. 3) ABCDEF题解

    Codeforces Round #527 (Div. 3) 题解 题目总链接:https://codeforces.com/contest/1092 A. Uniform String 题意: 输入 ...

  8. Codeforces Round #499 (Div. 1)部分题解(B,C,D)

    Codeforces Round #499 (Div. 1) 这场本来想和同学一起打\(\rm virtual\ contest\)的,结果有事耽搁了,之后又陆陆续续写了些,就综合起来发一篇题解. B ...

  9. Codeforces Round #545 (Div. 1) 简要题解

    这里没有翻译 Codeforces Round #545 (Div. 1) T1 对于每行每列分别离散化,求出大于这个位置的数字的个数即可. # include <bits/stdc++.h&g ...

随机推荐

  1. 【转】虚拟化(一):虚拟化及vmware产品介绍

    由于公司最近在做虚拟化监控,因此就需要把虚拟化方面的知识给学习总结一下,对于虚拟化的概念,摘自百度百科,如下:         虚拟化,是指通过虚拟化技术将一台计算机虚拟为多台逻辑计算机.在一台计算机 ...

  2. day002 计算机基础之 操作系统和编程语言的分类

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp今天主要针对计算机基础中的操作系统和编程语言的分类进行了讲解. 操作系统 &nbsp ...

  3. 谨慎调整内核参数:vm.min_free_kbytes

    内核参数:内存相关 内存管理从三个层次管理内存,分别是node, zone ,page; 64位的x86物理机内存从高地址到低地址分为: Normal DMA32 DMA.随着地址降低. [root@ ...

  4. 34.初识搜索引擎及timeout机制

    主要知识点 1.对搜索执行结果的说明 2.timeout机制讲解 一.对执行 GET /_search 的结果的说明 执行结果如下(只保留部分) { "took": 29, &qu ...

  5. js对对象的校验技巧,随时更新

    js中,字符串长度用length. 若不确定一个Map里,是否存在某个对象,则用underfind 去校验

  6. vscode简单使用介绍及个人常用扩展插件

    vscode全称Visual Studio Code 是微软开发一款IDE,官方地址 vscode  作为一款前端编辑器功能很强大,灵活,可以根据个人喜好选择扩展插件,而且还支持多种开发语言, 关于v ...

  7. OpenCV实现USM锐化与测试

    OpenCV实现USM锐化 [转]http://www.programdevelop.com/4964391/ USM (Unsharp masking) is a common operation ...

  8. sqlserver日志文件太大解决方法

    SQL Server 的事务日志意外增大或充满的处理方法 事务日志文件Transaction Log File是用来记录数据库更新情况的文件,扩展名为ldf. 在 SQL Server 7.0 和 S ...

  9. fzu 2138

    //假设n个人每个人都做对了两道题,那么要想获奖人数最少的话,那么做题数目肯定最多即全做对的,中间可能会小于零那么没有获奖的 #include<stdio.h> int main() { ...

  10. HDU 3934

    /*这是用的有旋转卡壳的思想. 首先确定i,j,对k进行循环,知道找到第一个k使得cross(i,j,k)>cross(i,j,k+1),如果k==i进入下一次循环. 对j,k进行旋转,每次循环 ...