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

light oj 1095 - Arrange the Numbers排列组合(错排列)的更多相关文章

  1. Light oj 1095 - Arrange the Numbers (组合数学+递推)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1095 题意: 给你包含1~n的排列,初始位置1,2,3...,n,问你刚好固定 ...

  2. Light OJ 1095 Arrange the Numbers(容斥)

    给定n,m,k,要求在n的全排列中,前m个数字中恰好有k个位置不变,有几种方案?首先,前m个中k个不变,那就是C(m,k),然后利用容斥原理可得 ans=ΣC(m,k)*(-1)^i*C(m-k,i) ...

  3. Light OJ 1095

    题意: 给你 N 个数, 总共有 N! 种排列, 现在 要你统计前 M 个数 刚好 有K 个数 在原来的位置上 的排列个数 思路: 首先 M 中选 K C(m,k): 则 共 剩下 n - k 个数, ...

  4. lightoj 1095 - Arrange the Numbers(dp+组合数)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1095 题解:其实是一道简单的组合数只要推导一下错排就行了.在这里就推导一下错排 ...

  5. LightOJ - 1095 - Arrange the Numbers(错排)

    链接: https://vjudge.net/problem/LightOJ-1095 题意: Consider this sequence {1, 2, 3 ... N}, as an initia ...

  6. light oj 1095 组合数学

    #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> ...

  7. DataFactory使用和注意,排列组合

    DataFactory使用和注意 mysql 连接ODBC开放数据库连接(Open Database Connectivity,ODBC)驱动程序 生成数据:int不能用 Build a compos ...

  8. 自然语言处理(NLP) - 数学基础(1) - 排列组合

    正如我在<自然语言处理(NLP) - 数学基础(1) - 总述>一文中所提到的NLP所关联的概率论(Probability Theory)知识点是如此的多, 饭只能一口一口地吃了, 我们先 ...

  9. Java蓝桥杯——排列组合

    排列组合介绍 排列,就是指从给定n个数的元素中取出指定m个数的元素,进行排序. 组合,则是指从给定n个数的元素中仅仅取出指定m个数的元素,不考虑排序. 全排列(permutation) 以数字为例,全 ...

随机推荐

  1. nginx服务无法停止(Windows)

    本人一般停止nginx服务都是通过Windows自带的任务管理器来强制结束nginx进程实现的,如图 2.但是 这次我通过同样的方法来结束nginx服务,发现nginx的进程无法结束   3.首先我要 ...

  2. Git将一个项目同时从本地推送到GitHub和Gitee

    前言 博主是根据自己的情况写这篇博文的,每个人遇到的情况和需求可能不一样哈,所以初始的步骤也不一定一致,但是同时推送到Github和Gitee的步骤都会是一致滴! Gitee拉取Github仓库 直接 ...

  3. CodeSign error: no provisioning profile at path '/Users/zhht-2015/Library/MobileDevice/Provisioning Profiles/79693141-f98b-4ac4-8bb4-476c9475f265.mobileprovision'

    解决方法: 1.关闭Xcode,找到项目中的**.xcodeproj文件,点击右键,show package contents(打开包内容). 2.打开后找到project.pbxproj文件,用文本 ...

  4. python 判断文件的字符编码

    import chardet f = open(file='test1.txt', mode='rb') data = f.read() print(chardet.detect(data))

  5. mongo 的 中文社区的 文档还是 挺不错的

    英文文档也奉上: http://api.mongodb.com/csharp/2.2/html/T_MongoDB_Bson_Serialization_Attributes_BsonIdAttrib ...

  6. NOIP【2016普及组】 考后有感(买铅笔,回文日期,海港,魔法阵)

    普及组考试已落下大幕,但我们那扑通扑通等待成绩的心仍然无法平静,先来给四道题做一个总结: 一. 买铅笔 自评难度:1星 其实这道题没有什么难度,生命之题,满分必拿,100分,保底啦~\(≧▽≦)/~ ...

  7. Linux 系统常用管理命令(精简笔记)

    Linux是一套免费使用和自由传播的类Unix操作系统,下面的笔记是我从鸟菜中摘抄出来的重要命令,并进行了一定的排版,摒弃了一些用不到的乱七八糟的命令,目的是在生产环境中能够快速的定位并查询需要命令的 ...

  8. Java容器解析系列(12) LinkedHashMap 详解

    LinkedHashMap继承自HashMap,除了提供HashMap的功能外,LinkedHashMap还是维护一个双向链表(实际为带头结点的双向循环链表),持有所有的键值对的引用: 这个双向链表定 ...

  9. influxdb+Grafana+jmeter监控搭建

    安装InfluxDB InfluxDB的简介 InfluxDB 是用Go语言编写的一个开源分布式时序.事件和指标数据库,无需外部依赖. 类似的数据库有Elasticsearch.Graphite等.. ...

  10. DRF框架之使用Django框架完成后端接口(API)的定义

    学习DRF框架,首先我们就需要明白为什么要学习这个框架. 接下来我们就先用原生的Django框架来定义一个符合RESTful设计方法的接口(API). RESTful接口的需求如下: GET /boo ...