Codeforces Round #581 (Div. 2) E. Natasha, Sasha and the Prefix Sums

Natasha's favourite numbers are 𝑛n and 11, and Sasha's favourite numbers are 𝑚m and 1−1. One day Natasha and Sasha met and wrote down every possible array of length 𝑛+𝑚n+m such that some 𝑛n of its elements are equal to 11 and another 𝑚m elements are equal to 1−1. For each such array they counted its maximal prefix sum, probably an empty one which is equal to 00 (in another words, if every nonempty prefix sum is less to zero, then it is considered equal to zero). Formally, denote as 𝑓(𝑎)f(a) the maximal prefix sum of an array 𝑎1,,𝑙a1,…,l of length 𝑙0l≥0. Then:

 

𝑓(𝑎)=max(0,max1𝑖𝑙𝑗=1𝑖𝑎𝑗)f(a)=max(0,max1≤i≤l∑j=1iaj)

 

Now they want to count the sum of maximal prefix sums for each such an array and they are asking you to help. As this sum can be very large, output it modulo 998244853998244853.

Input

The only line contains two integers 𝑛n and 𝑚m (0𝑛,𝑚20000≤n,m≤2000).

Output

Output the answer to the problem modulo 998244853998244853.

Examples
input
Copy
0 2
output
Copy
0
input
Copy
2 0
output
Copy
2
input
Copy
2 2
output
Copy
5
input
Copy
2000 2000
output
Copy
674532367
Note

In the first example the only possible array is [-1,-1], its maximal prefix sum is equal to 00.

In the second example the only possible array is [1,1], its maximal prefix sum is equal to 22.

There are 66 possible arrays in the third example:

[1,1,-1,-1], f([1,1,-1,-1]) = 2

[1,-1,1,-1], f([1,-1,1,-1]) = 1

[1,-1,-1,1], f([1,-1,-1,1]) = 1

[-1,1,1,-1], f([-1,1,1,-1]) = 1

[-1,1,-1,1], f([-1,1,-1,1]) = 0

[-1,-1,1,1], f([-1,-1,1,1]) = 0

So the answer for the third example is 2+1+1+1+0+0=52+1+1+1+0+0=5.

 

 

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 #define ll long long
 6 #define clr(a, x) memset(a, x, sizeof(a))
 7 #define pii pair<int, int>
 8 #define pb push_back
 9 
10 const int INF = 0x3f3f3f3f;
11 const double EPS = 1e-9;
12 const int MOD = 998244853;
13 
14 int n, m;
15 ll k[2015][2015], C[4015][2015], dp[2015][2015];
16 int main() {
17     scanf("%d%d", &n, &m);
18     for (int i = 0; i <= n + m; ++i) {
19         C[i][0] = 1;
20         for (int j = 1; j <= min(i, 2000); ++j) {
21             C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % MOD;
22         }
23     }
24     for (int j = 0; j <= m; ++j) {
25         k[0][j] = 1;
26         for (int i = 1; i <= min(n, j); ++i) {
27             k[i][j] = (k[i - 1][j] + k[i][j - 1]) % MOD;
28         }
29     }
30     for (int i = 0; i <= n; ++i) {
31         dp[i][0] = i;
32         for (int j = 1; j <= m; ++j) {
33             if (0 == i) dp[i][j] = 0;
34             else dp[i][j] = (C[i + j - 1][j] + dp[i - 1][j] + dp[i][j -1] - (C[i + j - 1][i] - k[i][j - 1])) % MOD;
35         }
36     }
37     printf("%lld\n", (dp[n][m] + MOD) % MOD);
38     return 0;
39 }
View Code

 

posted @ 2019-09-05 16:44  hit_yjl  阅读(247)  评论(0编辑  收藏  举报