QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#505217#6333. Festivals in JOI Kingdom 2hos_lyric87 179ms146808kbC++148.7kb2024-08-04 22:34:212024-08-04 22:34:21

Judging History

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

  • [2024-08-04 22:34:21]
  • 评测
  • 测评结果:87
  • 用时:179ms
  • 内存:146808kb
  • [2024-08-04 22:34:21]
  • 提交

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")


////////////////////////////////////////////////////////////////////////////////
// Barrett
struct ModInt {
  static unsigned M;
  static unsigned long long NEG_INV_M;
  static void setM(unsigned m) { M = m; NEG_INV_M = -1ULL / M; }
  unsigned x;
  ModInt() : x(0U) {}
  ModInt(unsigned x_) : x(x_ % M) {}
  ModInt(unsigned long long x_) : x(x_ % M) {}
  ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
  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) {
    const unsigned long long y = static_cast<unsigned long long>(x) * a.x;
    const unsigned long long q = static_cast<unsigned long long>((static_cast<unsigned __int128>(NEG_INV_M) * y) >> 64);
    const unsigned long long r = y - M * q;
    x = r - M * (r >= 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; }
};
unsigned ModInt::M;
unsigned long long ModInt::NEG_INV_M;
// !!!Use ModInt::setM!!!
////////////////////////////////////////////////////////////////////////////////

using Mint = ModInt;

constexpr int LIM_INV = 100'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;
    }
  }
}


namespace exper {
int N;
int tot;
vector<int> freq;
vector<int> L, R;

void check() {
  int opt = 0, grd = 0;
  {
    int l = 2*N;
    for (int i = N; --i >= 0; ) if (R[i] < l) {
      ++opt;
      l = L[i];
    }
  }
  {
    int r = -1;
    for (int i = 0; i < N; ++i) if (r < L[i]) {
      ++grd;
      r = R[i];
    }
  }
  if (opt == grd) {
if(N==4&&opt==2){
 for(int x=0;x<2*N;++x)cerr<<x<<" ";
 cerr<<endl;
 for(int i=0;i<N;++i)cerr<<string(2*L[i],' ')<<'|'<<string(2*(R[i]-L[i])-1,'-')<<'|'<<endl;
 cerr<<endl;
}
    ++tot;
    ++freq[opt];
  }
}

void dfs(int i, int yet) {
  if (i == N) {
    check();
  } else {
    L[i] = __builtin_ctz(yet);
    for (R[i] = L[i] + 1; R[i] < 2*N; ++R[i]) if (yet & 1 << R[i]) {
      dfs(i + 1, yet ^ 1 << L[i] ^ 1 << R[i]);
    }
  }
}

void run() {
  for (N = 1; N <= 8; ++N) {
    tot = 0;
    freq.assign(N + 1, 0);
    L.resize(N);
    R.resize(N);
    dfs(0, (1 << (2*N)) - 1);
    cerr << N << ": " << tot << " " << freq << endl;
  }
}
}  // exper
/*
1: 1 [0, 1]
2: 3 [0, 2, 1]
3: 13 [0, 6, 6, 1]
4: 77 [0, 24, 40, 12, 1]
5: 587 [0, 120, 312, 134, 20, 1]
6: 5501 [0, 720, 2820, 1598, 332, 30, 1]
7: 61251 [0, 5040, 29088, 20888, 5502, 690, 42, 1]
8: 790103 [0, 40320, 337680, 300096, 95622, 15052, 1276, 56, 1]
*/


/*
  min cost flow potential
       +------------+               +-----------+     
       |            |               |           |     
       v            |               v           |     
  m <- m -> ... -> m-1 <- m-1  ...  1 -> ... -> 0 <- 0
  leftmost i: i-th right-end of right-end greedy solution
  
  how many ways to insert left-end? (right-end: +1)
  
    +--------+  +------+  +---+  
    |        |  |      |  |   |  
  --v--------@---------@------@--
  
    +--------+  +------+  +---+  
    |        |  |      |  |   |  
  --v---@---------@---------@----
      | |  |      |  |      |    
      +-+  +------+  +------+    
  
  +1 +2 +2 +1
  
  m := (solution size)
  choose u[m-1], ..., u[0] \in {0, 1}
  ~~> 1+u[m-1], 1+u[m-1]+u[m-2], ..., 1+u[1]+u[0], 0+u[0]
  *2 for u[i+1] = 1 && u[i] = 1
*/
Mint slow(int N) {
  constexpr int MAX_N = 3010;
  // constexpr int MAX_N = 310;
  static Mint dp[MAX_N][2 * MAX_N][2];
  // static Mint dp[MAX_N][MAX_N][2 * MAX_N][2];
// for(int m=0;m<=N;++m)
  for (int n = 0; n <= N; ++n) for (int k = 0; k <= 2*N + 1; ++k) for (int u = 0; u < 2; ++u) {
    dp[n][k][u] = 0;
    // dp[m][n][k][u] = 0;
  }
  
  for (int u = 0; u < 2; ++u) {
    dp[u][u + 1][u] += 1;
    // dp[0][u][u + 1][u] += 1;
  }
// for(int m=0;m<N;++m)
  for (int n = 0; n <= N; ++n) for (int k = 1; k <= 2*N + 1; ++k) for (int u = 0; u < 2; ++u) if (dp[n][k][u]) {
  // for (int n = 0; n <= N; ++n) for (int k = 1; k <= 2*N + 1; ++k) for (int u = 0; u < 2; ++u) if (dp[m][n][k][u]) {
    for (int v = 0; v < 2; ++v) for (int a = 0; n + 1 + v + a <= N; ++a) {
      dp[n + 1 + v + a][k + a + 1 + u + v + a + 1][v]
      // dp[m + 1][n + 1 + v + a][k + a + 1 + u + v + a + 1][v]
        += dp[n][k][u]
        // += dp[m][n][k][u]
          * ((u && v) ? 2 : 1)
          * (fac[k + a - 1] * invFac[k - 1])
          * (fac[u + v + a] * invFac[u + v] * invFac[a])
        ;
    }
  }
  Mint ans = 0;
// vector<Mint>anss(N+1,0);
// for(int m=0;m<=N;++m)
  for (int k = 0; k <= 2*N + 1; ++k) {
    ans += dp[N][k][0];
    // ans += dp[m][N][k][0];
    // anss[m] += dp[m][N][k][0];
  }
// cerr<<"anss = "<<anss<<endl;
  return ans;
}


int main() {
  Mint::setM(1'000'000'007);
  prepare();
  
  // exper::run();
  // for (int N = 1; N <= 8; ++N) cerr << N << ": " << slow(N) << endl;
  
  int N, MO;
  for (; ~scanf("%d%d", &N, &MO); ) {
    Mint::setM(MO);
    prepare();
    Mint ans = slow(N);
    Mint all = 1;
    for (int i = 0; i < N; ++i) all *= (2 * i + 1);
    ans = all - ans;
    printf("%u\n", ans.x);
  }
  return 0;
}

詳細信息

Subtask #1:

score: 5
Accepted

Test #1:

score: 5
Accepted
time: 8ms
memory: 146760kb

input:

1 194903119

output:

0

result:

ok 1 number(s): "0"

Test #2:

score: 5
Accepted
time: 8ms
memory: 146592kb

input:

2 933234047

output:

0

result:

ok 1 number(s): "0"

Test #3:

score: 5
Accepted
time: 4ms
memory: 146504kb

input:

3 277793111

output:

2

result:

ok 1 number(s): "2"

Test #4:

score: 5
Accepted
time: 7ms
memory: 146512kb

input:

4 355321177

output:

28

result:

ok 1 number(s): "28"

Test #5:

score: 5
Accepted
time: 4ms
memory: 146508kb

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: 8ms
memory: 146792kb

input:

8 361605653

output:

1236922

result:

ok 1 number(s): "1236922"

Test #7:

score: 5
Accepted
time: 8ms
memory: 146808kb

input:

8 995512643

output:

1236922

result:

ok 1 number(s): "1236922"

Test #8:

score: 5
Accepted
time: 8ms
memory: 146764kb

input:

8 101102801

output:

1236922

result:

ok 1 number(s): "1236922"

Test #9:

score: 5
Accepted
time: 16ms
memory: 146600kb

input:

6 458322727

output:

4894

result:

ok 1 number(s): "4894"

Test #10:

score: 5
Accepted
time: 11ms
memory: 146600kb

input:

7 721691819

output:

73884

result:

ok 1 number(s): "73884"

Test #11:

score: 5
Accepted
time: 14ms
memory: 146504kb

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: 12ms
memory: 146460kb

input:

30 779092367

output:

686412377

result:

ok 1 number(s): "686412377"

Test #13:

score: 27
Accepted
time: 11ms
memory: 146792kb

input:

29 963995171

output:

128570082

result:

ok 1 number(s): "128570082"

Test #14:

score: 27
Accepted
time: 3ms
memory: 146500kb

input:

18 666092701

output:

185922458

result:

ok 1 number(s): "185922458"

Test #15:

score: 27
Accepted
time: 8ms
memory: 146588kb

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

input:

300 463478027

output:

89265245

result:

ok 1 number(s): "89265245"

Test #17:

score: 14
Accepted
time: 12ms
memory: 146600kb

input:

296 567610679

output:

406342763

result:

ok 1 number(s): "406342763"

Test #18:

score: 14
Accepted
time: 3ms
memory: 146528kb

input:

297 609090359

output:

128986577

result:

ok 1 number(s): "128986577"

Test #19:

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

input:

48 759569383

output:

635573072

result:

ok 1 number(s): "635573072"

Test #20:

score: 14
Accepted
time: 12ms
memory: 146508kb

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: 179ms
memory: 146588kb

input:

3000 752129633

output:

33058561

result:

ok 1 number(s): "33058561"

Test #22:

score: 36
Accepted
time: 179ms
memory: 146532kb

input:

2993 491173567

output:

343277625

result:

ok 1 number(s): "343277625"

Test #23:

score: 36
Accepted
time: 175ms
memory: 146788kb

input:

2993 783095563

output:

776085006

result:

ok 1 number(s): "776085006"

Test #24:

score: 36
Accepted
time: 17ms
memory: 146508kb

input:

327 399783431

output:

163535283

result:

ok 1 number(s): "163535283"

Test #25:

score: 36
Accepted
time: 38ms
memory: 146792kb

input:

1233 857060207

output:

422139845

result:

ok 1 number(s): "422139845"

Test #26:

score: 36
Accepted
time: 28ms
memory: 146500kb

input:

1114 600227447

output:

598509129

result:

ok 1 number(s): "598509129"

Subtask #6:

score: 0
Runtime Error

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

100%
Accepted

Dependency #5:

100%
Accepted

Test #27:

score: 0
Runtime Error

input:

20000 221054167

output:


result: