QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#387464 | #8047. DFS Order 4 | wsyear | TL | 3ms | 6168kb | C++14 | 3.0kb | 2024-04-12 15:29:55 | 2024-04-12 15:29:55 |
Judging History
answer
// Author: Klay Thompson
// Problem: # 8047. DFS Order 4
// Memory Limit: 1024 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
#define rep(i, j, k) for (int i = (j); i <= (k); ++i)
#define per(i, j, k) for (int i = (j); i >= (k); --i)
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
using ll = long long;
using pii = std::pair<int, int>;
using pll = std::pair<ll, ll>;
template<class T>inline void chkmn(T &x, T y) { if (y < x) x = y; }
template<class T>inline void chkmx(T &x, T y) { if (y > x) x = y; }
using namespace std;
int P;
class mod_int {
using Z = mod_int;
private:
static int mo(int x) { return x < 0 ? x + P : x; }
public:
int x;
int val() const { return x; }
mod_int() : x(0) {}
template <class T>
mod_int(const T &x_) : x(x_ >= 0 && x_ < P ? static_cast<int>(x_) : mo(static_cast<int>(x_ % P))) {}
bool operator==(const Z &rhs) const { return x == rhs.x; }
bool operator!=(const Z &rhs) const { return x != rhs.x; }
Z operator-() const { return Z(x ? P - x : 0); }
Z pow(long long k) const {
Z res = 1, t = *this;
while (k) {
if (k & 1) res *= t;
if (k >>= 1) t *= t;
}
return res;
}
Z &operator++() {
x < P - 1 ? ++x : x = 0;
return *this;
}
Z &operator--() {
x ? --x : x = P - 1;
return *this;
}
Z operator++(int) {
Z ret = x;
x < P - 1 ? ++x : x = 0;
return ret;
}
Z operator--(int) {
Z ret = x;
x ? --x : x = P - 1;
return ret;
}
Z inv() const { return pow(P - 2); }
Z &operator+=(const Z &rhs) {
(x += rhs.x) >= P && (x -= P);
return *this;
}
Z &operator-=(const Z &rhs) {
(x -= rhs.x) < 0 && (x += P);
return *this;
}
Z &operator*=(const Z &rhs) {
x = 1ULL * x * rhs.x % P;
return *this;
}
Z &operator/=(const Z &rhs) { return *this *= rhs.inv(); }
#define setO(T, o) \
friend T operator o(const Z &lhs, const Z &rhs) {\
Z res = lhs; \
return res o## = rhs; \
}
setO(Z, +) setO(Z, -) setO(Z, *) setO(Z, /)
#undef setO
};
using Z = mod_int;
const int maxn = 810;
int n;
Z f[maxn][maxn], fac[maxn], ivf[maxn], inv[maxn];
int main() {
cin.tie(nullptr) -> ios::sync_with_stdio(false);
cin >> n >> P;
fac[0] = 1;
rep (i, 1, n) fac[i] = fac[i - 1] * i;
ivf[n] = fac[n].inv();
per (i, n, 1) ivf[i - 1] = ivf[i] * i;
rep (i, 1, n) inv[i] = fac[i - 1] * ivf[i];
f[1][0] = 1;
rep (i, 2, n) rep (j, 0, n) {
f[i][j] = f[i - 1][0] * inv[i + j - 1];
rep (k, 2, i - 1) {
f[i][j] += f[k][0] * f[i - k][j] * inv[i + j - 1];
f[i][j] -= f[k][i - 1 - k + j] * f[i - k][j] * inv[i + j - 1];
}
// cerr << i << " " << j << ": " << f[i][j].val() << '\n';
}
cout << (f[n][0] * fac[n - 1]).val() << '\n';
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 6156kb
input:
4 114514199
output:
2
result:
ok 1 number(s): "2"
Test #2:
score: 0
Accepted
time: 1ms
memory: 6156kb
input:
10 998244353
output:
11033
result:
ok 1 number(s): "11033"
Test #3:
score: 0
Accepted
time: 3ms
memory: 6168kb
input:
100 1000000007
output:
270904395
result:
ok 1 number(s): "270904395"
Test #4:
score: -100
Time Limit Exceeded
input:
756 1001338769