QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#243322#7759. Permutation Counting 2hos_lyric#100 ✓1412ms9028kbC++146.2kb2023-11-08 02:55:332024-07-04 02:23:09

Judging History

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

  • [2024-07-04 02:23:09]
  • 评测
  • 测评结果:100
  • 用时:1412ms
  • 内存:9028kb
  • [2023-11-08 02:55:33]
  • 提交

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 = 510 * 510 + 10;
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, MO;
Mint f[510][510];
Mint g[510][510];

int main() {
  for (; ~scanf("%d%d", &N, &MO); ) {
    Mint::setM(MO);
    prepare();
    
    // a blocks * b blocks
    for (int a = 0; a <= N; ++a) for (int b = 0; b <= N; ++b) {
      f[a][b] = binom(a * b + N - 1, N);
    }
    // no empty block
    for (int a = 0; a <= N; ++a) for (int c = 1; c <= a; ++c) {
      for (int b = 0; b <= N; ++b) {
        f[a][b] -= binom(a, c) * f[a - c][b];
      }
    }
    for (int a = 0; a <= N; ++a) {
      for (int b = 0; b <= N; ++b) for (int c = 1; c <= b; ++c) {
        f[a][b] -= binom(b, c) * f[a][b - c];
      }
    }
// for(int a=0;a<=N;++a)pv(f[a],f[a]+(N+1));
    
    for (int a = 0; a <= N; ++a) for (int b = 0; b <= N; ++b) {
      f[a][b] = f[a + 1][b + 1];
    }
    for (int a = 0; a < N; ++a) for (int c = 1; c <= a; ++c) {
      for (int b = 0; b < N; ++b) {
        f[a][b] -= binom(N - 1 - (a - c), N - 1 - a) * f[a - c][b];
      }
    }
    for (int a = 0; a < N; ++a) {
      for (int b = 0; b < N; ++b) for (int c = 1; c <= b; ++c) {
        f[a][b] -= binom(N - 1 - (b - c), N - 1 - b) * f[a][b - c];
      }
    }
    
    for (int a = 0; a < N; ++a) {
      for (int b = 0; b < N; ++b) {
        if (b) printf(" ");
        printf("%u", f[a][b].x);
      }
      puts("");
    }
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 10
Accepted

Test #1:

score: 10
Accepted
time: 4ms
memory: 8976kb

input:

7 1001458121

output:

1 0 0 0 0 0 0
0 56 56 8 0 0 0
0 56 659 440 36 0 0
0 8 440 1520 440 8 0
0 0 36 440 659 56 0
0 0 0 8 56 56 0
0 0 0 0 0 0 1

result:

ok 49 tokens

Test #2:

score: 0
Accepted
time: 4ms
memory: 8904kb

input:

8 1008735209

output:

1 0 0 0 0 0 0 0
0 84 126 36 1 0 0 0
0 126 1773 1980 405 9 0 0
0 36 1980 8436 4761 405 1 0
0 1 405 4761 8436 1980 36 0
0 0 9 405 1980 1773 126 0
0 0 0 1 36 126 84 0
0 0 0 0 0 0 0 1

result:

ok 64 tokens

Subtask #2:

score: 15
Accepted

Test #3:

score: 15
Accepted
time: 4ms
memory: 8972kb

input:

14 1000253273

output:

1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 455 3003 6435 5005 1365 105 1 0 0 0 0 0 0
0 3003 112905 730665 1629435 1456560 529956 71940 2835 15 0 0 0 0
0 6435 730665 10865585 46433475 75169560 50184540 13633740 1349931 36735 120 0 0 0
0 5005 1629435 46433475 336576825 860578230 885230850 375891370 62035485 332475...

result:

ok 196 tokens

Test #4:

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

input:

15 1009800301

output:

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 560 4368 11440 11440 4368 560 16 0 0 0 0 0 0 0
0 4368 188682 1482416 4160120 4899264 2511376 536384 41328 800 1 0 0 0 0
0 11440 1482416 26232784 139089120 291102560 265085216 106311200 17712368 1048560 15232 16 0 0 0
0 11440 4160120 139089120 216926039 947184153 39833...

result:

ok 225 tokens

Test #5:

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

input:

16 1006729121

output:

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 680 6188 19448 24310 12376 2380 136 1 0 0 0 0 0 0 0
0 6188 305150 2867696 9916066 14924539 10288876 3196000 410329 17748 153 0 0 0 0 0
0 19448 2867696 59852036 387206263 15304436 216863763 683915984 173666645 18275272 650641 4828 1 0 0 0
0 24310 9916066 387206263 82...

result:

ok 256 tokens

Subtask #3:

score: 25
Accepted

Test #6:

score: 25
Accepted
time: 0ms
memory: 8976kb

input:

36 1003299797

output:

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 7770 435897 10295472 124403620 854992152 552567909 334501587 855871755 616535351 836177106 87288018 849183199 348330136 38608020 2324784 66045 666 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 435897 133844910 939232742 752696285 9488...

result:

ok 1296 tokens

Test #7:

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

input:

37 1009736899

output:

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 8436 501942 12620256 163011640 193585389 366265801 325233075 508510208 4472335 508510208 325233075 366265801 193585389 163011640 12620256 501942 8436 38 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 501942 164674938 380061172 795391...

result:

ok 1369 tokens

Test #8:

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

input:

38 1002064493

output:

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 9139 575757 15380937 211915132 673991551 105909500 89228335 917893160 782878886 231145424 634874749 53537001 904603957 635745396 61523748 3262623 82251 741 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 575757 201514950 26611267 ...

result:

ok 1444 tokens

Test #9:

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

input:

39 1000696681

output:

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 9880 658008 18643560 273438880 310408078 24862708 197477816 671070872 191143189 191143189 671070872 197477816 24862708 310408078 273438880 18643560 658008 9880 40 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 658008 245336455 ...

result:

ok 1521 tokens

Test #10:

score: 0
Accepted
time: 2ms
memory: 8844kb

input:

40 1002813283

output:

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 10660 749398 22481940 350343565 151022119 572250549 255038067 159674717 979042431 374977376 547170717 790491840 141687815 878961939 118286125 95548245 4496388 101270 820 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 749398...

result:

ok 1600 tokens

Subtask #4:

score: 25
Accepted

Test #11:

score: 25
Accepted
time: 15ms
memory: 8844kb

input:

96 1005401729

output:

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 147440 64446024 781420036 468430311 27733626 62151757 454795566 711626792 885805006 401110492 711423106 32...

result:

ok 9216 tokens

Test #12:

score: 0
Accepted
time: 15ms
memory: 9020kb

input:

97 1003022927

output:

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 152096 67910864 795115101 924546504 230011659 577127906 564913191 11843263 171607987 697964156 4314087 7...

result:

ok 9409 tokens

Test #13:

score: 0
Accepted
time: 11ms
memory: 8896kb

input:

98 1000259233

output:

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 156849 71523144 883402282 582472554 858367843 730708960 476781508 806308877 286962083 221796390 327681...

result:

ok 9604 tokens

Test #14:

score: 0
Accepted
time: 16ms
memory: 8976kb

input:

99 1000444889

output:

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 161700 75287520 442576 386074411 823487426 504618380 971268883 734797176 388493421 848753352 8574809...

result:

ok 9801 tokens

Test #15:

score: 0
Accepted
time: 12ms
memory: 9028kb

input:

100 1008746839

output:

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 166650 79208745 50916937 213745970 953400361 882774939 595265332 362179793 339853992 820776686 204...

result:

ok 10000 tokens

Subtask #5:

score: 25
Accepted

Test #16:

score: 25
Accepted
time: 1374ms
memory: 8976kb

input:

496 1005266363

output:

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

result:

ok 246016 tokens

Test #17:

score: 0
Accepted
time: 1391ms
memory: 8840kb

input:

497 1000331767

output:

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

result:

ok 247009 tokens

Test #18:

score: 0
Accepted
time: 1402ms
memory: 8836kb

input:

498 1000148759

output:

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

result:

ok 248004 tokens

Test #19:

score: 0
Accepted
time: 1405ms
memory: 9028kb

input:

499 1000176851

output:

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

result:

ok 249001 tokens

Test #20:

score: 0
Accepted
time: 1412ms
memory: 8840kb

input:

500 1002873259

output:

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

result:

ok 250000 tokens