Natasha, Sasha and the Prefix Sums
We consider the contribution of each program into the largest prefix rightmost, we enumerate the maximum position and the maximum value,
The number of programs found on the left and the right can be represented by the number of Cattleya.
#include<bits/stdc++.h> using namespace std; const int N = 4007; const int mod = 998244853; int n, m; int c[N][N]; inline int calc(int n, int m) { if(n == -1 && m == 0) return 1; if(n < 0 || m < 0) return 0; int ans = c[n + m][n]; if(m) ans -= c[n + m][m - 1]; if(ans < 0) ans += mod; return ans; } int main() { for(int i = 0; i < N; i++) { for(int j = c[i][0] = 1; j <= i; j++) { c[i][j] = c[i - 1][j] + c[i - 1][j - 1]; if(c[i][j] >= mod) c[i][j] -= mod; } } int ans = 0; scanf("%d%d", &n, &m); for(int i = 1; i <= n + m; i++) { for(int j = 1; j <= n; j++) { if(i + j & 1) continue; int x = (i + j) / 2; int y = i - x; if(x < 0 || y < 0 || x > n || y > m) continue; int K = calc (x, y); x = n - x; y = m - y; IF (y <x) Continue ; right = 1LL * * calc right (y - 1 , x)% v; right = right 1LL * j *% v; ans + = right; IF (ANS> = v) ans - = v; } } Printf ( " % d \ n " , ANS); return 0 ; } / * * /