C. Two Arrays,组合数学

Educational Codeforces Round 80 (Rated for Div. 2) C. Two Arrays

https://codeforces.com/contest/1288/problem/C
You are given two integers n and m. Calculate the number of pairs of arrays (a,b) such that:

the length of both arrays is equal to m;
each element of each array is an integer between 1 and n (inclusive);
ai≤bi for any index i from 1 to m;
array a is sorted in non-descending order;
array b is sorted in non-ascending order.
As the result can be very large, you should print it modulo 109+7.

思路:
之前想了个复杂度巨高的dp,有兴趣可以看这里https://blog.csdn.net/xing_mo/article/details/103982101

可以考虑将a,b数组连起来形成一个b[1],b[2],…,b[m],a[m],a[m-1],…,a[1]的数组,长度为2m且非严格递减,取值都在[1,n]之间(与原问题等价),就相当于有n个盒子排成一排,由他们的顺序决定所取的数字(即n个不同的盒子),然后将2m个相同的小球放进去,允许空盒子存在(将这些小球放的盒子标号从大到小连起来就组成了一个满足条件的数组),由隔板法得方案数为 C 2 m + n 1 n 1 C_{2m+n-1}^{n-1}

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int MOD = 1e9+7;
ll fac[1025],inv[1025];
inline int qpow(int a,int b)
{
	int ans = 1;
	while(b)
	{
		if(b&1) ans = 1ll * ans * a%MOD;
		a = 1ll * a * a % MOD;
		b >>= 1;
	}
	return ans;
}
inline void getInv(int k)
{
	fac[0] = 1;
	for(int i = 1;i <= k;++i)
		fac[i] = fac[i-1] * i % MOD; 
	inv[k] = qpow(fac[k],MOD-2);
	for(int i = k-1;i >= 0;--i)
		inv[i] = inv[i+1] * (i+1) % MOD;
}
int n,m;
int main()
{
	getInv(1020);
	while(~scanf("%d%d",&n,&m))
		printf("%lld\n",fac[2*m+n-1]*inv[2*m]%MOD*inv[n-1]%MOD);
	return 0;
}
发布了50 篇原创文章 · 获赞 3 · 访问量 3106

猜你喜欢

转载自blog.csdn.net/xing_mo/article/details/103999999