跳到正文

题解

题解:P11856 [CSP-J2022 山东] 吟诗

lkrqwq约 7 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 7 分钟
获赞
2
题目
P11856

版本

  1. v1当前

CSP-J 怎么出 AC 自动机啊。

把这道题类比为字符串匹配,要求找到序列中的一个子串满足第一个分界点的前缀为 xx,第二个分界点的前缀为 x+yx + y(整个子串的和必然为 x+y+zx + y + z)。那么我们考虑枚举所有可能的子串组合作为模式串加入 AC 自动机,那么令 fi,sf_{i, s} 表示前 ii 个数字且当前自动机状态为 ss 的不含模式串方案数,转移也很显然。

最终答案就是 10n−∑jfn,j\displaystyle 10^n - \sum_{j}f_{n, j},时间复杂度 O(1.998x+y+z⋅n2+nS)\mathcal O(1.998^{x + y + z}\cdot n^2 + nS),其中 SS 是插入的字符数量之和,容易证明很小。

代码如下:

C++
#define MULTI_TEST 0

constexpr LL mod = 998244353;

struct ACAM {
    struct Node {
        bool ter;
        int fail, nxt[11];
        Node() : ter(false), fail(0) {
            for (int i = 0; i < 11; ++i)
                nxt[i] = -1;
        }
    };
    std::vector<Node> c;
    void insert(const VI& p) {
        int cur = 0;
        for (auto& d : p) {
            if (c[cur].nxt[d] == -1) {
                c[cur].nxt[d] = c.size(), c.eb(Node());
            }
            cur = c[cur].nxt[d];
        }
        c[cur].ter = true;
    }
    void build() {
        std::queue<int> q;
        for (int d = 1; d <= 10; ++d) {
            if (c[0].nxt[d] == -1)
                c[0].nxt[d] = 0;
            else {
                c[c[0].nxt[d]].fail = 0;
                q.push(c[0].nxt[d]);
            }
        }
        while (!q.empty()) {
            int x = q.front();
            q.pop();
            c[x].ter |= c[c[x].fail].ter;
            for (int d = 1; d <= 10; ++d) {
                int y = c[x].nxt[d];
                if (y != -1) {
                    c[y].fail = c[c[x].fail].nxt[d];
                    q.push(y);
                }
                else {
                    c[x].nxt[d] = c[c[x].fail].nxt[d];
                }
            }
        }
    }
};

int main() {
#if MULTI_TEST == 1
    LL T;
    std::cin >> T;
    while (T--) {
        // Test cases...
    }
#else
    int n, x, y, z, t;
    std::cin >> n >> x >> y >> z;
    t = x + y + z;
    ACAM ac;
    VI cur;
    int cnt = 0;
    auto Add = [&](auto&& self, int r) -> void {
        if (r == t) {
            ++cnt;
            if ((int)cur.size() > 2) {
                int n = cur.size(), s = 0;
                bool ok = false;
                for (int i = 0; i < n; ++i) {
                    if ((s += cur[i]) == x) {
                        int s1 = s;
                        for (int j = i + 1; j < n; ++j) {
                            if ((s1 += cur[j]) == x + y) {
                                if (j < n) ok = true;
                                break;
                            }
                            if (s1 > x + y) break;
                        }
                        if (ok) break;
                    }
                    if (s > x) break;
                }
                if (ok) ac.insert(cur);
            }
            return;
        }
        for (int d = 1; d <= 10; ++d) {
            if (r + d <= t) {
                cur.eb(d), self(self, r + d);
                cur.pop_back();
            }
        }
        };
    ac.c.clear();
    ac.c.push_back(ACAM::Node());
    Add(Add, 0);
    ac.build();
    int s = ac.c.size();
    std::vector<VLL> f(n + 1, VLL(s, 0));
    f[0][0] = 1;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < s; ++j) {
            if (f[i][j] == 0) continue;
            for (int d = 1; d <= 10; ++d) {
                if (ac.c[ac.c[j].nxt[d]].ter) continue;
                f[i + 1][ac.c[j].nxt[d]] += f[i][j];
                f[i + 1][ac.c[j].nxt[d]] %= mod;
            }
        }
    }
    LL ans = 1;
    for (int i = 1; i <= n; ++i) ans = ans * 10 % mod;
    for (int j = 0; j < s; ++j)
        ans -= f[n][j], ans += mod, ans %= mod;
    std::cout << (ans + mod) % mod << "\n";
#endif
    return 0;
}

喜提目前最优解。

原文