Number of combinations - go (x, y)

You are at the plane rectangular coordinate system origin (0,0), that go (x, y). Each operation can only move along the x-axis positive direction or y-axis direction, and can only be moved one length unit.
Request from (0,0) to (x, y) the number of different paths, if it does not reach the (x, y) is output -1.

Input formats:

Row of two integers x, y. Separated by a space. | X | ≤2000, | y | ≤2000

Output formats:

A line integer sum, expressed from (0,0) to (x, y) of the number of different paths, since the number may be too large, so you only need to 998,244,353 outputs
the results to the modulo. If does not reach (x, y) is output -1.

Sample input:

1 1

Sample output:

2

First of all: not specified x, y size relationship.
First points of the discussion

If less than zero xy -1

- this question: is not a number Cattleya (Cattleya number: only two movements, and during the first two pre-K operations must be at least a K operations)
EG (20, 2)
their understanding:
a total of 22 steps to go in this step 22, an upward jump to step 2 - 22 is withdrawn from the two, the number of combinations

#include<iostream>
#include<cstdio>
#include<cstring>
#define ll long long
using namespace std;
const int mod=998244353;
const int maxn=4100;
int c[maxn][maxn];

int main(){
    int x,y;
    cin >> x >> y;
    
    if(x<0 || y<0)  //只能沿正方向运动
        cout << -1 << endl;
    
    int n = x+y;
    for(int i=0; i<=n; i++) //初始
        c[i][0] = c[i][i] = 1;
    for(int i=2; i<=n; i++) //组合数的运算
        for(int j=1; j<=i/2; j++){ //算一半即可
            c[i][j] = (c[i-1][j] + c[i-1][j-1])%mod;
            c[i][i-j] = c[i][j];
        }
    
    cout << c[x+y][x] << endl;
    return 0;
}

Published 62 original articles · won praise 0 · Views 1756

Guess you like

Origin blog.csdn.net/jhckii/article/details/104344546