QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#330521#8047. DFS Order 4ucup-team087#AC ✓920ms6644kbC++146.2kb2024-02-17 16:38:372024-02-17 16:38:38

Judging History

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

  • [2024-02-17 16:38:38]
  • 评测
  • 测评结果:AC
  • 用时:920ms
  • 内存:6644kb
  • [2024-02-17 16:38:37]
  • 提交

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

Mint interpolateIota(const vector<Mint> &fs, Mint x) {
  const int fsLen = fs.size();
  vector<Mint> prodR(fsLen + 1);
  prodR[fsLen] = 1;
  for (int i = fsLen; --i >= 0; ) prodR[i] = (x - i) * prodR[i + 1];
  Mint ans = 0;
  Mint prodL = 1;
  for (int i = 0; i < fsLen; ++i) {
    // (i - 0) ... (i - (i - 1)) (i - (i + 1)) ... (i - (fsLen - 1))
    ans += invFac[i] * (((fsLen - 1 - i) & 1) ? -1 : +1) *
           invFac[fsLen - 1 - i] * fs[i] * prodL * prodR[i + 1];
    prodL *= (x - i);
  }
  return ans;
}


int N, P;
Mint F[810][810];

inline Mint get(int n, int y) {
  return F[n][y] - (1 - y) * F[n][0];
}

int main() {
  for (; ~scanf("%d%d", &N, &P); ) {
    Mint::setM(P);
    prepare();
    
    --N;
    memset(F, 0, sizeof(F));
    for (int y = 0; y <= N + 1; ++y) {
      F[0][y] = 1;
    }
    for (int n = 0; n <= N; ++n) {
      for (int y = 1; y <= N + 1; ++y) {
        Mint sum = 0;
        sum += (1 - y) * inv[y] * get(n, y);
        for (int k = 0; k <= n; ++k) {
          sum += F[n - k][y] * get(k, y);
        }
        F[n+1][y] = inv[n+1] * sum;
      }
      F[n+1][0] = interpolateIota(vector<Mint>(F[n+1] + 1, F[n+1] + (N + 1 + 1)), -1);
// if(n<=10){for(int y=0;y<=11;++y)cerr<<(fac[n+1]*F[n+1][y])<<" ";cerr<<endl;}
    }
    const Mint ans = fac[N] * F[N][0];
    printf("%u\n", ans.x);
  }
  return 0;
}

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

詳細信息

Test #1:

score: 100
Accepted
time: 2ms
memory: 6360kb

input:

4 114514199

output:

2

result:

ok 1 number(s): "2"

Test #2:

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

input:

10 998244353

output:

11033

result:

ok 1 number(s): "11033"

Test #3:

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

input:

100 1000000007

output:

270904395

result:

ok 1 number(s): "270904395"

Test #4:

score: 0
Accepted
time: 769ms
memory: 6592kb

input:

756 1001338769

output:

901942543

result:

ok 1 number(s): "901942543"

Test #5:

score: 0
Accepted
time: 901ms
memory: 6360kb

input:

793 1009036033

output:

301770320

result:

ok 1 number(s): "301770320"

Test #6:

score: 0
Accepted
time: 789ms
memory: 6628kb

input:

759 1005587659

output:

846376219

result:

ok 1 number(s): "846376219"

Test #7:

score: 0
Accepted
time: 845ms
memory: 6316kb

input:

773 1007855479

output:

1398019

result:

ok 1 number(s): "1398019"

Test #8:

score: 0
Accepted
time: 760ms
memory: 6352kb

input:

751 1006730639

output:

321287237

result:

ok 1 number(s): "321287237"

Test #9:

score: 0
Accepted
time: 852ms
memory: 6428kb

input:

778 1007760653

output:

430322899

result:

ok 1 number(s): "430322899"

Test #10:

score: 0
Accepted
time: 910ms
memory: 6356kb

input:

798 1007543827

output:

688720826

result:

ok 1 number(s): "688720826"

Test #11:

score: 0
Accepted
time: 907ms
memory: 6360kb

input:

796 1004841413

output:

258829347

result:

ok 1 number(s): "258829347"

Test #12:

score: 0
Accepted
time: 840ms
memory: 6644kb

input:

775 1005185189

output:

744278608

result:

ok 1 number(s): "744278608"

Test #13:

score: 0
Accepted
time: 920ms
memory: 6428kb

input:

800 1006012831

output:

508549367

result:

ok 1 number(s): "508549367"

Test #14:

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

input:

1 1001338769

output:

1

result:

ok 1 number(s): "1"

Test #15:

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

input:

2 1001338769

output:

1

result:

ok 1 number(s): "1"

Test #16:

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

input:

9 1009036033

output:

1780

result:

ok 1 number(s): "1780"

Test #17:

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

input:

14 1001338769

output:

43297358

result:

ok 1 number(s): "43297358"

Extra Test:

score: 0
Extra Test Passed