Let us consider the combined sequence:
Its length is . From the problem conditions, it follows that this sequence:
is sorted in nondecreasing order;
consists of integers from to .
Thus, the problem reduces to counting the number of nondecreasing sequences
Let us perform a change of variables:
Then all y_i \ge 0, and their sum is
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 x_1, x_2, ..., x_{2m}, and hence to exactly one pair of arrays .
By the classical formula for combinations with repetition, the number of such decompositions is C_{n+2m-1}^{2m} (see Eolymp problem ).
Example
In the first test, there are 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
Let us perform a change of variables:
Let us sum the variables:
This equation has solutions. In each of them, exactly one variable y_i equals , while all the others equal . Let us consider the correspondence between these solutions and the original sequences:
Algorithm implementation
Declare a constant and an array , where dp[n][k] = C_n^k.
#define MOD 1000000007
long long dp[3001][30];The Cnk function computes the value of the binomial coefficient C_n^k.
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 array.
memset(dp, -1, sizeof(dp));Compute and print the answer C_{n+2m-1}^{2m}.
printf("%d\n", Cnk(n + 2 * m - 1, 2 * m));