11725. Two arrays
You are given two numbers n and m.
Find the number of pairs of arrays (a, b) such that:
·
both
arrays have a length of m;
·
each
element of both arrays is an integer from 1 to n inclusive;
·
ai ≤ bi for every index i from 1 to m;
·
array a is
sorted in non-decreasing order;
·
array b is
sorted in non-increasing order.
Since the result may be too large, compute
it modulo 109
+ 7.
Input. Two positive integers n and m (1 ≤ n ≤
1000, 1 ≤ m ≤ 10).
Output. Print a single number – the number of arrays a and
b that satisfy the conditions described above. Print the result modulo 109 + 7.
|
Sample input 1 |
Sample output
1 |
|
2 2 |
5 |
|
|
|
|
Sample input 2 |
Sample output
2 |
|
723 9 |
157557417 |
combinatorics
Algorithm analysis
Let us consider the combined sequence:
a1, a2, …, am,
bm, bm-1, …, b1
Its length is 2m. From the problem conditions, it follows that this sequence:
·
is sorted in nondecreasing order;
·
consists of integers from 1 to n.
Thus, the problem reduces to counting the number of nondecreasing sequences
1 ≤ x1
≤ x2 ≤ … ≤ x2m
≤ n
Let us perform a change of
variables:
y1 = x1 –
1,
y2 = x2 – x1,
y3 = x3 – x2,
…,
y2m = x2m
– x2m-1,
y2m+1 = n – x2m
Then all yi
≥ 0, and their sum is
y1 + y2 + … + y2m + y2m+1 = n – 1
Therefore, the problem
reduces to counting the number of ways to decompose the number n – 1 into 2m + 1 non-negative integers.
Each such decomposition corresponds to exactly one nondecreasing sequence x1, x2, …, x2m, and hence to exactly one
pair of arrays (a; b).
By the classical formula
for combinations with repetition, the number of such decompositions is
(see Eolymp problem 11551).
Example
In the first test, there are 5 valid
variants:
·
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]
The corresponding combined sequence
satisfies the inequalities
1 ≤ a1
≤ a2 ≤ b2 ≤ b1
≤ 2
Let us perform a change of
variables:
y1 = a1 –
1,
y2 = a2 – a1,
y3 = b2 – a2,
y4 = b1 – b2,
y5 = 2 – b1
Let us sum the variables:
y1 + y2 + … + y5 = 1, yi ≥ 0
This
equation has 5 solutions. In each of them, exactly one variable yi
equals 1, while all the others equal 0. Let us consider the correspondence
between these solutions and the original sequences:

Algorithm implementation
Declare
a constant and an array dp, where dp[n][k]
=
.
#define MOD 1000000007
long long dp[3001][30];
The Cnk function computes the value of the
binomial coefficient
.
long long Cnk(int n, int k)
{
if (n == k) return 1;
if (k == 0) return 1;
if (dp[n][k] != -1) return dp[n][k];
return dp[n][k] = (Cnk(n - 1, k - 1) + Cnk(n - 1, k)) % MOD;
}
The main part of the program. Read the input data.
scanf("%d %d", &n,
&m);
Initialize the dp array.
memset(dp, -1, sizeof(dp));
Compute and print the answer
.
printf("%d\n", Cnk(n + 2 * m - 1, 2 *
m));