QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#282137#7873. Since A Lighthos_lyric#32 105ms6400kbC++145.6kb2023-12-11 14:23:072024-07-04 03:12:22

Judging History

你现在查看的是最新测评结果

  • [2024-07-04 03:12:22]
  • 评测
  • 测评结果:32
  • 用时:105ms
  • 内存:6400kb
  • [2023-12-11 14:23:07]
  • 提交

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")

////////////////////////////////////////////////////////////////////////////////
template <unsigned M_> struct ModInt {
  static constexpr unsigned M = M_;
  unsigned x;
  constexpr ModInt() : x(0U) {}
  constexpr ModInt(unsigned x_) : x(x_ % M) {}
  constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
  constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
  constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
  ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
  ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
  ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; }
  ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
  ModInt pow(long long e) const {
    if (e < 0) return inv().pow(-e);
    ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
  }
  ModInt inv() const {
    unsigned a = M, b = x; int y = 0, z = 1;
    for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; }
    assert(a == 1U); return ModInt(y);
  }
  ModInt operator+() const { return *this; }
  ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; }
  ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
  ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
  ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
  ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
  template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
  template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
  template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
  template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
  explicit operator bool() const { return x; }
  bool operator==(const ModInt &a) const { return (x == a.x); }
  bool operator!=(const ModInt &a) const { return (x != a.x); }
  friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};
////////////////////////////////////////////////////////////////////////////////

constexpr unsigned MO = 998244353;
using Mint = ModInt<MO>;

constexpr int LIM_INV = 200'010;
Mint inv[LIM_INV], fac[LIM_INV], invFac[LIM_INV];

void prepare() {
  inv[1] = 1;
  for (int i = 2; i < LIM_INV; ++i) {
    inv[i] = -((Mint::M / i) * inv[Mint::M % i]);
  }
  fac[0] = invFac[0] = 1;
  for (int i = 1; i < LIM_INV; ++i) {
    fac[i] = fac[i - 1] * i;
    invFac[i] = invFac[i - 1] * inv[i];
  }
}
Mint binom(Int n, Int k) {
  if (n < 0) {
    if (k >= 0) {
      return ((k & 1) ? -1 : +1) * binom(-n + k - 1, k);
    } else if (n - k >= 0) {
      return (((n - k) & 1) ? -1 : +1) * binom(-k - 1, n - k);
    } else {
      return 0;
    }
  } else {
    if (0 <= k && k <= n) {
      assert(n < LIM_INV);
      return fac[n] * invFac[k] * invFac[n - k];
    } else {
      return 0;
    }
  }
}


Mint brute(int N, int D) {
cerr<<"[brute] N = "<<N<<", D = "<<D<<endl;
  // created on day n
  vector<Mint> as(N + 1);
  for (int n = 0; n <= N; ++n) {
    as[n] = (n <= 2) ? 1 : (as[n - 1] + as[n - 2]);
  }
  Mint ans = 0;
  Mint bads[2] = {};
  // n remaining days
  for (int n = 0; n <= N; ++n) {
    vector<Mint> fs(D + 1, 0);
    for (int d = 0; d <= D; ++d) {
      /*
        [x^<=n] (x/(1-x^2))^d
        = [x^n] x^d(1+x) / (1-x^2)^(d+1)
      */
      if (n - d >= 0) {
        fs[d] = binom(d + ((n - d) >> 1), d);
      }
    }
    Mint all = 0, bad = 0;
    for (int d = 0; d <= D; ++d) all += fs[d] * fs[D - d];
    for (int d = 0; d <= D - 2; ++d) bad += fs[d] * fs[D - 2 - d];
    ans += as[N - n] * (all - bads[(n + 1) & 1]);
    bads[n & 1] += bad;
  }
  ans /= 2;
  return ans;
}

int main() {
  prepare();
  
  int N, D;
  for (; ~scanf("%d%d", &N, &D); ) {
    Mint ans = 0;
    if (N <= 5000 && D <= 5000) {
      ans = brute(N, D);
    } else {
      assert(false);
    }
    printf("%u\n", ans.x);
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 1
Accepted

Test #1:

score: 1
Accepted
time: 2ms
memory: 6400kb

input:

1 1

output:

1

result:

ok single line: '1'

Test #2:

score: 0
Accepted
time: 0ms
memory: 6100kb

input:

2 1

output:

2

result:

ok single line: '2'

Test #3:

score: 0
Accepted
time: 3ms
memory: 6108kb

input:

3 2

output:

3

result:

ok single line: '3'

Test #4:

score: 0
Accepted
time: 0ms
memory: 6388kb

input:

4 4

output:

5

result:

ok single line: '5'

Test #5:

score: 0
Accepted
time: 3ms
memory: 6388kb

input:

5 7

output:

2

result:

ok single line: '2'

Test #6:

score: 0
Accepted
time: 0ms
memory: 6356kb

input:

6 4

output:

41

result:

ok single line: '41'

Test #7:

score: 0
Accepted
time: 3ms
memory: 6200kb

input:

7 11

output:

2

result:

ok single line: '2'

Test #8:

score: 0
Accepted
time: 0ms
memory: 6112kb

input:

8 8

output:

175

result:

ok single line: '175'

Test #9:

score: 0
Accepted
time: 3ms
memory: 6068kb

input:

9 10

output:

298

result:

ok single line: '298'

Test #10:

score: 0
Accepted
time: 0ms
memory: 6132kb

input:

10 3

output:

392

result:

ok single line: '392'

Test #11:

score: 0
Accepted
time: 3ms
memory: 6360kb

input:

11 8

output:

3785

result:

ok single line: '3785'

Test #12:

score: 0
Accepted
time: 3ms
memory: 6192kb

input:

12 15

output:

1422

result:

ok single line: '1422'

Test #13:

score: 0
Accepted
time: 0ms
memory: 6108kb

input:

13 17

output:

2008

result:

ok single line: '2008'

Test #14:

score: 0
Accepted
time: 3ms
memory: 6104kb

input:

14 16

output:

21508

result:

ok single line: '21508'

Test #15:

score: 0
Accepted
time: 0ms
memory: 6108kb

input:

15 1

output:

1596

result:

ok single line: '1596'

Test #16:

score: 0
Accepted
time: 3ms
memory: 6108kb

input:

16 28

output:

29

result:

ok single line: '29'

Test #17:

score: 0
Accepted
time: 3ms
memory: 6112kb

input:

17 6

output:

98086

result:

ok single line: '98086'

Test #18:

score: 0
Accepted
time: 0ms
memory: 6104kb

input:

18 11

output:

1478534

result:

ok single line: '1478534'

Test #19:

score: 0
Accepted
time: 3ms
memory: 6108kb

input:

19 25

output:

250068

result:

ok single line: '250068'

Test #20:

score: 0
Accepted
time: 3ms
memory: 6108kb

input:

20 27

output:

355418

result:

ok single line: '355418'

Test #21:

score: 0
Accepted
time: 2ms
memory: 6084kb

input:

21 23

output:

13517834

result:

ok single line: '13517834'

Test #22:

score: 0
Accepted
time: 3ms
memory: 6392kb

input:

22 4

output:

315460

result:

ok single line: '315460'

Test #23:

score: 0
Accepted
time: 3ms
memory: 6108kb

input:

23 37

output:

18428

result:

ok single line: '18428'

Test #24:

score: 0
Accepted
time: 3ms
memory: 6384kb

input:

24 18

output:

647287901

result:

ok single line: '647287901'

Test #25:

score: 0
Accepted
time: 3ms
memory: 6108kb

input:

25 40

output:

136655

result:

ok single line: '136655'

Subtask #2:

score: 7
Accepted

Test #26:

score: 7
Accepted
time: 3ms
memory: 6100kb

input:

380 59

output:

718355613

result:

ok single line: '718355613'

Test #27:

score: 0
Accepted
time: 2ms
memory: 6384kb

input:

164 46

output:

353450103

result:

ok single line: '353450103'

Test #28:

score: 0
Accepted
time: 3ms
memory: 6136kb

input:

206 144

output:

910367339

result:

ok single line: '910367339'

Test #29:

score: 0
Accepted
time: 3ms
memory: 6108kb

input:

270 127

output:

78796015

result:

ok single line: '78796015'

Test #30:

score: 0
Accepted
time: 3ms
memory: 6180kb

input:

157 87

output:

705420296

result:

ok single line: '705420296'

Subtask #3:

score: 10
Accepted

Test #31:

score: 10
Accepted
time: 4ms
memory: 6128kb

input:

413 652

output:

170600118

result:

ok single line: '170600118'

Test #32:

score: 0
Accepted
time: 3ms
memory: 6108kb

input:

724 979

output:

677376486

result:

ok single line: '677376486'

Test #33:

score: 0
Accepted
time: 18ms
memory: 6076kb

input:

1667 1699

output:

147640784

result:

ok single line: '147640784'

Test #34:

score: 0
Accepted
time: 9ms
memory: 6136kb

input:

1980 517

output:

276583672

result:

ok single line: '276583672'

Test #35:

score: 0
Accepted
time: 24ms
memory: 6120kb

input:

2000 2000

output:

265422351

result:

ok single line: '265422351'

Subtask #4:

score: 14
Accepted

Test #36:

score: 14
Accepted
time: 105ms
memory: 6184kb

input:

4495 4498

output:

375585699

result:

ok single line: '375585699'

Test #37:

score: 0
Accepted
time: 65ms
memory: 6232kb

input:

4968 2196

output:

844161053

result:

ok single line: '844161053'

Test #38:

score: 0
Accepted
time: 72ms
memory: 6216kb

input:

3967 3143

output:

496535114

result:

ok single line: '496535114'

Test #39:

score: 0
Accepted
time: 49ms
memory: 6152kb

input:

2406 4205

output:

599052202

result:

ok single line: '599052202'

Test #40:

score: 0
Accepted
time: 73ms
memory: 6228kb

input:

3942 3259

output:

110001226

result:

ok single line: '110001226'

Subtask #5:

score: 0
Runtime Error

Test #41:

score: 0
Runtime Error

input:

158314621 32

output:


result:


Subtask #6:

score: 0
Runtime Error

Test #46:

score: 0
Runtime Error

input:

812922977 1762

output:


result:


Subtask #7:

score: 0
Runtime Error

Test #51:

score: 0
Runtime Error

input:

320076133 78121

output:


result: