QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#336283 | #8047. DFS Order 4 | Licykoc | AC ✓ | 651ms | 5780kb | C++23 | 4.3kb | 2024-02-24 14:29:28 | 2024-02-24 14:29:29 |
Judging History
answer
#include <bits/stdc++.h>
typedef unsigned long long ull;
typedef __uint128_t L;
struct FastMod {
ull b, m;
FastMod(ull b) : b(b), m(ull((L(1) << 64) / b)) {}
ull reduce(ull a) {
ull q = (ull)((L(m) * a) >> 64);
ull r = a - q * b; // can be proven that 0 <= r < 2 * b
return r >= b ? r - b : r;
}
} Fastmod(2);
template<unsigned id>
class dynamic_modint {
using mint = dynamic_modint<id>;
protected:
static unsigned P;
unsigned v;
public:
dynamic_modint() : v() {}
template<typename T,
typename std::enable_if<std::is_integral<T>::value &&
std::is_signed<T>::value,
bool>::type = true>
dynamic_modint(T t_v) : v() {
long long tmp = t_v % static_cast<long long>(P);
if (tmp < 0) {
tmp += P;
}
v = tmp;
}
template<typename T,
typename std::enable_if<std::is_integral<T>::value &&
std::is_unsigned<T>::value,
bool>::type = true>
dynamic_modint(T t_v) : v() {
v = t_v % P;
}
unsigned val() const {
return v;
}
static unsigned mod() {
return P;
}
static void set_mod(unsigned nP) {
Fastmod = FastMod(nP);
P = nP;
}
static mint raw(unsigned v) {
mint res;
res.v = v;
return res;
}
mint &operator+=(const mint &rhs) {
v < P - rhs.v ? v += rhs.v : v -= P - rhs.v;
return *this;
}
mint &operator++() {
v + 1 < P ? ++v : v = 0;
return *this;
}
mint operator++(int) {
mint tmp = *this;
v + 1 < P ? ++v : v = 0;
return tmp;
}
mint &operator-=(const mint &rhs) {
v < rhs.v ? v += P - rhs.v : v -= rhs.v;
return *this;
}
mint &operator--() {
v ? --v : v = P - 1;
return *this;
}
mint operator--(int) {
mint tmp = *this;
v ? --v : v = P - 1;
return tmp;
}
mint operator-() const {
mint res;
res.v = v ? P - v : 0;
return res;
}
mint &operator*=(const mint &rhs) {
v = Fastmod.reduce(static_cast<unsigned long long>(v) * rhs.v);
return *this;
}
mint pow(unsigned long long b) const {
mint a(*this), s(1);
for (; b; b >>= 1) {
if (b & 1) {
s *= a;
}
a *= a;
}
return s;
}
mint inv() const {
return pow(P - 2);
}
friend mint operator+(const mint &lhs, const mint &rhs) {
return mint(lhs) += rhs;
}
friend mint operator-(const mint &lhs, const mint &rhs) {
return mint(lhs) -= rhs;
}
friend mint operator*(const mint &lhs, const mint &rhs) {
return mint(lhs) *= rhs;
}
friend bool operator==(const mint &lhs, const mint &rhs) {
return lhs.v == rhs.v;
}
friend bool operator!=(const mint &lhs, const mint &rhs) {
return lhs.v != rhs.v;
}
friend std::istream &operator>>(std::istream &in, mint &x) {
return in >> x.v;
}
friend std::ostream &operator<<(std::ostream &out, const mint &x) {
return out << x.v;
}
};
template<unsigned id>
unsigned dynamic_modint<id>::P;
using mint = dynamic_modint<0>;
class Binom {
private:
std::vector<mint> Fac, Inv;
public:
Binom(int n) : Fac(n + 1), Inv(n + 1) {
Fac[0] = 1;
for (int i = 1; i <= n; ++i) {
Fac[i] = Fac[i - 1] * i;
}
Inv[n] = Fac[n].inv();
for (int i = n; i; --i) {
Inv[i - 1] = Inv[i] * i;
}
}
mint fac(int x) {
return Fac[x];
}
mint invfac(int x) {
return Inv[x];
}
mint binom(int n, int m) {
if (n < m) {
return mint();
}
return Fac[n] * Inv[m] * Inv[n - m];
}
};
signed main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, P;
std::cin >> n >> P;
mint::set_mod(P);
Binom comb(n);
std::vector<std::vector<mint>> dp(n + 1, std::vector<mint>(n + 1));
dp[1][0] = 1;
for (int i = 2; i <= n; ++i) {
for (int j = 0; i + j <= n; ++j) {
dp[i][j] += dp[i - 1][0];
for (int k = 2; i - k > 1; ++k) {
dp[i][j] += (dp[k][0] - dp[k][i - k - 1 + j]) * dp[i - k][j];
}
dp[i][j] *= mint(i + j - 1).inv();
}
}
std::cout << dp[n][0] * comb.fac(n - 1) << '\n';
}
这程序好像有点Bug,我给组数据试试?
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3596kb
input:
4 114514199
output:
2
result:
ok 1 number(s): "2"
Test #2:
score: 0
Accepted
time: 1ms
memory: 3768kb
input:
10 998244353
output:
11033
result:
ok 1 number(s): "11033"
Test #3:
score: 0
Accepted
time: 2ms
memory: 3868kb
input:
100 1000000007
output:
270904395
result:
ok 1 number(s): "270904395"
Test #4:
score: 0
Accepted
time: 556ms
memory: 5428kb
input:
756 1001338769
output:
901942543
result:
ok 1 number(s): "901942543"
Test #5:
score: 0
Accepted
time: 633ms
memory: 5668kb
input:
793 1009036033
output:
301770320
result:
ok 1 number(s): "301770320"
Test #6:
score: 0
Accepted
time: 564ms
memory: 5560kb
input:
759 1005587659
output:
846376219
result:
ok 1 number(s): "846376219"
Test #7:
score: 0
Accepted
time: 587ms
memory: 5416kb
input:
773 1007855479
output:
1398019
result:
ok 1 number(s): "1398019"
Test #8:
score: 0
Accepted
time: 538ms
memory: 5408kb
input:
751 1006730639
output:
321287237
result:
ok 1 number(s): "321287237"
Test #9:
score: 0
Accepted
time: 600ms
memory: 5464kb
input:
778 1007760653
output:
430322899
result:
ok 1 number(s): "430322899"
Test #10:
score: 0
Accepted
time: 648ms
memory: 5680kb
input:
798 1007543827
output:
688720826
result:
ok 1 number(s): "688720826"
Test #11:
score: 0
Accepted
time: 651ms
memory: 5780kb
input:
796 1004841413
output:
258829347
result:
ok 1 number(s): "258829347"
Test #12:
score: 0
Accepted
time: 604ms
memory: 5416kb
input:
775 1005185189
output:
744278608
result:
ok 1 number(s): "744278608"
Test #13:
score: 0
Accepted
time: 651ms
memory: 5744kb
input:
800 1006012831
output:
508549367
result:
ok 1 number(s): "508549367"
Test #14:
score: 0
Accepted
time: 0ms
memory: 3596kb
input:
1 1001338769
output:
1
result:
ok 1 number(s): "1"
Test #15:
score: 0
Accepted
time: 0ms
memory: 3600kb
input:
2 1001338769
output:
1
result:
ok 1 number(s): "1"
Test #16:
score: 0
Accepted
time: 0ms
memory: 3552kb
input:
9 1009036033
output:
1780
result:
ok 1 number(s): "1780"
Test #17:
score: 0
Accepted
time: 0ms
memory: 3540kb
input:
14 1001338769
output:
43297358
result:
ok 1 number(s): "43297358"
Extra Test:
score: 0
Extra Test Passed