QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#334050 | #8047. DFS Order 4 | hos_lyric | AC ✓ | 214ms | 6672kb | C++14 | 7.5kb | 2024-02-21 04:47:19 | 2024-02-21 04:47:20 |
Judging History
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;
}
}
}
namespace exper {
int N;
vector<int> P;
map<vector<int>, vector<int>> vis;
vector<int> us;
bool decide() {
vector<int> par(N, -1);
// trying to attach w to u, last sibling is v
int u = 0, v = -1;
for (int j = 1; j < N; ++j) {
const int w = us[j];
for (; u > w || v > w; ) {
v = u;
u = par[u];
if (!~u) return false;
}
par[w] = u;
u = w;
v = -1;
}
return true;
}
void dfs(int u) {
us.push_back(u);
for (int v = u + 1; v < N; ++v) if (u == P[v]) {
dfs(v);
}
}
void rec(int u) {
if (u == N) {
us.clear();
dfs(0);
vis[us] = P;
} else {
for (P[u] = 0; P[u] < u; ++P[u]) {
rec(u + 1);
}
}
}
void run() {
for (N = 1; N <= 10; ++N) {
P.assign(N, -1);
vis.clear();
rec(1);
for (const auto &kv : vis) {
cerr << kv.first << " " << kv.second << endl;
}
us.resize(N);
for (int u = 0; u < N; ++u) us[u] = u;
do {
auto it = vis.find(us);
const bool brt = (it != vis.end());
const bool res = decide();
if (brt != res) {
cerr << "FAIL: " << us << ": " << brt << " " << res << endl;
}
assert(brt == res);
} while (next_permutation(us.begin() + 1, us.end()));
cerr << "DONE N = " << N << ": " << vis.size() << endl;
}
}
} // exper
/*
tree generated by the greedy decision
attach w to u, last sibling is v: u < w && v < w
if v = -1:
in previous iteration u < w
==> in previous iteration v > w
i.e. (rightmost child of v) > w
this condition is sufficient
PIE for "(rightmost child of v) > w"
-> constraints form a tree
*/
// size, size added to last child
Mint dp[810][810];
int main() {
// exper::run();
int N, MO;
for (; ~scanf("%d%d", &N, &MO); ) {
Mint::setM(MO);
prepare();
memset(dp, 0, sizeof(dp));
dp[1][0] = 1;
for (int n = 2; n <= N; ++n) {
for (int k = 0; n + k <= N; ++k) {
/*
// casework on leftmost subtree...
// single
dp[n][k] += inv[n - 1 + k] * dp[n - 1][0];
// multiple
for (int m = 2; m < n - 1; ++m) {
dp[n][k] += inv[n - 1 + k] * dp[m][0] * dp[n - m][k];
dp[n][k] -= inv[n - 1 + k] * dp[m][n - 1 + k - m] * dp[n - m][k];
}
*/
dp[n][k] += dp[n - 1][0];
for (int m = 2; m < n - 1; ++m) {
dp[n][k] += (dp[m][0] - dp[m][n - 1 + k - m]) * dp[n - m][k];
}
dp[n][k] *= inv[n - 1 + k];
}
// cerr<<n<<": ";for(int k=0;n+k<=N;++k)cerr<<(fac[n+k-1]*dp[n][k])<<" ";cerr<<endl;
}
const Mint ans = fac[N - 1] * dp[N][0];
printf("%u\n", ans.x);
}
return 0;
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 6400kb
input:
4 114514199
output:
2
result:
ok 1 number(s): "2"
Test #2:
score: 0
Accepted
time: 2ms
memory: 6668kb
input:
10 998244353
output:
11033
result:
ok 1 number(s): "11033"
Test #3:
score: 0
Accepted
time: 0ms
memory: 6604kb
input:
100 1000000007
output:
270904395
result:
ok 1 number(s): "270904395"
Test #4:
score: 0
Accepted
time: 178ms
memory: 6656kb
input:
756 1001338769
output:
901942543
result:
ok 1 number(s): "901942543"
Test #5:
score: 0
Accepted
time: 204ms
memory: 6344kb
input:
793 1009036033
output:
301770320
result:
ok 1 number(s): "301770320"
Test #6:
score: 0
Accepted
time: 160ms
memory: 6400kb
input:
759 1005587659
output:
846376219
result:
ok 1 number(s): "846376219"
Test #7:
score: 0
Accepted
time: 188ms
memory: 6340kb
input:
773 1007855479
output:
1398019
result:
ok 1 number(s): "1398019"
Test #8:
score: 0
Accepted
time: 176ms
memory: 6380kb
input:
751 1006730639
output:
321287237
result:
ok 1 number(s): "321287237"
Test #9:
score: 0
Accepted
time: 174ms
memory: 6672kb
input:
778 1007760653
output:
430322899
result:
ok 1 number(s): "430322899"
Test #10:
score: 0
Accepted
time: 212ms
memory: 6668kb
input:
798 1007543827
output:
688720826
result:
ok 1 number(s): "688720826"
Test #11:
score: 0
Accepted
time: 206ms
memory: 6372kb
input:
796 1004841413
output:
258829347
result:
ok 1 number(s): "258829347"
Test #12:
score: 0
Accepted
time: 194ms
memory: 6400kb
input:
775 1005185189
output:
744278608
result:
ok 1 number(s): "744278608"
Test #13:
score: 0
Accepted
time: 214ms
memory: 6624kb
input:
800 1006012831
output:
508549367
result:
ok 1 number(s): "508549367"
Test #14:
score: 0
Accepted
time: 2ms
memory: 6400kb
input:
1 1001338769
output:
1
result:
ok 1 number(s): "1"
Test #15:
score: 0
Accepted
time: 1ms
memory: 6392kb
input:
2 1001338769
output:
1
result:
ok 1 number(s): "1"
Test #16:
score: 0
Accepted
time: 1ms
memory: 6652kb
input:
9 1009036033
output:
1780
result:
ok 1 number(s): "1780"
Test #17:
score: 0
Accepted
time: 0ms
memory: 6376kb
input:
14 1001338769
output:
43297358
result:
ok 1 number(s): "43297358"
Extra Test:
score: 0
Extra Test Passed