1095 - Arrange the Numbers

Consider this sequence {1, 2, 3 ... N}, as an initial sequence of first N natural numbers. You can rearrange this sequence in many ways. There will be a total of N! arrangements. You have to calculate the number of arrangement of first N natural numbers, where in first M positions; exactly K numbers are in their initial position.

For Example, N = 5, M = 3, K = 2

You should count this arrangement {1, 4, 3, 2, 5}, here in first 3 positions 1 is in 1st position and 3 in 3rd position. So exactly 2 of its first 3 are in there initial position.

But you should not count {1, 2, 3, 4, 5}.

Input

Input starts with an integer T (≤ 1000), denoting the number of test cases.

Each case contains three integers N (1 ≤ N ≤ 1000), M (M ≤ N), K (0 < K ≤ M).

Output

For each case, print the case number and the total number of possible arrangements modulo 1000000007.

Sample Input

Output for Sample Input

2

5 3 2

10 6 3

Case 1: 12

Case 2: 64320

分析:先从m个数中选出k个数待在自己位置上(c(m, k)), 然后在剩下n-k个数中,有n-m个数可以待在自己原来的位置上,故每局n-m个数有多少个数在自己原来的位置上,剩下的 n - k - i 个数 就是一个错排列了。

(错排列 : D[i] = (i-1) * (D[i-1] + D[i-2] ) ; )

代码:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<iostream>
#include<algorithm>

using namespace std;
#define N 1100
#define INF 0x3f3f3f3f
#define mod 1000000007

long long c[N][N], d[N];
void init()///组合数c(n, m)的的值存在c数组中。
{
c[0][0] = 1;

for(int i = 1; i < N; i++)
{
c[i][0] = c[i][i] = 1;

for(int j = 1; j < i; j++)
{
c[i][j] = (c[i-1][j-1] + c[i-1][j]) % mod;
}
}
d[1] = 0;
d[2] = d[0] = 1;
for(int i = 3; i < N; i++)
d[i] = (i-1) * (d[i-1] + d[i-2]) % mod;///错排列 : D[i] = (i-1) * (D[i-1] + D[i-2] )
}

long long solve(int n, int m, int k)
{
long long ans = 0;

for(int i = 0; i <= n - m; i++)///i表示在n-m数中选i个数呆在自己原来的位置。
ans = (ans + (c[n-m][i] * d[n-k-i]) % mod) % mod;///c[n-m][i] * d[n-k-i]表示在n-m数中选i个数呆在自己原来的位置,剩下n-k-i都不呆在自己位上的数错排列得到的排列数。

return ans * c[m][k] % mod;
}
int main()
{
int T, cas;
int n, m, k;
scanf("%d", &T);
cas = 0;
init();
while(T--)
{
cas++;

scanf("%d%d%d", &n, &m, &k);

long long ans = solve(n, m, k);

printf("Case %d: %lld\n", cas, ans);

}

return 0;
}

最新文章

  1. C#RSA算法实现+如何将公钥为XML格式转为PEM格式,给object-C使用
  2. SQL Pass北京举办第10次线下活动,欢迎报名
  3. backbone库学习-model
  4. php实现数字格式化,数字每三位加逗号的功能函数
  5. linux权限,所有者、所在组、其他组(其他人员),chmod,chown
  6. 10. Max Points on a Line
  7. CoreBluetooth - 中心模式
  8. js监听和观察者模式
  9. codeforces 55D. Beautiful numbers 数位dp
  10. 后台调用外部程序的完美实现(使用CreateDesktop建立隐藏桌面)
  11. 【剑指offer】约瑟夫环问题
  12. ubuntu 12.04英文版设置成中文版
  13. 《k8s-1.13版本源码分析》-源码调试
  14. Solr 16 - 增删改Solr中索引数据的几种方式 (在URL上或Web页面中操作)
  15. SQLserver数据库反编译生成Hibernate实体类和映射文件
  16. js面向对象自定义MyString()的构造器函数,实现内建String()属性和方法:
  17. golang使用 gzip压缩
  18. git之一: git基础
  19. Git 子模块:git submodule
  20. hdu 5691(状压DP) Sitting in Line

热门文章

  1. Jetbrains CLion 安装及配置详解
  2. 洛谷p1137 模拟退火
  3. 投影方式- Unity3D游戏开发培训
  4. python3默认参数陷阱
  5. [bzoj3926] [loj2137] [Zjoi2015] 诸神眷顾的幻想乡
  6. 把本地仓库同步到github上去
  7. NOI2.5 1817:城堡问题
  8. Python学习,第一课 - 基础学习
  9. 移动端ui框架
  10. Mybatisplus代码生成器主类CodeGenerator配置