Light OJ 1095 Arrange the Numbers(计数)

来源:互联网 发布:java模拟器安卓4.4.2版 编辑:程序博客网 时间:2024/05/05 04:38

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

 

分析:K个数位置不变,剩余m-k个数肯定参与错排,那么n-m个数里参与错排的个数枚举下就可以算出来了
#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<set>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )const int INF=0x3f3f3f3f;typedef long long LL;const int mod=1000000007;LL dp[1010],C[1010][1010];void init(){    C[1][1]=C[1][0]=C[0][0]=1;    for(int i=2;i<=1000;i++)    {        C[i][i]=C[i][0]=1;        for(int j=1;j<i;j++)            C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod;    }    dp[1]=0;dp[2]=1;dp[0]=1;    for(int i=3;i<=1000;i++)        dp[i]=1LL*(i-1)*(dp[i-1]+dp[i-2])%mod;}LL Cal(int n,int m,int k){    int x=n-m;    int y=m-k;    LL ans=0;    for(int i=0;i<=x;i++)       ans=(ans+C[x][i]*dp[y+i]%mod)%mod;    return ans*C[m][k]%mod;}int main(){    init();    int t,cas=1;    int n,m,k;    scanf("%d",&t);    while(t--)    {        scanf("%d%d%d",&n,&m,&k);        printf("Case %d: %lld\n",cas++,Cal(n,m,k));    }    return 0;}


0 0