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. AI: DL方法与问题空间探索

    所谓问题的解决是生存参数空间的一种状态转移到另外一种状态,而目的状态恰好是主体所希望的.完成这种转换的一系列脚本变化过程叫做场景序列,也叫通路.驱动这一些列场景转换的主体参与过程,被称为主动执行.而主 ...

  2. CF319E Ping-Pong 线段树 + vector + 思维

    Code: #include<bits/stdc++.h> #define N 3000009 #define maxn 3000009 #define ll long long #def ...

  3. BZOJ 1577: [Usaco2009 Feb]庙会捷运Fair Shuttle 线段树 + 贪心

    escription 公交车一共经过N(1<=N<=20000)个站点,从站点1一直驶到站点N.K(1<=K<=50000)群奶牛希望搭乘这辆公交车.第i群牛一共有Mi(1&l ...

  4. eas之kingdeeUtils

        import java.sql.ResultSetMetaData;     import java.sql.SQLException;     import java.util.ArrayL ...

  5. Lua操作系统库、流、文件库

    Lua操作系统库.流.文件库 1.Lua中所有的操作系统库函数 (1)os.clock() --功能:返回执行该程序cpu花费的时钟秒数 (2)os.time(...) --按参数的内容返回一个时间值 ...

  6. String使用方式详细总结

    1.用双引号创建 2.用new String方式创建 3.双引号相加创建 4.两个new String相加时 5.两个引用相加时 6.双引号加new String创建或者new String加双引号创 ...

  7. 45.mapping建立、修改

    主要知识点 1.如何建立索引 2.修改mapping 3.测试mapping         一.如何建立索引 语法 PUT /website { "mappings": { &q ...

  8. 继续聊WPF——设置网格控件列标题的样式

    我很奇怪的是,微软那厮是怎么搞的,Blend里面居然不能编辑GridView的样式,十万般无奈之下,只好手写XAML来处理了. 要想知道一个控件的样式是如何设置,看控件类的定义很重要,我们来看看Gri ...

  9. java陷阱之spring事物管理导致锁无效

    模拟锁情况无效 1.创建一个表 SET NAMES utf8mb4; ; DROP TABLE IF EXISTS `demo`; CREATE TABLE `demo` ( `id` ) NOT N ...

  10. [下载]Oracle LOB字段编辑工具

    OraLobEditor 是Oracle LOB (CLOB, BLOB) 字段编辑工具. 查看.编辑LOB (CLOB, BLOB)字段(plain text, RTF, image, hex, h ...