QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#715831 | #9561. 树数叔术 | wsyear | 100 ✓ | 988ms | 35104kb | C++20 | 2.8kb | 2024-11-06 13:30:59 | 2024-11-06 13:30:59 |
Judging History
answer
#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 = 200;
int n, m;
Z f[maxn][maxn][maxn], fac[maxn], c[maxn][maxn];
// 考虑了 <= i 的值,虚树大小是 j,还有 k 个空位
int main() {
cin.tie(nullptr) -> ios::sync_with_stdio(false);
cin >> n >> m >> P;
chkmn(m, n);
c[0][0] = 1;
rep (i, 1, n) {
c[i][0] = 1;
rep (j, 1, i) c[i][j] = c[i - 1][j - 1] + c[i - 1][j];
}
fac[0] = 1;
rep (i, 1, n) fac[i] = fac[i - 1] * i;
f[0][1][0] = n;
rep (i, 1, m) {
rep (j, 1, n) rep (k, 0, j) rep (p, 1, k) {
f[i][j][k - p] += f[i - 1][j][k] * c[k][p];
}
rep (j, 1, n) rep (k, 0, j) rep (p, 1, n - j) {
f[i][j + p][k + p - 1] += f[i - 1][j][k] * j * c[n - j][p] * fac[p];
}
}
cout << f[m][n][0].val() << '\n';
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 5
Accepted
Test #1:
score: 5
Accepted
time: 0ms
memory: 35104kb
input:
1 1 624295285
output:
0
result:
ok single line: '0'
Test #2:
score: 5
Accepted
time: 0ms
memory: 34984kb
input:
4 684813415 564954712
output:
0
result:
ok single line: '0'
Test #3:
score: 5
Accepted
time: 4ms
memory: 35016kb
input:
4 2 844826878
output:
24
result:
ok single line: '24'
Test #4:
score: 5
Accepted
time: 4ms
memory: 35024kb
input:
4 17724073 252218682
output:
0
result:
ok single line: '0'
Test #5:
score: 5
Accepted
time: 0ms
memory: 35048kb
input:
4 3 697681963
output:
384
result:
ok single line: '384'
Subtask #2:
score: 15
Accepted
Test #6:
score: 15
Accepted
time: 0ms
memory: 35048kb
input:
6 4 956647977
output:
238320
result:
ok single line: '238320'
Test #7:
score: 15
Accepted
time: 3ms
memory: 34972kb
input:
6 450615260 491361886
output:
0
result:
ok single line: '0'
Test #8:
score: 15
Accepted
time: 0ms
memory: 34980kb
input:
6 5 339344353
output:
933120
result:
ok single line: '933120'
Test #9:
score: 15
Accepted
time: 0ms
memory: 35040kb
input:
6 3 842228619
output:
23760
result:
ok single line: '23760'
Test #10:
score: 15
Accepted
time: 0ms
memory: 35044kb
input:
6 5 331699652
output:
933120
result:
ok single line: '933120'
Subtask #3:
score: 30
Accepted
Test #11:
score: 30
Accepted
time: 6ms
memory: 35100kb
input:
48 26 424594716
output:
362283012
result:
ok single line: '362283012'
Test #12:
score: 30
Accepted
time: 14ms
memory: 35104kb
input:
49 1000000000 738885247
output:
0
result:
ok single line: '0'
Test #13:
score: 30
Accepted
time: 8ms
memory: 34932kb
input:
48 39 688951620
output:
598399200
result:
ok single line: '598399200'
Test #14:
score: 30
Accepted
time: 17ms
memory: 35036kb
input:
50 476039414 292870080
output:
0
result:
ok single line: '0'
Test #15:
score: 30
Accepted
time: 13ms
memory: 34972kb
input:
50 48 245196368
output:
123576912
result:
ok single line: '123576912'
Subtask #4:
score: 50
Accepted
Test #16:
score: 50
Accepted
time: 988ms
memory: 34984kb
input:
150 526250070 197316869
output:
0
result:
ok single line: '0'
Test #17:
score: 50
Accepted
time: 757ms
memory: 34980kb
input:
149 116 671784452
output:
18945228
result:
ok single line: '18945228'
Test #18:
score: 50
Accepted
time: 875ms
memory: 35052kb
input:
146 144 906402626
output:
438777234
result:
ok single line: '438777234'
Test #19:
score: 50
Accepted
time: 883ms
memory: 35048kb
input:
147 143 705666477
output:
70408701
result:
ok single line: '70408701'
Test #20:
score: 50
Accepted
time: 971ms
memory: 35052kb
input:
150 147 453481175
output:
336290325
result:
ok single line: '336290325'