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 |
#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
*/