Problem Statement

You are given two ints: n and m.

Let D be the number of permutations of the set {1,2,…,n+m} such that the first m values are not fixed points of the permutation. Formally, we are interested in permutations p such that for each j between 1 and m, inclusive, we have p(j) != j.

Compute and return D modulo 1,000,000,007.

Definition

Class:

DerangementsDiv2

Method:

count

Parameters:

int, int

Returns:

int

Method signature:

int count(int n, int m)

(be sure your method is public)

Limits

Time limit (s):

2.000

Memory limit (MB):

512

Stack limit (MB):

512

Constraints

n will be between 0 and 50, inclusive.

m will be between 1 and 50, inclusive.

Examples

0)

0

2

Returns: 1

Here we are looking for permutations of {1, 2} such that p(1) != 1 and p(2) != 2. There is only one such permutation: the permutation (2, 1). In other words, the permutation p such that p(1) = 2 and p(2) = 1.

1)

2

1

Returns: 4

Here we are counting permutations of {1, 2, 3} such that p(1) != 1. There are four such permutations: (2, 1, 3), (2, 3, 1), (3, 1, 2), and (3, 2, 1). Here, (a, b, c) denotes a permutation p for which p(1) = a, p(2) = b, and p(3) = c.

2)

1

2

Returns: 3

This time we want permutations of {1, 2, 3} such that p(1) != 1 and p(2) != 2. The three such permutations are (2, 1, 3), (2, 3, 1), and (3, 1, 2).

3)

3

5

Returns: 21234

4)

20

27

Returns: 88437461

Watch out for integer overflow.

【题目链接】:

【题意】



给你两个整数n和m;

然后让你求1..n+m的一些满足以下要求的排列p的个数:

要求i从1..m满足p[i]!=i;

【题解】



容斥原理搞;

设ci表示1..m中有i个位置满足pi==i的方案数;

ci=C(m,i)*(n+m-i)!

则答案就为(n+m)!-c1∪c2∪c3…..∪cm

减号右边那个东西,用容斥原理搞

为了不重复计数;

先加上每一个位置都不同的方案,然后减去有两个位置不同的方案,然后加上有3个位置不同的方案,然后减去有4个位置不同的方案…



【Number Of WA】



0



【反思】



取模过程中会出现负数的话,要注意加上MOD数;



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0) typedef pair<int, int> pii;
typedef pair<LL, LL> pll; const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 50+5;
const LL MOD = (int) 1e9 + 7;
//head LL c[N][N],fac[N+N]; class DerangementsDiv2
{
public:
int count(int n, int m)
{
rep1(i, 1, 50)
c[i][i] = c[i][0] = 1;
rep1(i, 1, 50)
rep1(j, 1, i - 1)
c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % MOD;
fac[0] = 1;
rep1(i, 1, 100)
fac[i] = (fac[i - 1] * i) % MOD;
LL ans = fac[n + m],temp = 0,p = 1;
rep1(i, 1, m) {
temp += (p*c[m][i]%MOD + MOD) % MOD*fac[n + m - i] % MOD;
p = -p;
}
ans = ((ans - temp)%MOD + MOD) % MOD;
return (int) ans;
}
};

【SRM 717 DIV2 C】DerangementsDiv2的更多相关文章

  1. 【SRM 717 div2 B】LexmaxReplace

    Problem Statement Alice has a string s of lowercase letters. The string is written on a wall. Alice ...

  2. 【SRM 717 div2 A】 NiceTable

    Problem Statement You are given a vector t that describes a rectangular table of zeroes and ones. Ea ...

  3. 【Codeforces #312 div2 A】Lala Land and Apple Trees

    # [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...

  4. 【TP SRM 703 div2 250】AlternatingString

    Problem Statement A string of zeros and ones is called an alternating string if no two adjacent char ...

  5. 【TP SRM 703 div2 500】 GCDGraph

    Problem Statement You are given four ints: n, k, x, and y. The ints n and k describe a simple undire ...

  6. 【cf 483 div2 -C】Finite or not?(数论)

    链接:http://codeforces.com/contest/984/problem/C 题意 三个数p, q, b, 求p/q在b进制下小数点后是否是有限位. 思路 题意转化为是否q|p*b^x ...

  7. 【市场调研与分析】Intel发力移动安全领域——By Me at 20140613

                                                    [市场调研与分析]Intel发力移动安全领域                               ...

  8. 【疯狂造轮子-iOS】JSON转Model系列之二

    [疯狂造轮子-iOS]JSON转Model系列之二 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 上一篇<[疯狂造轮子-iOS]JSON转Model系列之一> ...

  9. 【疯狂造轮子-iOS】JSON转Model系列之一

    [疯狂造轮子-iOS]JSON转Model系列之一 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 之前一直看别人的源码,虽然对自己提升比较大,但毕竟不是自己写的,很容易遗 ...

随机推荐

  1. 对称平方数(to_string函数,stoi函数真香)

    题目描述 打印所有不超过n(n<256)的,其平方具有对称性质的数.如11*11=121. 输入描述: 无 输出描述: 每行一个数,表示对称平方数. 示例1 输入 复制 无 输出 复制 无解题思 ...

  2. 4----COM:a Generative Model for group recommendation(组推荐的一种生成模型)

    1.摘要: 组推荐的一个挑战性问题:因为不同组的成员就有不同的偏好,如何平衡这些组员的偏好是一个难以解决的问题. 在本文中,作者提出了一个COM的概率模型来建立组活动生成过程. 直觉上: 一个组中的用 ...

  3. 小程序自定义tabbar

    代码片段: wechatide://minicode/IUoCyemJ7D3d GitHub: https://github.com/WozHuang/Miniprogram-Demo/tree/ma ...

  4. BZOJ 1190 [HNOI2007]梦幻岛宝珠(背包)

    1190: [HNOI2007]梦幻岛宝珠 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1385  Solved: 798[Submit][Stat ...

  5. 通过force index了解的MySQL查询的性能优化

    查询是数据库技术中最常用的操作.查询操作的过程比较简单,首先从客户端发出查询的SQL语句,数据库服务端在接收到由客户端发来的SQL语句后, 执行这条SQL语句,然后将查询到的结果返回给客户端.虽然过程 ...

  6. 利用hive源码解析sql查了哪些表哪些字段

    在hiveserver2中使用了org.apache.hadoop.hive.ql.parse.BaseSemanticAnalyzer对抽象语法树(AST)进行语义分析,分析的过程可以得出hive查 ...

  7. cin详解(get()、getline()、clear()、sync())

    简述 在C中,输入输出用scanf和printf,在输入数据的同时还需说明数据的类型,如果输入数据较多,那就很麻烦,而C++中也有相似的东西cin和cout,它们来自C++的一个名叫" io ...

  8. IIC总线协议基础1

    文档类别 文档标识 IIC总线协议基础1 当前版本号 V0.2 作    者 Louis 完毕时间 2015-05-27 IIC总线协议基础1 IIC总线协议基础1. 1.            II ...

  9. 【Android UI】案例02 圆角边框、圆角背景的实现(shape)

    本文主要分享圆角边框与圆角背景的实现方式.该方式的实现,须要了解shape的使用.该部分的具体介绍,请阅读博客http://blog.csdn.net/mahoking/article/details ...

  10. blog_html

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE html> <html b:v ...