UVA - 11481 Arrange the Numbers

Consider this sequence {1, 2, 3, … , N}, as a initial sequence of firstN natural numbers. You can rearrange this sequence in many ways. Therewill be N! different arrangements. You have to calculate the number ofarrangement of first N natural numbers, where in first M (M<=N)positions, exactly K (K<=M) numbers are in its initial position.

 

Example:

 

For, N = 5, M = 3, K =2

 

You should count this arrangement {1, 4, 3, 2, 5}, here in first 3positions 1 is in 1st position and 3 in 3rd position. Soexactly 2 of its first 3 are in there initial position.

 

But you should not count this {1, 2, 3, 4, 5}.

 

Input

The first line ofinput is an integer T(T<=1000) that indicates the number of testcases. Next T line contains 3 integers each, N(1<=N<=1000), M,and K.

 

Output

For each case,output the case number, followed by the answer modulo 1000000007. Lookat the sample for clarification.

 

SampleInput                             Outputfor Sample Input

1
5 3 2

Case 1: 12

 


Problem Setter : Md. Arifuzzaman Arif

Special Thanks : Abdullah Al Mahmud, Jane Alam Jan

题意:可以把序列1-n任意重排,但重排后的前m个位置中恰好要有k个是不变的,输入整数n,m,k,输出重排数%1000000007.

思路:首先从前m个选出k作为不变的,然后剩下的n-k个再任意重排,也是从中选出i个来作为不变的,剩下的错排。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
typedef long long ll;
using namespace std;
const int maxn = 1005;
const ll mod = 1000000007;

ll dp[maxn], C[maxn][maxn];
int n, m, k;

void init() {
	memset(C, 0, sizeof(C));
	C[0][0] = 1;
	for (int i = 1; i < maxn; i++) {
		C[i][0] = C[i][i] = 1;
		for (int j = 1; j < i; j++)
			C[i][j] = (C[i-1][j] + C[i-1][j-1]) % mod;
	}

	dp[1] = 0, dp[0] = dp[2] = 1;
	for (int i = 3; i < maxn; i++)
		dp[i] = ((i - 1) * (dp[i-2] + dp[i-1]) % mod) %  mod;
}

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

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


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值