QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#822846#9777. Balance in All Thingsucup-team087#WA 1900ms1011524kbC++147.4kb2024-12-20 16:59:392024-12-20 16:59:39

Judging History

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

  • [2024-12-20 16:59:39]
  • 评测
  • 测评结果:WA
  • 用时:1900ms
  • 内存:1011524kb
  • [2024-12-20 16:59:39]
  • 提交

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[401][801][801];
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;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 143ms
memory: 1011452kb

input:

3 1 1000000007

output:

15

result:

ok single line: '15'

Test #2:

score: 0
Accepted
time: 148ms
memory: 1011400kb

input:

100 3 1000000007

output:

894710378

result:

ok single line: '894710378'

Test #3:

score: 0
Accepted
time: 119ms
memory: 1011524kb

input:

6 6 1000000007

output:

103387851

result:

ok single line: '103387851'

Test #4:

score: 0
Accepted
time: 136ms
memory: 1011460kb

input:

2 6 998244353

output:

729

result:

ok single line: '729'

Test #5:

score: -100
Wrong Answer
time: 1900ms
memory: 1011436kb

input:

400 20 713862469

output:

93703861

result:

wrong answer 1st lines differ - expected: '561801910', found: '93703861'