题目:

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

分析:首先先确定固定位置的数是那几个暨C(M,K),然后在非[1,M]内还有N-M个数,在[1,M]内还有还有M-K个数,而且这M-K个数是应该处于错排的状态,我们需要做的是讨论外面的N-M个数中的参与错排的个数暨sum C[N-M,i]*D[M-K+i] (i=0...N-M]   D 为错排数,注意本题中D[0]=1,也就是说当M=K且外面参与错排的个数为0时有且只有一种方法。

1,组合数表可以通过递推C(n,m)=C(n-1,m-1)+C(n-1,m)预处理打印,不要学我暴力直接用C(m,n)的公式,自己YY算法。。。,

for(int i=0;i<=1000;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;
    }

2,找的一个板子,使用于n,m不大,但mod很大的时候:

typedef long long LL;
const int P = 1000000007;
LL f[1000001], v[1000001];
LL rp(LL now, int k)
{
    LL will = 1;
    for (; k; k >>= 1, now *= now, now %= P)
    {
        if (k & 1) will *= now, will %= P;
    }
    return will;
}
LL C(int n, int m)
{
    if(n < m) return 0;
    return f[n] * rp(f[m], P - 2) % P * rp(f[n - m], P - 2) % P;
}
void init()
{
    f[0] = 1; v[0] = 1;
    for (int i = 1; i <= 1000000; i++) //1e6以内的组合数
    {
        f[i] = f[i - 1] * i % P;
    }
}
int main()
{
    init();
    int n, m;
    scanf("%d%d", &m, &n);
    printf("%lld\n", C(m, n));
}

这道题的代码:

code:

#include<cstdio>
#include<cstring>
const long long  mod=1000000007;
const int MAXN=1000+5;
long long D[MAXN];
typedef long long LL;
const long long P = 1000000007;
LL f[1000001], v[1000001];
LL rp(LL now, int k)
{
    LL will = 1;
    for (; k; k >>= 1, now *= now, now %= P)
    {
        if (k & 1) will *= now, will %= P;
    }
    return will;
}
LL C(int n, int m)
{
    if(n < m) return 0;
    return f[n] * rp(f[m], P - 2) % P * rp(f[n - m], P - 2) % P;
}
void init()
{
    f[0] = 1; v[0] = 1;
    for (int i = 1; i <= MAXN; i++) //1e6以内的组合数
    {
        f[i] = f[i - 1] * i % P;
    }
    D[1]=0,D[2]=D[0]=1;
    for(int i=3;i<=MAXN;i++)
    {
        D[i]=1LL*(i-1)*(D[i-1]+D[i-2])%mod;
    }
}

int main(void){
    int T;scanf("%d",&T);
    int CASE=0;
    init();
    while(T--){
        int N,M,K;scanf("%d%d%d",&N,&M,&K);
        LL Cmk=C(M,K);
        LL res=0;
        for(int i=0;i<=N-M;++i){//枚举M外的参加错排数的个数
            res=(res+C(N-M,i)*D[M-K+i])%mod;
        }
        res=res*Cmk%mod;
        printf("Case %d: %lld\n",++CASE,(res+mod)%mod);
    }
}

lightoj1095 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. LightOJ 1095 Arrange the Numbers(容斥原理)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1095 题意:给出数字n,m,K(0<K<=m<=n).在n个 ...

  3. POJ-1430 Binary Stirling Numbers 组合数学

    这题是定义如下的一个数: S(0, 0) = 1; S(n, 0) = 0 for n > 0;S(0, m) = 0 for m > 0; S(n, m) = m S(n - 1, m) ...

  4. pku 3252 Round Numbers 组合数学 找规律+排列组合

    http://poj.org/problem?id=3252 看了discuss里面的解题报告才明白的,这个解题报告太强大了:http://poj.org/showmessage?message_id ...

  5. UVa 11481 (计数) Arrange the Numbers

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

  6. 如何将功能测试用例转为自动化脚本?

    如何设计自动化测试用例或脚本? 自动化始终遵循手动测试.通常,将在AUT上执行一轮或多轮手动测试.这意味着手动测试用例已经存在并且已经执行了至少一次. 例如,假设以下是您的手动测试用例.它只是登录到G ...

  7. 《算法入门经典大赛——培训指南》第二章考试

    UVa特别考试 UVa站点专门为本书设立的分类题库配合,方便读者提交: http://uva.onlinejudge.org/index.php?option=com_onlinejudge& ...

  8. 【CodeForces - 1153D】Serval and Rooted Tree(树形dp)

    题干: Now Serval is a junior high school student in Japari Middle School, and he is still thrilled on ...

  9. c++ 基数排序算法_基数排序算法– C / C ++实现的基础

    c++ 基数排序算法 Radix Sort Algorithm is a unique sorting algorithm that works on the basic principle of n ...

最新文章

  1. LAMP笔记之MySQL高阶篇(5)
  2. 在编程和算法领域,有哪些经典问题
  3. Java开发知识点!手把手讲解-一个复杂动效的自定义绘制
  4. Unity3D 2D游戏中寻径算法的一些解决思路
  5. Solr 查询时候关键期 编码问题
  6. php按需加载方式来增加程序的灵活度
  7. LAMP 系统性能调优,第 3 部分: MySQL 服务器调优(转)
  8. ASP.NET中Cookie编程的基础知识
  9. Kubernetes Ingress入门指南和实践练习
  10. vista中安装语言包出错解决
  11. 2019年终总结-如果这是一场电影
  12. [需求管理-9]:需求规格说明书SRS
  13. latex数学公式转换器
  14. c#进阶一:使用ILDASM来查看c#中间语言
  15. 2022年编程语言排名,官方数据来了,让人大开眼界。
  16. 用计算机算锐角三角比,锐角三角比中计算器使用方法ppt
  17. Java 创建pdf
  18. 婚姻对女人很重要,但远远不是我们的全部
  19. 饮用水铁离子超标,各种溶液铁离子超标去除工艺
  20. rust沙河游戏_逆水寒死不了!网易新年放大招,沙盒生存玩法的网游版rust?

热门文章

  1. Python3 assert函数
  2. 常见希腊字母及其读音
  3. 数据库的OLTP和OLAP区别
  4. 【stm32f407】CAN总线
  5. 几张图片解答区块链的技术应用
  6. inquirer.js_如何使用Inquirer.js
  7. 转:三星电子CEO:一切决策都要从认清自己开始
  8. 有效解决hive小文件过多问题
  9. 360 度评估中如何评价他人
  10. linux 云输入法下载,搜狗云输入法!