QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#894690#6333. Festivals in JOI Kingdom 2liuziao100 ✓1735ms4096kbC++232.5kb2025-02-11 19:34:022025-02-11 19:34:02

Judging History

This is the latest submission verdict.

  • [2025-02-11 19:34:02]
  • Judged
  • Verdict: 100
  • Time: 1735ms
  • Memory: 4096kb
  • [2025-02-11 19:34:02]
  • Submitted

answer

#include <bits/stdc++.h>

// #define int int64_t

using i64 = int64_t;

const int kMaxN = 2e4 + 5;

int n, mod;
int fac[kMaxN * 2], ifac[kMaxN * 2], f[kMaxN][2];

struct Barrett {
  int64_t m, p;
  void init(int64_t mod) {
    m = ((__int128_t)1 << 64) / mod;
    p = mod;
  }
  int operator()(int64_t x) { return x - (((__int128_t)x * m) >> 64) * p; }
} Reduce;

int qpow(int bs, int64_t idx = mod - 2) {
  int ret = 1;
  for (; idx; idx >>= 1, bs = (int64_t)bs * bs % mod)
    if (idx & 1)
      ret = (int64_t)ret * bs % mod;
  return ret;
}

inline int add(int x, int y) { return (x + y >= mod ? x + y - mod : x + y); }
inline int sub(int x, int y) { return (x >= y ? x - y : x - y + mod); }
inline void inc(int &x, int y) { (x += y) >= mod ? x -= mod : x; }
inline void dec(int &x, int y) { (x -= y) < 0 ? x += mod : x; }

inline int getfac(int l, int r) {
  return l <= 0 ? 0 : 1ll * fac[r] * ifac[l - 1] % mod;
}

inline int up(int x, int k) {
  return getfac(x, x + k - 1);
}

void prework(int n = 4e4) {
  fac[0] = 1;
  for (int i = 1; i <= n; ++i) fac[i] = 1ll * i * fac[i - 1] % mod;
  ifac[n] = qpow(fac[n]);
  for (int i = n; i; --i) ifac[i - 1] = 1ll * i * ifac[i] % mod;
}

void dickdreamer() {
  std::cin >> n >> mod;
  Reduce.init(mod);
  prework(2 * n);
  f[1][1] = f[2][0] = 1;
  for (int i = 1; i < n; ++i) {
    for (int o1 = 0; o1 <= 1; ++o1) {
      if (!f[i][o1]) continue;
      for (int o2 = 0; o2 <= 1; ++o2) {
        int tmp = 1ll * f[i][o1] * ifac[2 * i - 2 + o1 - 1] % mod;
        for (int j = 0; j <= n - i - 2 + o2; ++j) {
          i64 coef = tmp;
          coef = Reduce(coef * fac[2 * i - 2 + o1 + j - 1]);
          if (!o1) coef = Reduce(coef * (j + 1));
          if (!o2) coef = Reduce(coef * (j + !o1 + !o2));
          inc(f[i + j + 2 - o2][o2], coef);
        }
      }
    }
  }
  int ans = 1;
  for (int i = 1; i <= 2 * n; i += 2) ans = 1ll * i * ans % mod;
  for (int i = 1; i <= n; ++i) {
    int j = n - i;
    dec(ans, 1ll * (j + 1) * up(2 * i - 2, j) % mod * f[i][0] % mod);
    dec(ans, 1ll * up(2 * i - 1, j) * f[i][1] % mod);
  }
  std::cout << ans << '\n';
}

int32_t main() {
#ifdef ORZXKR
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  std::ios::sync_with_stdio(0), std::cin.tie(0), std::cout.tie(0);
  int T = 1;
  // std::cin >> T;
  while (T--) dickdreamer();
  // std::cerr << 1.0 * clock() / CLOCKS_PER_SEC << "s\n";
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 5
Accepted

Test #1:

score: 5
Accepted
time: 0ms
memory: 3584kb

input:

1 194903119

output:

0

result:

ok 1 number(s): "0"

Test #2:

score: 5
Accepted
time: 0ms
memory: 3584kb

input:

2 933234047

output:

0

result:

ok 1 number(s): "0"

Test #3:

score: 5
Accepted
time: 0ms
memory: 3712kb

input:

3 277793111

output:

2

result:

ok 1 number(s): "2"

Test #4:

score: 5
Accepted
time: 0ms
memory: 3584kb

input:

4 355321177

output:

28

result:

ok 1 number(s): "28"

Test #5:

score: 5
Accepted
time: 0ms
memory: 3712kb

input:

5 306636893

output:

358

result:

ok 1 number(s): "358"

Subtask #2:

score: 5
Accepted

Dependency #1:

100%
Accepted

Test #6:

score: 5
Accepted
time: 0ms
memory: 3584kb

input:

8 361605653

output:

1236922

result:

ok 1 number(s): "1236922"

Test #7:

score: 5
Accepted
time: 0ms
memory: 3584kb

input:

8 995512643

output:

1236922

result:

ok 1 number(s): "1236922"

Test #8:

score: 5
Accepted
time: 1ms
memory: 3712kb

input:

8 101102801

output:

1236922

result:

ok 1 number(s): "1236922"

Test #9:

score: 5
Accepted
time: 0ms
memory: 3712kb

input:

6 458322727

output:

4894

result:

ok 1 number(s): "4894"

Test #10:

score: 5
Accepted
time: 1ms
memory: 3712kb

input:

7 721691819

output:

73884

result:

ok 1 number(s): "73884"

Test #11:

score: 5
Accepted
time: 0ms
memory: 3584kb

input:

7 370629137

output:

73884

result:

ok 1 number(s): "73884"

Subtask #3:

score: 27
Accepted

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Test #12:

score: 27
Accepted
time: 0ms
memory: 3712kb

input:

30 779092367

output:

686412377

result:

ok 1 number(s): "686412377"

Test #13:

score: 27
Accepted
time: 0ms
memory: 3584kb

input:

29 963995171

output:

128570082

result:

ok 1 number(s): "128570082"

Test #14:

score: 27
Accepted
time: 0ms
memory: 3712kb

input:

18 666092701

output:

185922458

result:

ok 1 number(s): "185922458"

Test #15:

score: 27
Accepted
time: 0ms
memory: 3712kb

input:

14 671243719

output:

623913899

result:

ok 1 number(s): "623913899"

Subtask #4:

score: 14
Accepted

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Test #16:

score: 14
Accepted
time: 1ms
memory: 3584kb

input:

300 463478027

output:

89265245

result:

ok 1 number(s): "89265245"

Test #17:

score: 14
Accepted
time: 0ms
memory: 3584kb

input:

296 567610679

output:

406342763

result:

ok 1 number(s): "406342763"

Test #18:

score: 14
Accepted
time: 0ms
memory: 3584kb

input:

297 609090359

output:

128986577

result:

ok 1 number(s): "128986577"

Test #19:

score: 14
Accepted
time: 0ms
memory: 3584kb

input:

48 759569383

output:

635573072

result:

ok 1 number(s): "635573072"

Test #20:

score: 14
Accepted
time: 1ms
memory: 3712kb

input:

99 298873033

output:

285340078

result:

ok 1 number(s): "285340078"

Subtask #5:

score: 36
Accepted

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

100%
Accepted

Test #21:

score: 36
Accepted
time: 40ms
memory: 3712kb

input:

3000 752129633

output:

33058561

result:

ok 1 number(s): "33058561"

Test #22:

score: 36
Accepted
time: 39ms
memory: 3840kb

input:

2993 491173567

output:

343277625

result:

ok 1 number(s): "343277625"

Test #23:

score: 36
Accepted
time: 39ms
memory: 3840kb

input:

2993 783095563

output:

776085006

result:

ok 1 number(s): "776085006"

Test #24:

score: 36
Accepted
time: 1ms
memory: 3584kb

input:

327 399783431

output:

163535283

result:

ok 1 number(s): "163535283"

Test #25:

score: 36
Accepted
time: 7ms
memory: 3712kb

input:

1233 857060207

output:

422139845

result:

ok 1 number(s): "422139845"

Test #26:

score: 36
Accepted
time: 5ms
memory: 3712kb

input:

1114 600227447

output:

598509129

result:

ok 1 number(s): "598509129"

Subtask #6:

score: 13
Accepted

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

100%
Accepted

Dependency #5:

100%
Accepted

Test #27:

score: 13
Accepted
time: 1733ms
memory: 4096kb

input:

20000 221054167

output:

39809956

result:

ok 1 number(s): "39809956"

Test #28:

score: 13
Accepted
time: 1731ms
memory: 4096kb

input:

19997 823974001

output:

267537750

result:

ok 1 number(s): "267537750"

Test #29:

score: 13
Accepted
time: 1735ms
memory: 4096kb

input:

19991 501689843

output:

16527909

result:

ok 1 number(s): "16527909"

Test #30:

score: 13
Accepted
time: 889ms
memory: 3968kb

input:

14344 925452091

output:

212324779

result:

ok 1 number(s): "212324779"

Test #31:

score: 13
Accepted
time: 161ms
memory: 3712kb

input:

6098 507350869

output:

310480789

result:

ok 1 number(s): "310480789"

Test #32:

score: 13
Accepted
time: 135ms
memory: 3840kb

input:

5564 406069759

output:

105694337

result:

ok 1 number(s): "105694337"

Extra Test:

score: 0
Extra Test Passed