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数;



【完整代码】

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define lson l,m,rt<<1
  4. #define rson m+1,r,rt<<1|1
  5. #define LL long long
  6. #define rep1(i,a,b) for (int i = a;i <= b;i++)
  7. #define rep2(i,a,b) for (int i = a;i >= b;i--)
  8. #define mp make_pair
  9. #define pb push_back
  10. #define fi first
  11. #define se second
  12. #define ms(x,y) memset(x,y,sizeof x)
  13. #define Open() freopen("F:\\rush.txt","r",stdin)
  14. #define Close() ios::sync_with_stdio(0)
  15. typedef pair<int, int> pii;
  16. typedef pair<LL, LL> pll;
  17. const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
  18. const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
  19. const double pi = acos(-1.0);
  20. const int N = 50+5;
  21. const LL MOD = (int) 1e9 + 7;
  22. //head
  23. LL c[N][N],fac[N+N];
  24. class DerangementsDiv2
  25. {
  26. public:
  27. int count(int n, int m)
  28. {
  29. rep1(i, 1, 50)
  30. c[i][i] = c[i][0] = 1;
  31. rep1(i, 1, 50)
  32. rep1(j, 1, i - 1)
  33. c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % MOD;
  34. fac[0] = 1;
  35. rep1(i, 1, 100)
  36. fac[i] = (fac[i - 1] * i) % MOD;
  37. LL ans = fac[n + m],temp = 0,p = 1;
  38. rep1(i, 1, m) {
  39. temp += (p*c[m][i]%MOD + MOD) % MOD*fac[n + m - i] % MOD;
  40. p = -p;
  41. }
  42. ans = ((ans - temp)%MOD + MOD) % MOD;
  43. return (int) ans;
  44. }
  45. };

【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. SpringCloud学习笔记(13)----Spring Cloud Netflix之Hystrix断路器的隔离策略

    说明 : 1.Hystrix通过舱壁模式来隔离限制依赖的并发量和阻塞扩散 2. Hystrix提供了两种隔离策略:线程池(THREAD)和信号量隔离SEMAPHORE). 1. 线程池隔离(默认策略模 ...

  2. Web前端为什么这么火爆?

    Web前端为什么这么火爆? 互联网发展到今天,全球已有28.9亿互联网用户,中国有355万网站,6.5亿网民,13亿手机用户,5亿微信用户,当步入互联网+时代后,互联网已经越来越复杂,纷繁复杂的互联网 ...

  3. javascript中实现继承的几种方式

    javascript中实现继承的几种方式 1.借用构造函数实现继承 function Parent1(){ this.name = "parent1" } function Chi ...

  4. PHP XML To Array将XML转换为数组

    // Xml 转 数组, 包括根键,忽略空元素和属性,尚有重大错误 function xml_to_array( $xml ) { $reg = "/<(\\w+)[^>]*?& ...

  5. 查看centos7启动项

    [root@k8s-master ~]# chkconfig Note: This output shows SysV services only and does not include nativ ...

  6. 一片非常有趣的文章 三分钟读懂TT猫分布式、微服务和集群之路

    原文http://www.cnblogs.com/smallSevens/p/7501932.html#3782600 三分钟读懂TT猫分布式.微服务和集群之路   针对新手入门的普及,有过大型网站技 ...

  7. dva基本用法

    1. npm install -g dva-cli 全局安装dva.2. dva new myApp --demo 创建dva项目.3. cd myApp npm start 启动项目.4. 定义 m ...

  8. HTTP——学习笔记(6)https

    HTTP+加密+认证+完整性保护=HTTPS HTTP是一种新协议吗?: 不是,HTTPS只是HTTP通信接口部分用SSL和TLS协议代替而已 HTTP中,身处应用层的HTTP直接和TCP通信.而在使 ...

  9. [转载]深入JVM锁机制-synchronized

    转自:http://blog.csdn.net/chen77716/article/details/6618779,并加上少量自己的理解 目前在Java中存在两种锁机制:synchronized和Lo ...

  10. 一个leetcode解题报告类目,代码很简洁

    http://bookshadow.com/leetcode/ 里面的代码很简洁.可以看.