Consider this sequence {1, 2, 3, . . . , N}, as a initial sequence of first N natural numbers. You can
earrange this sequence in many ways. There will be N! different arrangements. You have to calculate
the number of arrangement of first N natural numbers, where in first M (M ≤ N) positions, exactly
K (K ≤ M) numbers are in its initial position.
Example:
For, N = 5, M = 3, K = 2
You should count this arrangement {1, 4, 3, 2, 5}, here in first 3 positions 1 is in 1-st position and
3 in 3-rd position. So exactly 2 of its first 3 are in there initial position.
But you should not count this {1, 2, 3, 4, 5}.
Input
The first line of input is an integer T (T ≤ 1000) that indicates the number of test cases. Next T line
contains 3 integers each, N (1 ≤ N ≤ 1000), M, and K.
Output
For each case, output the case number, followed by the answer modulo 1000000007. Look at the sample
for clarification.
Sample Input
1
5 3 2
Sample Output
Case 1: 12

题意:给你 n,m,k,   表示a[i] = 1,2....,n 经过变换后->  前m个数中只有任意 k个数满足 i = a[i]问你方案数

题解:我们  先在前m个数中任意选k个数是满足不变的  即 C(m,k);

   再枚举后n-m个中有多少个数的位置是不变的,C(n−m,x),这样就有n−k−x个数为乱序排列。

   对于y个数乱序排序,我们考虑dp做法,假设已经 求出 y-1,y-2个数的乱序排序数,那么 dp[y] = (y-1)*(dp[y-1]+dp[y-2]);(详见上一题)

  1. //meek///#include<bits/stdc++.h>
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <algorithm>
  6. #include<iostream>
  7. #include<bitset>
  8. #include<vector>
  9. #include <queue>
  10. #include <map>
  11. #include <set>
  12. #include <stack>
  13. using namespace std ;
  14. #define mem(a) memset(a,0,sizeof(a))
  15. #define pb push_back
  16. #define fi first
  17. #define se second
  18. #define MP make_pair
  19. typedef long long ll;
  20.  
  21. const int N = +;
  22. const int M = ;
  23. const int inf = 0x3f3f3f3f;
  24. const ll MOD = ;
  25.  
  26. int n, m, k;
  27. ll dp[N], c[N][N];
  28. void init () {
  29. for (int i = ; i < N; i++) {
  30. c[i][] = c[i][i] = ;
  31. for (int j = ; j < i; j++)
  32. c[i][j] = (c[i-][j-] + c[i-][j]) % MOD;
  33. }
  34. dp[] = ;
  35. dp[] = ;
  36. dp[] = ;
  37. for (ll i = ; i < N; i++)
  38. dp[i] = ((dp[i-] + dp[i-]) % MOD * (i-)) % MOD;
  39. }
  40.  
  41. ll solve () {
  42. ll ans = ;
  43. int t = n - m;
  44. for(int i = ;i <= n-m; i++) ans += (c[t][i]*dp[n-k-i]), ans %= MOD;
  45. return (ans * c[m][k]) % MOD;
  46. }
  47. int main () {
  48. init();
  49. int cas = , T;
  50. scanf("%d", &T);
  51. while(T--) {
  52. scanf("%d%d%d", &n, &m, &k);
  53. printf("Case %d: %lld\n", cas++, solve());
  54. }
  55. return ;
  56. }

代码

UVA 11481 - Arrange the Numbers 数学的更多相关文章

  1. UVA 11481 Arrange the Numbers(组合数学 错位排序)

    题意:长度为n的序列,前m位恰好k位正确排序,求方法数 前m位选k个数正确排,为cm[m][k],剩余m - k个空位,要错排,这m - k个数可能是前m个数中剩下的,也可能来自后面的n - m个数 ...

  2. UVa 11481 Arrange the Numbers (组合数学)

    题意:给定 n,m,k,问你在 1 ~ n 的排列中,前 m 个恰好有 k 个不在自己位置的排列有多少个. 析:枚举 m+1 ~ n 中有多少个恰好在自己位置,这个是C(n-m, i),然后前面选出 ...

  3. UVA 11582 Colossal Fibonacci Numbers(数学)

    Colossal Fibonacci Numbers 想先说下最近的状态吧,已经考完试了,这个暑假也应该是最后刷题的暑假了,打完今年acm就应该会退了,但是还什么都不会呢? +_+ 所以这个暑假,一定 ...

  4. uva 10712 - Count the Numbers(数位dp)

    题目链接:uva 10712 - Count the Numbers 题目大意:给出n,a.b.问说在a到b之间有多少个n. 解题思路:数位dp.dp[i][j][x][y]表示第i位为j的时候.x是 ...

  5. UVA 10539 - Almost Prime Numbers(数论)

    UVA 10539 - Almost Prime Numbers 题目链接 题意:给定一个区间,求这个区间中的Almost prime number,Almost prime number的定义为:仅 ...

  6. light oj 1095 - Arrange the Numbers排列组合(错排列)

    1095 - Arrange the Numbers Consider this sequence {1, 2, 3 ... N}, as an initial sequence of first N ...

  7. UVa 11481 (计数) Arrange the Numbers

    居然没有往错排公式那去想,真是太弱了. 先在前m个数中挑出k个位置不变的数,有C(m, k)种方案,然后枚举后面n-m个位置不变的数的个数i,剩下的n-k-i个数就是错排了. 所以这里要递推一个组合数 ...

  8. POJ2402/UVA 12050 Palindrome Numbers 数学思维

    A palindrome is a word, number, or phrase that reads the same forwards as backwards. For example,the ...

  9. UVA 11461 - Square Numbers 数学水题

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

随机推荐

  1. orcherd 汉化

    点击这里下载汉化包,此汉化包是在是在前人的基础上精心整理修改的,后续汉化版本随时升级更新. Orchard汉化包 下载后解压缩后打开后看到如下文件夹(App_Data.Core.Modules.The ...

  2. C#中的Attribute

    最近用到了,所以静下心来找些资料看了一下,终于把这东西搞清楚了. 一.什么是Attribute 先看下面的三段代码: 1.自定义Attribute类:VersionAttribute [Attribu ...

  3. JavaScript AJAX stream 流式显示

      当使用AJAX进行信息交互的时候,如果服务器返回的信息比较大,那么相对于传送完成之后的统一显示,流式显示就比较友好了. 流式实现 原理就是设置定时器,定时的查看AJAX对象的状态并更新内容,如果传 ...

  4. 无法解决“Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed”与“Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed”之间的冲突。正在随意选择“Newtonsoft.Jso

    今天的程序莫名报错:  无法解决“Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed” ...

  5. linux下sort详解(sort对科学记数法的排序)

    1.参数解释 -t 设置分隔符 -k 设置比较域(列) -n 按数字比较 -g 科学记数法方式比较 -o 设置输出文件,与“>”相比可以设置输出到原文件,“>”会清空原文件 -r 降序(大 ...

  6. springboot常见应用属性

    # ===================================================================# COMMON SPRING BOOT PROPERTIES ...

  7. 搭建SpringMVC+MyBatis开发框架三

    新增spingmvc.xml配置 在WEB-INF下新增spingmvc.xml,主要是配置spring扫描的包:  <?xml version="1.0" encodin ...

  8. Careercup - Microsoft面试题 - 5428361417457664

    2014-05-11 03:37 题目链接 原题: You have three jars filled with candies. One jar is filled with banana can ...

  9. C#和asp.net执行外部EXE程序

    这两天研究下.Net的执行外部EXE程序问题,就是在一个程序里通过按钮或其他操作运行起来另外一个程序,需要传入参数,如用户名.密码之类(实际上很类似单点登录,不过要简单的多的多):总结如下: 1.CS ...

  10. Netsharp快速入门(之4) 基础档案(之C 实体建模 计量单位、商品、往来单位)

    作者:秋时 杨昶   时间:2014-02-15  转载须说明出处 3.3.2   基础档案建模 1.在基础档案项目,右击,选择新建包, 2.录入包的名称,录入名称.完成后点确定 3.3.2.1 计量 ...