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. HDU_1907_基础博弈nim游戏

    John Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submis ...

  2. Jmeter JSON断言和响应断言的区别是什么?

    假设响应数据是{"code":0,"datas":{"informationStatus":1}} 响应断言:"code" ...

  3. 小程序viewflex布局的对齐不对的问题

    index.wxml: <view class="container"> <view class="nav-container"> &l ...

  4. 关于JsonArray与JsonObject的使用

    学习地址:http://blog.csdn.net/lishuangzhe7047/article/details/28880009 关于前台向后台传递数组(里面包含json格式) [{"i ...

  5. PAT_A1148#Werewolf - Simple Version

    Source: PAT 1148 Werewolf - Simple Version (20 分) Description: Werewolf(狼人杀) is a game in which the ...

  6. 下拉框处理(select)

    在UI自动化测试过程中,经常会遇到一些下拉框,我们有三种可选方式来操作下拉框. 第一种方法 基于webdriver的两次click,很容易出现问题,不建议使用.(由于部分下拉框在点击一次后,失去焦点再 ...

  7. 2019 gplt团体程序设计天梯赛总结

    分很菜… 以后写题一定记得把题意理清楚了再开始写. 模拟题还是大坑,代码还是写得不够多,代码量一大就写bug. 补题 l1-8 估值一亿的AI核心代码 补题链接:https://pintia.cn/p ...

  8. Problem 19

    Problem 19 You are given the following information, but you may prefer to do some research for yours ...

  9. js实现cookie有效期至当次日凌晨

    实际开发中有要求用户一些行为每天一次,次日开始重新回复功能,一般前端都是通过cookie来记住用户的操作,然后进行判断当日是否还有机会,这时候需要给存储的cookie值一个有效期,让次日自动失效,重新 ...

  10. springcloud 中文文档

    spring cloud 中文文档:https://springcloud.cc/spring-cloud-dalston.html spring cloud 中文网:https://springcl ...