QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#858093#9777. Balance in All Thingshos_lyricAC ✓1700ms1016516kbC++147.4kb2025-01-16 14:03:062025-01-16 14:03:06

Judging History

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

  • [2025-01-16 14:03:06]
  • 评测
  • 测评结果:AC
  • 用时:1700ms
  • 内存:1016516kb
  • [2025-01-16 14:03:06]
  • 提交

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 = 810;
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;
    }
  }
}

////////////////////////////////////////////////////////////////////////////////


int N, K, MO;
Mint dp[402][802][802];
Mint f[810], g[810][810];

int main() {
  for (; ~scanf("%d%d%d", &N, &K, &MO); ) {
    Mint::setM(MO);
    prepare();
    
    memset(dp, 0, sizeof(dp));
    for (int q = 0; q <= N; ++q) {
      dp[q][0][0] += 1;
      for (int x = 0; q + x <= 2*N; ++x) for (int y = 0; q + x + y <= 2*N && q + x - y >= 0; ++y) {
        dp[q][x + 1][y] += dp[q][x][y];
        dp[q][x][y + 1] += dp[q][x][y] * (q + x - y);
      }
    }
    
#define go(all,minus) t *= dp[q][all-minus][minus]; q += (all-minus-minus); if (q < 0) { continue; } {}
    memset(f, 0, sizeof(f));
    memset(g, 0, sizeof(g));
    f[0] += 1;
    for (int k = 0; k < K; ++k) {
      if (k % 2 == 0) {
        for (int a = 0; a <= 2*N; ++a) if (f[a]) {
          const int c = a;
          const int b = 2*N - a - c;
          assert(a + b + c == 2*N);
          assert(-2*a + 0*b + 2*c == 0);
          for (int x = 0; x <= a; ++x) for (int z = 0; z <= c; ++z) {
            const int y = (b - (0 - (a-x-x) - (c-z-z))) / 2;
            if (0 <= y && y <= b) {
              Mint t = f[a];
              int q = 0;
              go(a, x);
              go(b, y);
              go(c, z);
// if(N<=10)cerr<<"k = "<<k<<", a = "<<a<<", f[a] = "<<f[a]<<"; "<<a<<" "<<b<<" "<<c<<"; "<<x<<" "<<y<<" "<<z<<" -> "<<x<<" "<<((a-x)+y)<<", t = "<<t<<"; "<<dp[0][a-x][x]<<" * "<<dp[a-x-x][b-y][y]<<" * "<<dp[a-x-x+b-y-y][c-z][z]<<endl;
              assert(q == 0);
              g[x][(a-x) + y] += t;
            }
          }
        }
        memset(f, 0, sizeof(f));
      } else {
        for (int a = 0; a <= 2*N; ++a) for (int b = 0; a + b <= 2*N; ++b) if (g[a][b]) {
          const int c = 3*N - 3*a - 2*b;
          const int d = -N + 2*a + b;
          assert(a + b + c + d == 2*N);
          assert(-3*a - b + c + 3*d == 0);
          for (int y = 0; y <= b; ++y) {
            const int x = 0, w = d;
            const int z = (c - (0 - (a-x-x) - (b-y-y) - (d-w-w))) / 2;
            if (0 <= z && z <= c) {
              Mint t = g[a][b];
              int q = 0;
              go(a, x);
              go(b, y);
              go(c, z);
              go(d, w);
              assert(q == 0);
              f[(a-x) + y] += t;
            }
          }
        }
        memset(g, 0, sizeof(g));
      }
    }
    
    Mint ans = 0;
    for (int a = 0; a <= 2*N; ++a) ans += f[a];
    for (int a = 0; a <= 2*N; ++a) for (int b = 0; a + b <= 2*N; ++b) ans += g[a][b];
    printf("%u\n", ans.x);
  }
  return 0;
}

这程序好像有点Bug,我给组数据试试?

詳細信息

Test #1:

score: 100
Accepted
time: 109ms
memory: 1016272kb

input:

3 1 1000000007

output:

15

result:

ok single line: '15'

Test #2:

score: 0
Accepted
time: 112ms
memory: 1016232kb

input:

100 3 1000000007

output:

894710378

result:

ok single line: '894710378'

Test #3:

score: 0
Accepted
time: 108ms
memory: 1016192kb

input:

6 6 1000000007

output:

103387851

result:

ok single line: '103387851'

Test #4:

score: 0
Accepted
time: 113ms
memory: 1016512kb

input:

2 6 998244353

output:

729

result:

ok single line: '729'

Test #5:

score: 0
Accepted
time: 1700ms
memory: 1016512kb

input:

400 20 713862469

output:

561801910

result:

ok single line: '561801910'

Test #6:

score: 0
Accepted
time: 1424ms
memory: 1016400kb

input:

397 17 861815723

output:

764050494

result:

ok single line: '764050494'

Test #7:

score: 0
Accepted
time: 1152ms
memory: 1016396kb

input:

391 14 189546607

output:

131011086

result:

ok single line: '131011086'

Test #8:

score: 0
Accepted
time: 877ms
memory: 1016400kb

input:

382 11 922402597

output:

58290636

result:

ok single line: '58290636'

Test #9:

score: 0
Accepted
time: 628ms
memory: 1016400kb

input:

370 8 911997731

output:

823449745

result:

ok single line: '823449745'

Test #10:

score: 0
Accepted
time: 428ms
memory: 1016512kb

input:

355 5 708230279

output:

173013324

result:

ok single line: '173013324'

Test #11:

score: 0
Accepted
time: 996ms
memory: 1016400kb

input:

337 19 742344989

output:

592413780

result:

ok single line: '592413780'

Test #12:

score: 0
Accepted
time: 796ms
memory: 1016400kb

input:

334 16 816423319

output:

567577272

result:

ok single line: '567577272'

Test #13:

score: 0
Accepted
time: 663ms
memory: 1016384kb

input:

328 13 463459417

output:

77314092

result:

ok single line: '77314092'

Test #14:

score: 0
Accepted
time: 490ms
memory: 1016400kb

input:

319 10 469180903

output:

94351928

result:

ok single line: '94351928'

Test #15:

score: 0
Accepted
time: 364ms
memory: 1016404kb

input:

307 7 240226001

output:

147458009

result:

ok single line: '147458009'

Test #16:

score: 0
Accepted
time: 275ms
memory: 1016404kb

input:

292 4 725635439

output:

275908993

result:

ok single line: '275908993'

Test #17:

score: 0
Accepted
time: 488ms
memory: 1016516kb

input:

274 18 518962097

output:

200937333

result:

ok single line: '200937333'

Test #18:

score: 0
Accepted
time: 455ms
memory: 1016404kb

input:

271 15 765905797

output:

683367155

result:

ok single line: '683367155'

Test #19:

score: 0
Accepted
time: 360ms
memory: 1016252kb

input:

265 12 240076537

output:

6822852

result:

ok single line: '6822852'

Test #20:

score: 0
Accepted
time: 284ms
memory: 1016400kb

input:

256 9 319057337

output:

9076486

result:

ok single line: '9076486'

Test #21:

score: 0
Accepted
time: 233ms
memory: 1016516kb

input:

244 6 257631131

output:

53842743

result:

ok single line: '53842743'

Test #22:

score: 0
Accepted
time: 188ms
memory: 1016512kb

input:

229 3 953863681

output:

247424565

result:

ok single line: '247424565'

Test #23:

score: 0
Accepted
time: 116ms
memory: 1016400kb

input:

1 20 998244353

output:

1

result:

ok single line: '1'

Test #24:

score: 0
Accepted
time: 117ms
memory: 1016396kb

input:

8 4 998244353

output:

740255777

result:

ok single line: '740255777'

Test #25:

score: 0
Accepted
time: 109ms
memory: 1016356kb

input:

5 20 998244353

output:

725931122

result:

ok single line: '725931122'

Test #26:

score: 0
Accepted
time: 476ms
memory: 1016400kb

input:

400 1 1000000007

output:

705610633

result:

ok single line: '705610633'

Test #27:

score: 0
Accepted
time: 488ms
memory: 1016396kb

input:

400 2 1000000007

output:

917456162

result:

ok single line: '917456162'

Test #28:

score: 0
Accepted
time: 109ms
memory: 1016404kb

input:

1 1 1000000007

output:

1

result:

ok single line: '1'

Extra Test:

score: 0
Extra Test Passed