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. WebGL画点程序v3

    本文程序实现画一个点的任务,如下图.其中,点的颜色由Javascript传到片元着色器程序中. 整个程序包含两个文件,分别是: 1. HelloPoint3.html <!DOCTYPE HTM ...

  2. Swift Pointer 使用指南

    Overview C Syntax Swift Syntax Note const Type * UnsafePointer<Type> 指针可变,指针指向的内存值不可变. Type * ...

  3. 微信jssdk图片上传

    一.html页面如下: <div class="weui-cell"> <div class="weui-cell__hd"></ ...

  4. Windows上架设自己的ssh代理 — copSSH

    最近网上对ssh代理讨论的比较多, 主要是为了穿墙. 其实在自己的电脑上也可以架设ssh代理, 当然国内自己架的不具有FQ功能, 如果你有国外朋友或是远程win服务器…则可以用此方法架设私有代理. 其 ...

  5. 【Shell编程】Shell基本语法

    Shell 语法   Shell程序设计作为一种脚本语言,在Linux系统中有广泛的应用,本文记录了关于Shell程序设计的基础语法知识和常用命令,方便查询,熟练使用shell也需要经常实践,这对于完 ...

  6. Linux 中, 安装html转pdf工具:wkhtmltopdf

    wkhtmltopdf下载地址官网:https://wkhtmltopdf.org/downloads.html 进入到/opt文件夹下面,新建文件夹wkhtmltopdf,然后把下载好的wkhtml ...

  7. 通过请求接口的办法获得本设备IP以及IP地址

    获取本设备IP接口(搜狐) http://pv.sohu.com/cityjson?ie=utf-8 result Content: {    "cip": "58.21 ...

  8. mysql中的高级查询语句

    此随笔用到的数据全是来自  关于mysql中表关系的一些理解(一对一,一对多,多对多) 提及的    学院表,学生表,学生详情表,选课表,课程表 单标查询:(查看学生表的学生名单) select st ...

  9. 20150805-20150807 tradeDate-----python

    1.创建数据库(strategy).表(trade_date 交易日) create database strategy default character set utf8 collate utf8 ...

  10. 【ACM】poj_3981_字符串替换_201307271019

    字符串替换Time Limit: 1000MS  Memory Limit: 65536K Total Submissions: 8447  Accepted: 3988 Description 编写 ...