2018 Changsha University of Science and Technology Programming Competition J - Cup

Title: Link: https://www.nowcoder.com/acm/contest/96/J
One day durong bought an infinite cup and n balls, and the labels were 1, 2, 3.. ....n, classmate durong suddenly thought of a question----if he put n balls in order,

That is, put it into the cup in the order of 1, 2, 3...n, and then take it out (note that you don't have to wait until all the balls are put in to take out the ball), and the order in which the ball is put in and taken out will be recorded. ,
durong wants to know, to satisfy that when the mth ball goes in, there are exactly k balls in the cup at this time, and then the remaining nm balls still need to be put in, and finally the balls in the cup must be taken out,

How many ways are there to put in and take out the ball, the answer may be very large, so the mod is 1e9+7.

 

kuangbing's blog

This topic is well explained

 

For n 0s, the number of arbitrary prefix 0s of m 1s is not less than 1 is C(n+m,n)-C(n+m,m-1).

The Cattelan number is a special case of n = m, and the general formula is C(2 * n, n) / (n + 1)

 

 1 #include<bits/stdc++.h>
 2 #define LL long long
 3 #define fi first
 4 #define se second
 5 #define mk make_pair
 6 #define pii pair<int,int>
 7 using namespace std;
 8 
 9 const int N=2e6+7;
10 const int M=1e4+7;
11 const int inf=0x3f3f3f3f;
12 const LL INF=0x3f3f3f3f3f3f3f3f;
13 const int mod=1e9 + 7;
14 
15 int n, m, k;
16 LL f[N], inv[N];
17 
18 LL fastPow(LL a, LL b) {
19     LL ans = 1;
20     while(b) {
21         if(b & 1) ans = ans * a % mod;
22         a = a * a % mod; b >>= 1;
23     }
24     return ans;
25 }
26 void init() {
27     f[0] = 1;
28     for(int i = 1; i < N; i++) {
29         f[i] = f[i - 1] * i % mod;
30     }
31     for(int i = N - 1; i >= 0; i--)
32         inv[i] = fastPow(f[i], mod - 2);
33 
34 }
35 
36 LL C(int n, int m) {
37     if(n < m) return 0;
38     return f[n] * inv[m] % mod * inv[n - m] % mod;
39 }
40 
41 LL getCatalan(int n, int m) {
42    return (C(n + m, n) - C(n + m, m - 1) + mod) % mod;
43 }
44 
45 int main() {
46     init();
47     int T; scanf("%d", &T);
48     while(T--) {
49         scanf("%d%d%d", &n, &m, &k);
50         if(m > n || k > m) {
51             puts("0");
52             continue;
53         }
54         LL ret1 = getCatalan(m - 1, m - k);
55         LL ret2 = getCatalan(n - (m - k), n - m);
56         printf("%lld\n", ret1 * ret2 % mod);
57     }
58     return 0;
59 }
60 /*
61 */

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325300002&siteId=291194637