Submission #2135787


Source Code Expand

/**
 * File    : E.cpp
 * Author  : Kazune Takahashi
 * Created : 2018-2-25 21:06:35
 * Powered by Visual Studio Code
 */

#include <iostream>
#include <iomanip>   // << fixed << setprecision(xxx)
#include <algorithm> // do { } while ( next_permutation(A, A+xxx) ) ;
#include <vector>
#include <string> // to_string(nnn) // substr(m, n) // stoi(nnn)
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map> // if (M.find(key) != M.end()) { }
#include <set>
#include <random> // random_device rd; mt19937 mt(rd());
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;

#define DEBUG 0 // change 0 -> 1 if we need debug.

typedef long long ll;

// const int dx[4] = {1, 0, -1, 0};
// const int dy[4] = {0, 1, 0, -1};

// const int C = 1e6+10;
// const ll M = 1000000007;

const int MAX_SIZE = 1000010;
const long long MOD = 998244353;

long long inv[MAX_SIZE];
long long fact[MAX_SIZE];
long long factinv[MAX_SIZE];

void init() {
  inv[1] = 1;
  for (int i=2; i<MAX_SIZE; i++) {
    inv[i] = ((MOD - inv[MOD%i]) * (MOD/i))%MOD;
  }
  fact[0] = factinv[0] = 1;
  for (int i=1; i<MAX_SIZE; i++) {
    fact[i] = (i * fact[i-1])%MOD;
    factinv[i] = (inv[i] * factinv[i-1])%MOD;
  }
}

long long C(int n, int k) {
  if (n >=0 && k >= 0 && n-k >= 0) {
    return ((fact[n] * factinv[k])%MOD * factinv[n-k])%MOD;
  }
  return 0;
}

long long power(long long x, long long n) {
  if (n == 0) {
    return 1;
  } else if (n%2 == 1) {
    return (x * power(x, n-1)) % MOD;
  } else {
    long long half = power(x, n/2);
    return (half * half) % MOD;
  }
}

long long gcm(long long a, long long b) {
  if (a < b) {
    return gcm(b, a);
  }
  if (b == 0) return a;
  return gcm(b, a%b);
}

ll N, K;

ll catalan(ll T, ll X, ll Y)
{
  return (C((X + Y) % MOD, X) + MOD - C((X + Y) % MOD, (Y + MOD - T) % MOD)) % MOD;
}

int main() {
  init();
  cin >> N >> K;
  if (N > K)
  {
    cout << 0 << endl;
    return 0;
  }
  ll ans = 0;
  for (ll R = 0; R <= K; R++)
  {
    ll B = K - R;
    if (R < B)
      continue;
    else if (R == B)
    {
      ans += catalan(R - N + 1, R, B - 1);
      ans %= MOD;
    }
    else if (B - R < N)
    {
      ans += catalan(R - N + 1, R, B);
      ans %= MOD;
    }
    else
    {
      ans += C(R + B, R);
      ans %= MOD;
    }
  }
  cout << ans << endl;
}

Submission Info

Submission Time
Task E - Ball Eat Chameleons
User kazunetakahashi
Language C++14 (Clang 3.8.0)
Score 1200
Code Size 2492 Byte
Status AC
Exec Time 32 ms
Memory 24056 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 1200 / 1200
Status
AC × 5
AC × 39
Set Name Test Cases
Sample s1.txt, s2.txt, s3.txt, s4.txt, s5.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, s1.txt, s2.txt, s3.txt, s4.txt, s5.txt
Case Name Status Exec Time Memory
01.txt AC 32 ms 24056 KB
02.txt AC 29 ms 23680 KB
03.txt AC 30 ms 23680 KB
04.txt AC 30 ms 23680 KB
05.txt AC 30 ms 23680 KB
06.txt AC 26 ms 23680 KB
07.txt AC 26 ms 23680 KB
08.txt AC 26 ms 23680 KB
09.txt AC 26 ms 23680 KB
10.txt AC 30 ms 23680 KB
11.txt AC 29 ms 23680 KB
12.txt AC 30 ms 23680 KB
13.txt AC 30 ms 23680 KB
14.txt AC 29 ms 23680 KB
15.txt AC 28 ms 23680 KB
16.txt AC 30 ms 23680 KB
17.txt AC 30 ms 23680 KB
18.txt AC 26 ms 23680 KB
19.txt AC 30 ms 23680 KB
20.txt AC 29 ms 23680 KB
21.txt AC 29 ms 23680 KB
22.txt AC 28 ms 23680 KB
23.txt AC 26 ms 23680 KB
24.txt AC 29 ms 23680 KB
25.txt AC 29 ms 23680 KB
26.txt AC 29 ms 23808 KB
27.txt AC 30 ms 23680 KB
28.txt AC 29 ms 23680 KB
29.txt AC 26 ms 23680 KB
30.txt AC 26 ms 23680 KB
31.txt AC 26 ms 23680 KB
32.txt AC 26 ms 23680 KB
33.txt AC 29 ms 23680 KB
34.txt AC 28 ms 23680 KB
s1.txt AC 26 ms 23680 KB
s2.txt AC 26 ms 23680 KB
s3.txt AC 26 ms 23680 KB
s4.txt AC 26 ms 23680 KB
s5.txt AC 27 ms 23680 KB