QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#914233 | #10111. Dividing Chains | thinking (Fedor Romashov, Alexey Mikhnenko, Gimran Abdullin) | TL | 113ms | 5376kb | C++20 | 7.4kb | 2025-02-25 03:22:45 | 2025-02-25 03:28:21 |
Judging History
This is the latest submission verdict.
- [2025-02-25 03:27:21]
- hack成功,自动添加数据
- (/hack/1576)
- [2025-02-25 03:22:45]
- Submitted
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define all(a) begin(a), end(a)
#define len(a) int((a).size())
#ifdef LOCAL
#include "debug.h"
#else
#define dbg(...)
#define dprint(...)
#define debug if constexpr (false)
#endif // LOCAL
/*
! WARNING: MOD must be prime if you use division or .inv().
! WARNING: 2 * (MOD - 1) must be smaller than INT_MAX
* Use .value to get the stored value.
*/
template<typename T>
int normalize(T value, int mod) {
if (value < -mod || value >= 2 * mod) value %= mod;
if (value < 0) value += mod;
if (value >= mod) value -= mod;
return value;
}
template<int mod>
struct static_modular_int {
static_assert(mod - 2 <= std::numeric_limits<int>::max() - mod, "2(mod - 1) <= INT_MAX");
using mint = static_modular_int<mod>;
int value;
static_modular_int() : value(0) {}
static_modular_int(const mint &x) : value(x.value) {}
template<typename T, typename U = std::enable_if_t<std::is_integral<T>::value>>
static_modular_int(T value) : value(normalize(value, mod)) {}
static constexpr int get_mod() {
return mod;
}
template<typename T>
mint power(T degree) const {
mint prod = 1, a = *this;
for (; degree > 0; degree >>= 1, a *= a)
if (degree & 1)
prod *= a;
return prod;
}
mint inv() const {
return power(mod - 2);
}
mint& operator=(const mint &x) {
value = x.value;
return *this;
}
mint& operator+=(const mint &x) {
value += x.value;
if (value >= mod) value -= mod;
return *this;
}
mint& operator-=(const mint &x) {
value -= x.value;
if (value < 0) value += mod;
return *this;
}
mint& operator*=(const mint &x) {
value = int64_t(value) * x.value % mod;
return *this;
}
mint& operator/=(const mint &x) {
return *this *= x.inv();
}
friend mint operator+(const mint &x, const mint &y) {
return mint(x) += y;
}
friend mint operator-(const mint &x, const mint &y) {
return mint(x) -= y;
}
friend mint operator*(const mint &x, const mint &y) {
return mint(x) *= y;
}
friend mint operator/(const mint &x, const mint &y) {
return mint(x) /= y;
}
mint& operator++() {
++value;
if (value == mod) value = 0;
return *this;
}
mint& operator--() {
--value;
if (value == -1) value = mod - 1;
return *this;
}
mint operator++(int) {
mint prev = *this;
value++;
if (value == mod) value = 0;
return prev;
}
mint operator--(int) {
mint prev = *this;
value--;
if (value == -1) value = mod - 1;
return prev;
}
mint operator-() const {
return mint(0) - *this;
}
bool operator==(const mint &x) const {
return value == x.value;
}
bool operator!=(const mint &x) const {
return value != x.value;
}
bool operator<(const mint &x) const {
return value < x.value;
}
template<typename T>
explicit operator T() {
return value;
}
friend std::istream& operator>>(std::istream &in, mint &x) {
std::string s;
in >> s;
x = 0;
bool neg = s[0] == '-';
for (const auto c : s)
if (c != '-')
x = x * 10 + (c - '0');
if (neg)
x *= -1;
return in;
}
friend std::ostream& operator<<(std::ostream &out, const mint &x) {
return out << x.value;
}
static int primitive_root() {
if constexpr (mod == 1'000'000'007)
return 5;
if constexpr (mod == 998'244'353)
return 3;
if constexpr (mod == 786433)
return 10;
static int root = -1;
if (root != -1)
return root;
std::vector<int> primes;
int value = mod - 1;
for (int i = 2; i * i <= value; i++)
if (value % i == 0) {
primes.push_back(i);
while (value % i == 0)
value /= i;
}
if (value != 1)
primes.push_back(value);
for (int r = 2;; r++) {
bool ok = true;
for (auto p : primes)
if ((mint(r).power((mod - 1) / p)).value == 1) {
ok = false;
break;
}
if (ok)
return root = r;
}
}
};
// constexpr int MOD = 1'000'000'007;
constexpr int MOD = 998'244'353;
using mint = static_modular_int<MOD>;
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n;
cin >> n;
vector<int> a(n);
for (auto &x : a) {
cin >> x;
}
vector f(n, vector<mint>(n)); // both orders are valid
vector g(n, vector<mint>(n)); // at least one order is valid
for (int d = 0; d < n; d++) {
for (int l = 0; l + d < n; l++) {
int r = l + d;
if (a[l] == a[r]) {
f[l][r] = g[l][r] = 1;
continue;
}
g[l][r] += g[l + 1][r] * 2;
for (int m = l + 1; m < r; m++) {
g[l][r] += (g[l][m] - f[l][m]) * g[m + 1][r];
}
int cnt_mx = 0;
while (a[r - cnt_mx] == a[r]) {
cnt_mx++;
}
int cnt_mn = 0;
while (a[l + cnt_mn] == a[l]) {
cnt_mn++;
}
{ // mx mx mx .... mx mx mx
auto place = [&](int left, int right) -> mint {
if (left + right > cnt_mx) {
return 0;
}
return g[l][r - left - right];
};
for (int left = 1; left < cnt_mx; left++) {
for (int right = 1; left + right <= cnt_mx; right++) {
mint ways = 0;
ways += place(left, right);
ways -= place(left + 1, right);
ways -= place(left, right + 1);
ways += place(left + 1, right + 1);
f[l][r] += ways;
}
}
}
{ // mn mn mn .... mn mn mn
auto place = [&](int left, int right) -> mint {
if (left + right > cnt_mn) {
return 0;
}
return g[l + left + right][r];
};
for (int left = 1; left < cnt_mn; left++) {
for (int right = 1; left + right <= cnt_mn; right++) {
mint ways = 0;
ways += place(left, right);
ways -= place(left + 1, right);
ways -= place(left, right + 1);
ways += place(left + 1, right + 1);
f[l][r] += ways;
}
}
}
g[l][r] -= f[l][r];
}
}
dbg(f);
dbg(g);
cout << g[0][n - 1] << '\n';
// cout << f[0][n - 1] << '\n';
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3584kb
input:
3 2 3 3
output:
3
result:
ok "3"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3584kb
input:
5 1 1 3 3 5
output:
29
result:
ok "29"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3584kb
input:
9 1 2 3 5 5 6 7 8 9
output:
26276
result:
ok "26276"
Test #4:
score: 0
Accepted
time: 1ms
memory: 3584kb
input:
10 1 2 2 3 4 5 8 10 10 10
output:
42088
result:
ok "42088"
Test #5:
score: 0
Accepted
time: 0ms
memory: 3584kb
input:
10 1 2 4 5 5 5 5 6 10 10
output:
17210
result:
ok "17210"
Test #6:
score: 0
Accepted
time: 0ms
memory: 3584kb
input:
10 1 2 3 5 6 6 6 8 9 9
output:
41826
result:
ok "41826"
Test #7:
score: 0
Accepted
time: 1ms
memory: 3584kb
input:
10 2 6 6 7 7 8 9 10 10 10
output:
26684
result:
ok "26684"
Test #8:
score: 0
Accepted
time: 0ms
memory: 3712kb
input:
10 1 1 3 3 4 5 9 9 10 10
output:
33378
result:
ok "33378"
Test #9:
score: 0
Accepted
time: 1ms
memory: 3584kb
input:
10 1 1 2 3 4 4 9 9 10 10
output:
33348
result:
ok "33348"
Test #10:
score: 0
Accepted
time: 0ms
memory: 3584kb
input:
10 1 3 4 4 5 5 8 8 10 10
output:
32826
result:
ok "32826"
Test #11:
score: 0
Accepted
time: 1ms
memory: 3584kb
input:
10 1 3 3 4 5 6 9 9 9 10
output:
42136
result:
ok "42136"
Test #12:
score: 0
Accepted
time: 0ms
memory: 3584kb
input:
10 1 1 1 2 2 4 4 5 5 6
output:
16274
result:
ok "16274"
Test #13:
score: 0
Accepted
time: 0ms
memory: 3584kb
input:
10 1 3 5 5 5 6 7 8 8 9
output:
42088
result:
ok "42088"
Test #14:
score: 0
Accepted
time: 113ms
memory: 5248kb
input:
500 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9 10 10 10 10 10 10 10 10 11 11 11 11 11 11 11 11 11 11 12 12 12 12 12 12 12 12 12...
output:
316934967
result:
ok "316934967"
Test #15:
score: 0
Accepted
time: 74ms
memory: 5248kb
input:
500 1 1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 4 4 4 4 5 5 5 5 5 5 5 5 5 6 6 7 7 7 7 7 8 8 9 9 10 10 10 10 10 11 11 11 11 12 12 12 12 12 13 13 14 14 14 14 15 15 15 16 16 16 17 17 17 17 17 17 17 18 18 18 18 18 18 19 19 19 19 19 19 19 19 20 21 21 21 21 21 21 22 22 22 22 22 22 22 22 22 23 23 23 23 23 23 23 ...
output:
211204090
result:
ok "211204090"
Test #16:
score: 0
Accepted
time: 65ms
memory: 5376kb
input:
500 1 1 1 2 3 4 4 4 4 4 4 4 4 5 5 5 6 7 7 7 7 8 8 9 9 10 10 10 10 10 10 11 11 11 11 11 11 12 12 12 12 12 12 13 13 13 14 14 14 14 15 15 15 16 16 16 16 18 19 19 19 19 19 20 20 21 21 21 22 22 22 22 22 22 23 23 24 24 24 24 24 24 24 25 26 26 27 27 27 28 28 29 29 29 30 30 31 31 31 31 31 32 32 32 32 33 33 ...
output:
66910402
result:
ok "66910402"
Test #17:
score: 0
Accepted
time: 60ms
memory: 5248kb
input:
500 1 1 2 2 2 2 3 4 4 4 4 4 5 6 6 7 8 8 8 9 9 9 9 9 10 10 11 12 12 13 13 14 14 15 15 15 16 16 17 17 17 17 17 17 18 18 18 19 19 19 19 19 20 20 21 21 21 21 22 22 22 23 23 24 24 25 25 25 25 26 26 26 27 27 27 28 28 28 28 29 30 30 31 31 32 32 33 33 34 34 34 35 35 35 36 36 36 36 37 37 37 37 37 37 38 38 39...
output:
778673575
result:
ok "778673575"
Test #18:
score: 0
Accepted
time: 58ms
memory: 5376kb
input:
500 1 1 1 2 4 4 4 5 6 6 6 7 7 8 8 8 8 9 9 13 14 14 14 15 15 16 16 16 16 16 17 18 19 20 21 21 21 22 22 22 22 23 23 24 24 24 25 25 26 26 26 27 27 28 28 28 28 28 28 30 30 30 31 31 32 32 33 34 34 34 35 36 36 36 37 37 39 39 39 40 40 40 41 42 42 42 43 43 43 44 44 45 45 45 46 47 48 48 48 49 49 49 49 50 51 ...
output:
626585352
result:
ok "626585352"
Test #19:
score: 0
Accepted
time: 58ms
memory: 5120kb
input:
500 1 2 3 3 4 5 5 5 6 6 8 8 8 10 11 12 13 13 13 13 13 13 14 14 15 15 17 18 18 18 18 19 19 20 20 20 21 21 21 25 25 26 26 27 27 29 30 30 30 32 33 33 33 34 34 34 35 36 36 37 37 37 38 38 38 39 40 40 40 42 42 43 44 44 45 45 46 47 47 48 50 54 54 55 55 57 57 57 58 59 59 60 61 62 63 63 63 64 65 65 66 66 66 ...
output:
918943801
result:
ok "918943801"
Test #20:
score: 0
Accepted
time: 56ms
memory: 5376kb
input:
500 1 1 2 3 3 3 4 4 5 6 7 9 9 9 11 12 13 13 14 14 14 14 15 15 16 19 19 20 22 23 24 25 25 26 26 27 30 31 32 33 33 33 34 35 35 36 37 38 38 39 43 43 45 45 46 46 47 47 48 48 50 51 51 52 52 52 52 53 54 57 58 58 58 59 60 60 61 62 62 63 63 64 64 64 65 66 66 66 68 68 70 70 70 72 72 73 73 73 75 75 75 76 78 7...
output:
593100090
result:
ok "593100090"
Test #21:
score: 0
Accepted
time: 56ms
memory: 5376kb
input:
500 1 2 2 3 4 4 5 5 6 6 7 8 9 11 11 12 12 13 14 14 14 14 16 17 19 19 20 21 22 22 24 26 26 26 27 27 28 28 29 29 35 39 40 41 42 43 46 47 48 49 49 50 50 51 52 52 52 56 56 56 56 57 59 59 59 60 61 61 65 66 68 69 69 71 71 72 72 73 73 74 74 75 75 76 76 76 79 80 82 82 84 85 87 87 88 88 89 90 91 91 91 94 95 ...
output:
51077903
result:
ok "51077903"
Test #22:
score: 0
Accepted
time: 58ms
memory: 5376kb
input:
500 1 2 3 3 4 4 4 5 6 6 7 9 10 11 11 12 14 14 15 15 15 16 16 17 17 17 18 18 18 18 18 19 19 20 20 21 22 23 24 25 25 25 31 32 32 32 32 33 33 34 35 36 36 37 37 38 38 45 46 47 48 50 50 51 52 52 53 56 58 60 61 61 61 61 62 62 62 62 63 63 63 63 63 64 64 65 67 67 68 71 72 73 74 74 75 75 76 77 77 77 77 77 77...
output:
645029056
result:
ok "645029056"
Test #23:
score: 0
Accepted
time: 57ms
memory: 5376kb
input:
500 1 1 2 2 2 6 8 8 9 9 11 11 11 12 13 13 13 13 15 16 16 19 19 19 20 20 20 20 22 23 24 25 25 26 27 29 31 31 33 33 33 35 35 39 39 39 40 40 40 45 46 46 47 47 47 48 49 50 52 53 53 53 54 55 57 57 58 59 61 62 64 64 66 66 66 68 69 70 71 72 74 74 75 75 76 76 78 80 81 83 83 84 85 85 85 86 88 89 90 90 91 92 ...
output:
799979293
result:
ok "799979293"
Test #24:
score: 0
Accepted
time: 0ms
memory: 3584kb
input:
1 1
output:
1
result:
ok "1"
Test #25:
score: 0
Accepted
time: 55ms
memory: 5248kb
input:
500 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 ...
output:
903664836
result:
ok "903664836"
Test #26:
score: 0
Accepted
time: 1ms
memory: 5376kb
input:
500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 ...
output:
1
result:
ok "1"
Extra Test:
score: -3
Extra Test Failed : Time Limit Exceeded on 4
input:
500 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...