Codeforce 1288C. Two Arrays(DP組合數學,n個數選擇m個數,單調不遞減個數,排列組合打表N*N)

https://codeforces.com/problemset/problem/1288/Cios

Examplesc++

input數組

2 2

outputui

5

inputspa

10 1

outputcode

55

inputci

723 9

outputget

157557417

Noteinput

In the first test there are 5 suitable arrays:it

  • a=[1,1],b=[2,2]
  • a=[1,2],b=[2,2]
  • a=[2,2],b=[2,2]
  • a=[1,1],b=[2,1]
  • a=[1,1],b=[1,1]

題目大意:

給定一個數n和一個數m,讓構建兩個數組a和b知足條件,

1.數組中全部元素的取值在1~n之間,a和b數組長度是m。2. a數組是單調不遞減的,b數組是單調不遞增 3. 任意的位置i,有ai<=bi

問你有多少對這樣的數組

思路:

從n個數中任選m個數,這m個數從小到大排列,且可重複選取的方案數爲C(n+m-1,m)

轉化爲代碼:

#include<bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int maxn = 10001;
int dp[21][1001];
int main() {
	//freopen("in.txt", "r", stdin);
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	int n, m;
	cin >> n >> m;
	m *= 2;
	dp[1][0] = 1;
	for (int i = 1; i <= m; ++i)
		for (int j = 1; j <= n; ++j)
			dp[i][j] = (dp[i - 1][j] + dp[i][j - 1] ) % mod;
	long long ans = 0;
	for (int i = 1; i <= n; ++i)
		ans = (ans + dp[m][i]) % mod;
	cout << ans;
}
相關文章
相關標籤/搜索