QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#912861#10111. Dividing Chains呆呆鸟 (Weicheng Wang, Anyi Xu, Xiangwen Wang)AC ✓54ms8948kbC++233.7kb2025-02-24 00:20:592025-02-24 00:21:00

Judging History

This is a historical verdict posted at 2025-02-24 00:21:00.

  • [2025-02-25 03:28:07]
  • 自动重测本题所有获得100分的提交记录
  • Verdict: AC
  • Time: 51ms
  • Memory: 8840kb
  • [2025-02-25 03:27:21]
  • hack成功,自动添加数据
  • (/hack/1576)
  • [2025-02-24 00:21:00]
  • Judged
  • Verdict: 100
  • Time: 54ms
  • Memory: 8948kb
  • [2025-02-24 00:20:59]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;

const int md = 998244353;

inline int add(int x, int y) {
    if (x + y >= md) return x + y - md;
    return x + y;
}

inline void addx(int &x, int y) {
    x += y;
    if (x >= md) x -= md;
}

inline int sub(int x, int y) {
    if (x < y) return x - y + md;
    return x - y;
}

inline void subx(int &x, int y) {
    x -= y;
    if (x < 0) x += md;
}

inline int mul(int x, int y) { return 1ull * x * y % md; }

inline int fpow(int x, int y) {
    int ans = 1;
    while (y) {
        if (y & 1) ans = mul(ans, x);
        y >>= 1; x = mul(x, x);
    }
    return ans;
}

const int N = 505;

int dp[N][N], tmpl[N][N], tmpr[N][N], ans[N][N], dp0[N][N];
// dp[l][r] 这个区间不能被划分成若干个上升的段, 能被划分成若干个下降的段的方案数
// 是否等价于 l 不是最小值 && r 不是最大值?
// 如果划分后 l 可能是最小值, 说明右边全是最小值
// 如果划分后 r 可能是最大值, 说明左边全是最大值
int a[N];
int n;

int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &a[i]);
    }
    if (a[1] == a[n]) {
        printf("1\n");
        return 0;
    }
    for (int i = 1; i <= n + 1; i++) {
        dp[i][i] = dp[i][i - 1] = 1;
        ans[i][i] = 1;
        dp0[i][i] = 1;
    }
    for (int len = 2; len <= n; len++) {
        for (int l = 1; l <= n - len + 1; l++) {
            int r = l + len - 1;
            if (a[l] == a[r]) {
                dp[l][r] = 0; // 肯定能再分
                ans[l][r] = 1;
                dp0[l][r] = 1;
                continue;
            }
            for (int i = l; i <= r - 1; i++) {
                dp0[l][r] = add(dp0[l][r], mul(dp[l][i], dp0[i + 1][r]));
            }
            dp[l][r] = dp0[l][r];
            // fprintf(stderr, "dp[%d][%d] = %d\n", l, r, dp[l][r]);
            ans[l][r] = dp[l][r];
            for (int i = l; i <= r - 1; i++) {
                if (a[i] == a[r]) {
                    int val;
                    if (a[i] == a[i - 1]) val = sub(ans[l][i - 1], ans[l][i - 2]);
                    else val = ans[l][i - 1];
                    // fprintf(stderr, "bad r [%d, %d] = %d\n", l, i, val);
                    tmpr[l][r] = add(tmpr[l][r], val);
                    dp[l][r] = sub(dp[l][r], val);
                }
            }
            // 3 4 5 3 3 3 非法
            // 4 3 5 3 3 3 合法
            // 枚举第一段 [i, r], 后面全是 3, [i, r] 是一个上升, [i, r] 不能以 3 作为开头
            for (int i = l + 1; i <= r; i++) {
                if (a[l] == a[i]) {
                    int val;
                    if (a[i] == a[i + 1]) val = sub(ans[i + 1][r], ans[i + 2][r]);
                    else val = ans[i + 1][r];
                    // fprintf(stderr, "bad l [%d, %d] = %d\n", i, r, val);
                    tmpl[l][r] = add(tmpl[l][r], val);
                    dp[l][r] = sub(dp[l][r], val);
                    // fprintf(stderr, "dpr[%d][%d] = %d\n", i, r, dpr[i][r]);
                    // fprintf(stderr, "l = %d, r = %d, i = %d, (%d, %d), dp = %d\n", l, r, i, dp[i][r], dpr[i][r], dp[l][r]);
                }
            }
            ans[l][r] = add(ans[l][r], dp[l][r]);
            dp0[l][r] = add(dp0[l][r], dp[l][r]);
            // fprintf(stderr, "l = %d, r = %d, dp = %d\n", l, r, dp[l][r]);
        }
    }
    printf("%d\n", ans[1][n]);
    return 0;
}

// (1 3 3) 1
// dp[2][4] : (1 3 3 排成下降, 3 3 1)
// 3 (1, 3)

/*

l = 1, r = 2, dp = 998244351, dpl = 0, dpr = 0
l = 1, r = 3, dp = 998244351, dpl = 0, dpr = 0

*/



// 1+1, 1+2, 1+3, 2+1, 2+2, 3+1
// 1 1 2 3

// 3 2 1  1
// 3 1 2  1
// 2 3 1  1
// 2 1 3  1

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3960kb

input:

3
2 3 3

output:

3

result:

ok "3"

Test #2:

score: 0
Accepted
time: 0ms
memory: 7952kb

input:

5
1 1 3 3 5

output:

29

result:

ok "29"

Test #3:

score: 0
Accepted
time: 0ms
memory: 8044kb

input:

9
1 2 3 5 5 6 7 8 9

output:

26276

result:

ok "26276"

Test #4:

score: 0
Accepted
time: 2ms
memory: 8048kb

input:

10
1 2 2 3 4 5 8 10 10 10

output:

42088

result:

ok "42088"

Test #5:

score: 0
Accepted
time: 2ms
memory: 7904kb

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: 8048kb

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: 6068kb

input:

10
2 6 6 7 7 8 9 10 10 10

output:

26684

result:

ok "26684"

Test #8:

score: 0
Accepted
time: 1ms
memory: 4064kb

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: 4012kb

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: 7904kb

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: 4060kb

input:

10
1 3 3 4 5 6 9 9 9 10

output:

42136

result:

ok "42136"

Test #12:

score: 0
Accepted
time: 2ms
memory: 7984kb

input:

10
1 1 1 2 2 4 4 5 5 6

output:

16274

result:

ok "16274"

Test #13:

score: 0
Accepted
time: 1ms
memory: 4024kb

input:

10
1 3 5 5 5 6 7 8 8 9

output:

42088

result:

ok "42088"

Test #14:

score: 0
Accepted
time: 49ms
memory: 8812kb

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: 54ms
memory: 8756kb

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: 51ms
memory: 8828kb

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: 47ms
memory: 8760kb

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: 49ms
memory: 8872kb

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: 50ms
memory: 8772kb

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: 53ms
memory: 8768kb

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: 46ms
memory: 8884kb

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: 47ms
memory: 8940kb

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: 52ms
memory: 8752kb

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: 1ms
memory: 3844kb

input:

1
1

output:

1

result:

ok "1"

Test #25:

score: 0
Accepted
time: 49ms
memory: 8948kb

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: 3864kb

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: 0
Extra Test Passed