Light oj 1095 - Arrange the Numbers(错排)

1095 - Arrange the Numbers
Time Limit: 2 second(s)Memory Limit: 32 MB

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

 


PROBLEM SETTER: MD. ARIFUZZAMAN ARIF
SPECIAL THANKS: JANE ALAM JAN (SOLUTION, DATASET)




#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define bug printf("hihi\n")

#define eps 1e-12

typedef long long ll;
using namespace std;

#define mod 1000000007
#define N 1005

ll C[N][N];
ll f[N];

int n,m,k;

void inint()
{
    int i,j;
    for(i=0;i<N;i++)
        for(j=0;j<=i;j++)
           if(j==0) C[i][j]=1;
          else
            C[i][j]=(C[i-1][j]+C[i-1][j-1])%mod;

   f[0]=1;
   f[1]=0;
   f[2]=1;
   for(i=3;i<N;i++)
     f[i]=(ll)(i-1)*(f[i-1]+f[i-2])%mod;
}

ll solve()
{
    int i,j;
    ll sum=0;
    int t=n-m;
    for(i=0;i<=t;i++)
        sum=(sum+C[n-m][i]*f[m-k+i]%mod)%mod;
    return (ll)sum*C[m][k]%mod;
}

int main()
{
    inint();
    int i,j,t,ca=0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&k);
        printf("Case %d: %lld\n",++ca,solve());
    }
    return 0;

}

/*

2
5 3 2
10 6 3

*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值