QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#351300#6333. Festivals in JOI Kingdom 2JCY_100 ✓2505ms4688kbC++142.3kb2024-03-11 20:12:222024-03-11 20:12:23

Judging History

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

  • [2024-03-11 20:12:23]
  • 评测
  • 测评结果:100
  • 用时:2505ms
  • 内存:4688kb
  • [2024-03-11 20:12:22]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using i128 = __int128;
using u128 = unsigned __int128;
template <typename T>
void chkmax(T &x, const T &y) {
  if (x < y) x = y;
}
template <typename T>
void chkmin(T &x, const T &y) {
  if (y < x) x = y;
}
namespace brt {
int p;
ull coef;
void init(int _p) {
  p = _p;
  coef = (1ull << 62) / p;
}
int reduce(ll x) {
  x -= (((i128)x * coef) >> 62) * p;
  return x >= p ? x - p : x;
}
}  // namespace brt
int mod;
void inc(int &x, int y) {
  x += y;
  if (x >= mod) x -= mod;
}
int sub(int x, int y) {
  x -= y;
  return x < 0 ? x + mod : x;
}
void upd(int &x, ll y) { x = brt::reduce(x + y); }
int qpow(int x, int y) {
  int ret = 1;
  for (; y; y >>= 1, x = (ll)x * x % mod)
    if (y & 1) ret = (ll)ret * x % mod;
  return ret;
}
constexpr int MAXN = 2e4 + 10;
int n, f[2][MAXN][3], ext[MAXN * 2][2], fac[MAXN * 2], ifac[MAXN * 2];
int A(int x, int y) { return brt::reduce((ll)fac[x] * ifac[x - y]); }
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n >> mod;
  brt::init(mod);
  fac[0] = 1;
  for (int i = 1; i <= n * 2; ++i) fac[i] = (ll)fac[i - 1] * i % mod;
  ifac[n * 2] = qpow(fac[n * 2], mod - 2);
  for (int i = n * 2 - 1; i >= 0; --i) ifac[i] = (ll)ifac[i + 1] * (i + 1) % mod;
  int p = 0;
  f[0][0][2] = 1;
  for (int i = 2; i <= n * 2; ++i) {
    p ^= 1;
    memset(f[p], 0, sizeof(int) * (min(i, n * 2 - i) + 1) * 3);
    for (int j = 0, up = min(i - 1, n * 2 - i + 1); j <= up; ++j) {
      int cur = f[!p][j][0];
      if (cur) inc(f[p][j][2], cur);
      cur = f[!p][j][1];
      if (cur) {
        inc(f[p][j + 1][1], cur);
        inc(f[p][j][0], cur);
      }
      cur = f[!p][j][2];
      if (cur) {
        inc(f[p][j + 1][2], cur);
        upd(ext[i + j][0], (ll)cur * A(n * 2 - i, j));
        upd(ext[i + j - 1][1], (ll)brt::reduce((ll)cur * A(n * 2 - i, j - 1)) * j);
      }
    }
    inc(f[p][0][0], ext[i][0]);
    inc(f[p][0][1], ext[i][1]);
  }
  int all = 1;
  for (int i = n * 2 - 1; i >= 1; i -= 2) all = (ll)all * i % mod;
  cout << sub(all, f[p][0][0]) << "\n";
  return 0;
}
/*
g++ kingdom.cpp -o kingdom -std=c++14 -O2 -Wall -Wextra -Wshadow -g -fsanitize=address,undefined
*/

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 5
Accepted

Test #1:

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

input:

1 194903119

output:

0

result:

ok 1 number(s): "0"

Test #2:

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

input:

2 933234047

output:

0

result:

ok 1 number(s): "0"

Test #3:

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

input:

3 277793111

output:

2

result:

ok 1 number(s): "2"

Test #4:

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

input:

4 355321177

output:

28

result:

ok 1 number(s): "28"

Test #5:

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

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: 3604kb

input:

8 361605653

output:

1236922

result:

ok 1 number(s): "1236922"

Test #7:

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

input:

8 995512643

output:

1236922

result:

ok 1 number(s): "1236922"

Test #8:

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

input:

8 101102801

output:

1236922

result:

ok 1 number(s): "1236922"

Test #9:

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

input:

6 458322727

output:

4894

result:

ok 1 number(s): "4894"

Test #10:

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

input:

7 721691819

output:

73884

result:

ok 1 number(s): "73884"

Test #11:

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

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: 3576kb

input:

30 779092367

output:

686412377

result:

ok 1 number(s): "686412377"

Test #13:

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

input:

29 963995171

output:

128570082

result:

ok 1 number(s): "128570082"

Test #14:

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

input:

18 666092701

output:

185922458

result:

ok 1 number(s): "185922458"

Test #15:

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

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: 3664kb

input:

300 463478027

output:

89265245

result:

ok 1 number(s): "89265245"

Test #17:

score: 0
Accepted
time: 1ms
memory: 3572kb

input:

296 567610679

output:

406342763

result:

ok 1 number(s): "406342763"

Test #18:

score: 0
Accepted
time: 1ms
memory: 3620kb

input:

297 609090359

output:

128986577

result:

ok 1 number(s): "128986577"

Test #19:

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

input:

48 759569383

output:

635573072

result:

ok 1 number(s): "635573072"

Test #20:

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

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: 56ms
memory: 3736kb

input:

3000 752129633

output:

33058561

result:

ok 1 number(s): "33058561"

Test #22:

score: 0
Accepted
time: 56ms
memory: 3800kb

input:

2993 491173567

output:

343277625

result:

ok 1 number(s): "343277625"

Test #23:

score: 0
Accepted
time: 57ms
memory: 3812kb

input:

2993 783095563

output:

776085006

result:

ok 1 number(s): "776085006"

Test #24:

score: 0
Accepted
time: 1ms
memory: 3620kb

input:

327 399783431

output:

163535283

result:

ok 1 number(s): "163535283"

Test #25:

score: 0
Accepted
time: 6ms
memory: 3644kb

input:

1233 857060207

output:

422139845

result:

ok 1 number(s): "422139845"

Test #26:

score: 0
Accepted
time: 8ms
memory: 3696kb

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: 2505ms
memory: 4688kb

input:

20000 221054167

output:

39809956

result:

ok 1 number(s): "39809956"

Test #28:

score: 0
Accepted
time: 2487ms
memory: 4648kb

input:

19997 823974001

output:

267537750

result:

ok 1 number(s): "267537750"

Test #29:

score: 0
Accepted
time: 2492ms
memory: 4652kb

input:

19991 501689843

output:

16527909

result:

ok 1 number(s): "16527909"

Test #30:

score: 0
Accepted
time: 1283ms
memory: 4360kb

input:

14344 925452091

output:

212324779

result:

ok 1 number(s): "212324779"

Test #31:

score: 0
Accepted
time: 237ms
memory: 3904kb

input:

6098 507350869

output:

310480789

result:

ok 1 number(s): "310480789"

Test #32:

score: 0
Accepted
time: 193ms
memory: 3880kb

input:

5564 406069759

output:

105694337

result:

ok 1 number(s): "105694337"

Extra Test:

score: 0
Extra Test Passed