QOJ.ac
QOJ
ID | Submission ID | Problem | Hacker | Owner | Result | Submit time | Judge time |
---|---|---|---|---|---|---|---|
#754 | #494869 | #9141. Array Spread | snuke | ucup-team004 | Failed. | 2024-07-29 03:58:22 | 2024-07-29 03:58:22 |
Details
Extra Test:
Accepted
time: 0ms
memory: 3744kb
input:
1 4 5 1 3 2 4 1 2 2 3 3 4
output:
499122178
result:
ok 1 number(s): "499122178"
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#494869 | #9141. Array Spread | ucup-team004# | AC ✓ | 179ms | 66140kb | C++20 | 4.9kb | 2024-07-27 17:22:22 | 2024-10-14 07:39:25 |
answer
#include <bits/stdc++.h>
using u32 = unsigned;
using i64 = long long;
using u64 = unsigned long long;
// TODO: Dynamic ModInt
template<typename T>
constexpr T power(T a, u64 b) {
T res {1};
for (; b != 0; b /= 2, a *= a) {
if (b % 2 == 1) {
res *= a;
}
}
return res;
}
template<u32 P>
constexpr u32 mulMod(u32 a, u32 b) {
return 1ULL * a * b % P;
}
template<u64 P>
constexpr u64 mulMod(u64 a, u64 b) {
u64 res = a * b - u64(1.L * a * b / P - 0.5L) * P;
res %= P;
return res;
}
template<typename U, U P>
requires std::unsigned_integral<U>
struct ModIntBase {
public:
constexpr ModIntBase() : x {0} {}
template<typename T>
requires std::integral<T>
constexpr ModIntBase(T x_) : x {norm(x_ % T {P})} {}
constexpr static U norm(U x) {
if ((x >> (8 * sizeof(U) - 1) & 1) == 1) {
x += P;
}
if (x >= P) {
x -= P;
}
return x;
}
constexpr U val() const {
return x;
}
constexpr ModIntBase operator-() const {
ModIntBase res;
res.x = norm(P - x);
return res;
}
constexpr ModIntBase inv() const {
return power(*this, P - 2);
}
constexpr ModIntBase &operator*=(const ModIntBase &rhs) & {
x = mulMod<P>(x, rhs.val());
return *this;
}
constexpr ModIntBase &operator+=(const ModIntBase &rhs) & {
x = norm(x + rhs.x);
return *this;
}
constexpr ModIntBase &operator-=(const ModIntBase &rhs) & {
x = norm(x - rhs.x);
return *this;
}
constexpr ModIntBase &operator/=(const ModIntBase &rhs) & {
return *this *= rhs.inv();
}
friend constexpr ModIntBase operator*(ModIntBase lhs, const ModIntBase &rhs) {
lhs *= rhs;
return lhs;
}
friend constexpr ModIntBase operator+(ModIntBase lhs, const ModIntBase &rhs) {
lhs += rhs;
return lhs;
}
friend constexpr ModIntBase operator-(ModIntBase lhs, const ModIntBase &rhs) {
lhs -= rhs;
return lhs;
}
friend constexpr ModIntBase operator/(ModIntBase lhs, const ModIntBase &rhs) {
lhs /= rhs;
return lhs;
}
friend constexpr std::ostream &operator<<(std::ostream &os, const ModIntBase &a) {
return os << a.val();
}
friend constexpr bool operator==(ModIntBase lhs, ModIntBase rhs) {
return lhs.val() == rhs.val();
}
friend constexpr bool operator!=(ModIntBase lhs, ModIntBase rhs) {
return lhs.val() != rhs.val();
}
friend constexpr bool operator<(ModIntBase lhs, ModIntBase rhs) {
return lhs.val() < rhs.val();
}
private:
U x;
};
template<u32 P>
using ModInt = ModIntBase<u32, P>;
template<u64 P>
using ModInt64 = ModIntBase<u64, P>;
constexpr u32 P = 998244353;
using Z = ModInt<P>;
void solve() {
int n, m;
std::cin >> n >> m;
std::vector<int> l(m), r(m);
std::vector<int> v;
v.reserve(2 * m);
for (int i = 0; i < m; i++) {
std::cin >> l[i] >> r[i];
l[i]--;
v.push_back(l[i]);
v.push_back(r[i]);
}
std::sort(v.begin(), v.end());
v.erase(std::unique(v.begin(), v.end()), v.end());
for (int i = 0; i < m; i++) {
l[i] = std::lower_bound(v.begin(), v.end(), l[i]) - v.begin();
r[i] = std::lower_bound(v.begin(), v.end(), r[i]) - v.begin();
}
n = v.size();
std::vector<int> p(m);
std::iota(p.begin(), p.end(), 0);
std::sort(p.begin(), p.end(),
[&](int i, int j) {
return r[i] > r[j];
});
std::vector dp(n + 1, std::vector<int>(n));
for (int i = 0; i <= n; i++) {
if (i >= 1) {
for (int j = 0; j < m; j++) {
dp[i][r[j]] = std::min(dp[i][r[j]], dp[i - 1][l[j]]);
}
}
for (int j = n - 1, k = 0; j > 0; j--) {
dp[i][j - 1] = std::min(dp[i][j - 1], dp[i][j]);
while (k < m && r[p[k]] == j) {
int x = p[k++];
dp[i][l[x]] = std::min(dp[i][l[x]], dp[i][r[x]] - 1);
}
}
}
int P = 0, Q = 1;
for (int j = 0; j < n; j++) {
int mP = 1, mQ = 0;
for (int i = 0; i < n; i++) {
int p = dp[i][j] - dp[n][j], q = n - i;
if (p * mQ < mP * q) {
mP = p;
mQ = q;
}
}
if (mP * Q > P * mQ) {
P = mP;
Q = mQ;
}
}
std::cout << Z(P) / Q << "\n";
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
std::cin >> t;
while (t--) {
solve();
}
return 0;
}