{"id":105,"date":"2022-10-14T17:44:58","date_gmt":"2022-10-14T09:44:58","guid":{"rendered":"http:\/\/43.129.93.179\/?p=105"},"modified":"2022-10-16T23:40:55","modified_gmt":"2022-10-16T15:40:55","slug":"105","status":"publish","type":"post","link":"http:\/\/yuri3.cn\/?p=105","title":{"rendered":"\u8fd1\u671f\u505a\u7684\u4e00\u4e9bAtcoder\u9898"},"content":{"rendered":"<p>\u5927\u56db\u8001\u5e74\u4eba\u5b8c\u5168\u6ca1\u6709\u667a\u529b\uff0c\u4e8e\u662f\u505a\u4e00\u4e9bAtcoder\u9898\u6765\u9884\u9632\u8001\u5e74\u75f4\u5446\u3002<\/p>\n<p><!--more--><\/p>\n<h2>1. AtCoder Beginner Contest 269 Ex<\/h2>\n<p>\u9898\u610f\uff1a<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/atcoder.jp\/contests\/abc269\/tasks\/abc269_h\" title=\"Link\">Link<\/a><\/p>\n<p>\u505a\u6cd5\uff1a\u8003\u8651\u6bcf\u4e2a\u70b9\u7684\u751f\u6210\u51fd\u6570\uff1a<\/p>\n<div class=\"katex math multi-line no-emojify\">G_{u}(x)=\\sum_{i} g_{ui} x^i\n<\/div>\n<p>\u5176\u4e2d\uff0c<span class=\"katex math inline\">g_{ui}<\/span>\u8868\u793a<span class=\"katex math inline\">u<\/span>\u53f7\u70b9\u7684\u5b50\u6811\u4e2d\u9009\u5927\u5c0f\u4e3a<span class=\"katex math inline\">i<\/span>\u7684good vertex set\u7684\u65b9\u6848\u6570\u3002\u8003\u8651\u5377\u79ef\u7684\u7ec4\u5408\u610f\u4e49\uff0c\u6709\uff1a<\/p>\n<p><span class=\"katex math multi-line\">G_{u}(x)=x+\\prod_{v\\in son[u]}{G_{v}(x)}<\/span><\/p>\n<p>\u9700\u8981\u8ba1\u7b97\u7684\u5c31\u662f<span class=\"katex math inline\">G_{root}<\/span>.<br \/>\n\u76f4\u63a5\u66b4\u529b\u5377\u79ef\uff0c\u590d\u6742\u5ea6\u4e0d\u592a\u884c\uff0c\u8003\u8651\u542f\u53d1\u5f0f\u5408\u5e76\uff0c\u6216\u8005\u53ebHLD\uff08\u8f7b\u91cd\u94fe\u5256\u5206\uff09\u3002<br \/>\n\u8f7b\u91cd\u94fe\u5256\u5206\u76f8\u5f53\u4e8e\u662f\u6bcf\u6761\u91cd\u94fe\u5355\u72ec\u5904\u7406\uff0c\u7136\u540e\u201c\u8df3\u201d\u5230\u53e6\u4e00\u6761\u91cd\u94fe\u4e0a\u3002\u8003\u8651\u5f53\u524d\u5904\u7406\u7684\u91cd\u94fe\u4e3a<span class=\"katex math inline\">&#92;{v_{i_1}\uff0cv_{i_2},\\cdots,v_{i_t}&#92;}<\/span>(\u6309\u6df1\u5ea6\u4ece\u6d45\u81f3\u6df1)\u3002\u90a3\u4e48\u8be5\u91cd\u94fe\u6700\u6d45\u7684\u70b9\u5bf9\u5176\u7236\u4eb2\u8d21\u732e\u7684\u751f\u6210\u51fd\u6570\u4e3a\uff1a<\/p>\n<div class=\"katex math multi-line no-emojify\">\\begin{aligned}<br \/>\nG_{v_{top}}(x)=&amp;(((G_{v_{i_t}}(x)+x)\\\\<br \/>\n&amp;\\times G_{v_{i_{t-1}}}(x)+x)\\\\<br \/>\n&amp;\\times\\cdots)\\times G_{v_{i_1}}(x) +x<br \/>\n\\end{aligned}\n<\/div>\n<p>\u8fd9\u6837\u66b4\u529b\u7b97\uff0c\u590d\u6742\u5ea6\u8fd8\u662f\u4e0d\u592a\u5bf9\uff0c\u6b64\u65f6\u8003\u8651\u5206\u6cbb\u3002\u8003\u8651\u4eff\u5c04\u51fd\u6570<span class=\"katex math inline\">\\mathcal{F_{u}}(\\Box)=G_{u}(x)\\times\\Box +x<\/span>\uff0c\u90a3\u4e48\u6539\u5199\u8d21\u732e\uff1a<\/p>\n<p><span class=\"katex math multi-line\">G_{v_{top}}(x)=\\mathcal{F_{i_1}}\\mathcal{F_{i_1}}\\cdots\\mathcal{F_{i_t}}(1)<\/span><\/p>\n<p>\u7ef4\u62a4<span class=\"katex math inline\">A\\times\\Box +B<\/span>\u4e2d<span class=\"katex math inline\">(A,B)<\/span>\u4e24\u4e2a\u591a\u9879\u5f0f\uff0c\u76f4\u63a5\u5206\u6cbbNTT\u5373\u53ef\u3002<br \/>\n\u65f6\u95f4\u590d\u6742\u5ea6\uff1a<span class=\"katex math inline\">\\mathcal{O}(n\\log^3n)<\/span>\u3002<br \/>\n\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<details>\n<summary>Code<\/summary>\n<pre><code class=\"language-cpp line-numbers\">#include &lt;bits\/stdc++.h&gt;\nusing namespace std;\nusing i64 = long long;\n\ntemplate &lt;class T&gt;\nT power(T a, int b) {\n    T res = 1;\n    for (; b; b \/= 2, a *= a) {\n        if (b % 2) {\n            res *= a;\n        }\n    }\n    return res;\n}\ntemplate &lt;int mod&gt;\nstruct mint {\n    int x;\n    mint() : x(0) {}\n    mint(int64_t y) : x(y &gt;= 0 ? y % mod : (mod - (-y) % mod) % mod) {}\n    mint &amp;operator+=(const mint &amp;p) {\n        if ((x += p.x) &gt;= mod) x -= mod;\n        return *this;\n    }\n    mint &amp;operator-=(const mint &amp;p) {\n        if ((x += mod - p.x) &gt;= mod) x -= mod;\n        return *this;\n    }\n    mint &amp;operator*=(const mint &amp;p) {\n        x = (int)(1LL * x * p.x % mod);\n        return *this;\n    }\n    mint &amp;operator\/=(const mint &amp;p) {\n        *this *= p.inverse();\n        return *this;\n    }\n    mint operator-() const { return mint(-x); }\n    mint operator+(const mint &amp;p) const { return mint(*this) += p; }\n    mint operator-(const mint &amp;p) const { return mint(*this) -= p; }\n    mint operator*(const mint &amp;p) const { return mint(*this) *= p; }\n    mint operator\/(const mint &amp;p) const { return mint(*this) \/= p; }\n    bool operator==(const mint &amp;p) const { return x == p.x; }\n    bool operator!=(const mint &amp;p) const { return x != p.x; }\n    mint inverse() const {\n        int a = x, b = mod, u = 1, v = 0, t;\n        while (b &gt; 0) {\n            t = a \/ b;\n            swap(a -= t * b, b);\n            swap(u -= t * v, v);\n        }\n        return mint(u);\n    }\n    friend ostream &amp;operator&lt;&lt;(ostream &amp;os, const mint &amp;p) { return os &lt;&lt; p.x; }\n    friend istream &amp;operator&gt;&gt;(istream &amp;is, mint &amp;a) {\n        int64_t t;\n        is &gt;&gt; t;\n        a = mint&lt;mod&gt;(t);\n        return (is);\n    }\n    int get() const { return x; }\n    static constexpr int get_mod() { return mod; }\n};\n\ntemplate &lt;class Z, int rt&gt;\nstruct NTT {\n    vector&lt;int&gt; rev;\n    vector&lt;Z&gt; roots{0, 1};\n    void dft(vector&lt;Z&gt; &amp;a) {\n        int n = a.size();\n\n        if (int(rev.size()) != n) {\n            int k = __builtin_ctz(n) - 1;\n            rev.resize(n);\n            for (int i = 0; i &lt; n; i++) {\n                rev[i] = rev[i &gt;&gt; 1] &gt;&gt; 1 | (i &amp; 1) &lt;&lt; k;\n            }\n        }\n\n        for (int i = 0; i &lt; n; i++) {\n            if (rev[i] &lt; i) {\n                swap(a[i], a[rev[i]]);\n            }\n        }\n        if (int(roots.size()) &lt; n) {\n            int k = __builtin_ctz(roots.size());\n            roots.resize(n);\n            while ((1 &lt;&lt; k) &lt; n) {\n                Z e = power(Z(rt), (Z::get_mod() - 1) &gt;&gt; (k + 1));\n                for (int i = 1 &lt;&lt; (k - 1); i &lt; (1 &lt;&lt; k); i++) {\n                    roots[2 * i] = roots[i];\n                    roots[2 * i + 1] = roots[i] * e;\n                }\n                k++;\n            }\n        }\n        for (int k = 1; k &lt; n; k *= 2) {\n            for (int i = 0; i &lt; n; i += 2 * k) {\n                for (int j = 0; j &lt; k; j++) {\n                    Z u = a[i + j];\n                    Z v = a[i + j + k] * roots[k + j];\n                    a[i + j] = u + v;\n                    a[i + j + k] = u - v;\n                }\n            }\n        }\n    }\n    void idft(vector&lt;Z&gt; &amp;a) {\n        int n = a.size();\n        reverse(a.begin() + 1, a.end());\n        dft(a);\n        Z inv = (1 - Z::get_mod()) \/ n;\n        for (int i = 0; i &lt; n; i++) {\n            a[i] *= inv;\n        }\n    }\n    vector&lt;Z&gt; multiply(vector&lt;Z&gt; a, vector&lt;Z&gt; b) {\n        int sz = 1, tot = a.size() + b.size() - 1;\n        while (sz &lt; tot) {\n            sz *= 2;\n        }\n\n        a.resize(sz), b.resize(sz);\n        dft(a), dft(b);\n\n        for (int i = 0; i &lt; sz; ++i) {\n            a[i] = a[i] * b[i];\n        }\n\n        idft(a);\n        a.resize(tot);\n        return a;\n    }\n};\n\ntemplate &lt;class Z, int rt&gt;\nstruct Poly {\n    vector&lt;Z&gt; a;\n    Poly() {}\n    Poly(int sz, Z val) { a.assign(sz, val); }\n    Poly(const vector&lt;Z&gt; &amp;a) : a(a) {}\n    Poly(const initializer_list&lt;Z&gt; &amp;a) : a(a) {}\n    int size() const { return a.size(); }\n    void resize(int n) { a.resize(n); }\n    Z operator[](int idx) const {\n        if (idx &lt; size()) {\n            return a[idx];\n        } else {\n            return 0;\n        }\n    }\n    Z &amp;operator[](int idx) { return a[idx]; }\n    Poly mulxk(int k) const {\n        auto b = a;\n        b.insert(b.begin(), k, 0);\n        return Poly(b);\n    }\n    Poly modxk(int k) const {\n        k = min(k, size());\n        return Poly(vector&lt;Z&gt;(a.begin(), a.begin() + k));\n    }\n    Poly divxk(int k) const {\n        if (size() &lt;= k) {\n            return Poly();\n        }\n        return Poly(vector&lt;Z&gt;(a.begin() + k, a.end()));\n    }\n    friend Poly operator+(const Poly &amp;a, const Poly &amp;b) {\n        vector&lt;Z&gt; res(max(a.size(), b.size()));\n        for (int i = 0; i &lt; int(res.size()); i++) {\n            res[i] = a[i] + b[i];\n        }\n        return Poly(res);\n    }\n    friend Poly operator-(const Poly &amp;a, const Poly &amp;b) {\n        vector&lt;Z&gt; res(max(a.size(), b.size()));\n        for (int i = 0; i &lt; int(res.size()); i++) {\n            res[i] = a[i] - b[i];\n        }\n        return Poly(res);\n    }\n\n    friend Poly operator*(Poly a, Poly b) {\n        if (a.size() == 0 || b.size() == 0) {\n            return Poly();\n        }\n        static NTT&lt;Z, rt&gt; ntt;\n        return ntt.multiply(a.a, b.a);\n    }\n    friend Poly operator*(Z a, Poly b) {\n        for (int i = 0; i &lt; int(b.size()); i++) {\n            b[i] *= a;\n        }\n        return b;\n    }\n    friend Poly operator*(Poly a, Z b) {\n        for (int i = 0; i &lt; int(a.size()); i++) {\n            a[i] *= b;\n        }\n        return a;\n    }\n    Poly &amp;operator+=(Poly b) { return (*this) = (*this) + b; }\n    Poly &amp;operator-=(Poly b) { return (*this) = (*this) - b; }\n    Poly &amp;operator*=(Poly b) { return (*this) = (*this) * b; }\n};\nint main() {\n    std::ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    constexpr int mod = 998244353;\n    using Z = mint&lt;mod&gt;;\n    using poly = Poly&lt;Z, 3&gt;;\n    int n;\n    cin &gt;&gt; n;\n\n    vector&lt;vector&lt;int&gt;&gt; t(n + 1);\n    for (int i = 2; i &lt;= n; i++) {\n        int p;\n        cin &gt;&gt; p;\n        t[i].push_back(p);\n        t[p].push_back(i);\n    }\n\n    vector&lt;int&gt; siz(n + 1), son(n + 1), f(n + 1);\n\n    function&lt;void(int, int)&gt; dfs = [&amp;](int u, int fa) {\n        siz[u] = 1;\n        f[u] = fa;\n        for (auto v : t[u]) {\n            if (v == fa) continue;\n            dfs(v, u);\n            siz[u] += siz[v];\n            if (siz[v] &gt; siz[son[u]]) son[u] = v;\n        }\n    };\n    dfs(1, 0);\n    vector&lt;poly&gt; dp(n + 1);\n    function&lt;void(int)&gt; dfs2 = [&amp;](int u) {\n        vector&lt;int&gt; lt;\n        int now = u;\n        while (now) {\n            lt.push_back(now);\n            now = son[now];\n        }\n        for (auto it : lt) {\n            vector&lt;poly&gt; res;\n            for (auto v : t[it]) {\n                if (v == f[it] || v == son[it]) continue;\n                dfs2(v);\n                res.emplace_back(std::move(dp[v]));\n                res.rbegin()-&gt;resize(res.rbegin()-&gt;size() + 1);\n            }\n            function&lt;poly(int, int)&gt; dc = [&amp;](int l, int r) -&gt; poly {\n                if (r - l &lt;= 1) {\n                    if (l == r) return {1, 0};\n                    return res[l];\n                }\n                int mid = (l + r) \/ 2;\n                return dc(l, mid) * dc(mid, r);\n            };\n            dp[it] = dc(0, res.size());\n        }\n        vector&lt;poly&gt; res;\n        for (auto &amp;&amp;it : lt) res.emplace_back(std::move(dp[it]));\n\n        function&lt;pair&lt;poly, poly&gt;(int, int)&gt; dc2 =\n            [&amp;](int l, int r) -&gt; pair&lt;poly, poly&gt; {\n            if (r - l == 1) {\n                return {{0, 1}, res[l]};\n            }\n            int mid = (l + r) \/ 2;\n            auto [al, bl] = dc2(l, mid);\n            auto [ar, br] = dc2(mid, r);\n            return {ar * bl + al, bl * br};\n        };\n\n        auto [x, y] = dc2(0, res.size());\n        dp[u] = x + y;\n        dp[u].resize(dp[u].size() + 1);\n    };\n    dfs2(1);\n    for (int i = 1; i &lt;= n; i++) cout &lt;&lt; dp[1][i] &lt;&lt; '\\n';\n\n    return 0;\n}\n<\/code><\/pre>\n<\/details>\n<h2>2. AtCoder Beginner Contest 268 Ex<\/h2>\n<p>\u9898\u610f\uff1a<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/atcoder.jp\/contests\/abc268\/tasks\/abc268_h\" title=\"Link\">Link<\/a><br \/>\n\u505a\u6cd5\uff1a\u8003\u8651\u5bf9T\u5efa\u51faAC\u81ea\u52a8\u673a\uff0c\u7136\u540e\u5904\u7406\u51faS\u4e2d\u6bcf\u4e2a\u4f4d\u7f6e<span class=\"katex math inline\">i<\/span>\u7684\u6700\u77ed\u80fd\u5339\u914dT\u4e2d\u7684\u957f\u5ea6\u7684\u533a\u95f4\uff0c\u8fd9\u4e2a\u4e1c\u897f\u53ef\u4ee5\u5728fail\u6811\u4e0a\u9884\u5904\u7406\u3002\u7136\u540e\u95ee\u9898\u8f6c\u6362\u6210\u4e86\u533a\u95f4\u9009\u70b9\u4f7f\u5f97\u6bcf\u4e2a\u533a\u95f4\u81f3\u5c11\u6709\u4e00\u4e2a\u70b9\u7684\u95ee\u9898\u3002\u8d2a\u5fc3\u5373\u53ef\u3002<br \/>\n\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<details>\n<summary>Code<\/summary>\n<pre><code class=\"language-cpp line-numbers\">#include &lt;bits\/stdc++.h&gt;\n\nusing namespace std;\nusing i64 = long long;\n\nstruct TrieNode {\n    TrieNode() { id = 0, dep = 0, nxt = array&lt;int, 26&gt;(); };\n    TrieNode(int _id, int _dep) : id(_id), dep(_dep) {}\n    int id;\n    int dep;\n    array&lt;int, 26&gt; nxt = {};\n    int&amp; operator[](const int x) { return this-&gt;nxt[x]; }\n};\ntemplate &lt;class Node&gt;\nstruct trie {\n    vector&lt;Node&gt; tr;\n    trie() { tr.push_back(Node()); };\n\n    int add(const string&amp; s) {\n        int n = s.size();\n        int p = 0;\n        for (int i = 0; i &lt; n; i++) {\n            int c = s[i] - 'a';\n            if (!tr[p][c]) {\n                tr[p][c] = tr.size();\n                tr.emplace_back(tr[p][c], tr[p].dep + 1);\n            }\n            p = tr[p][c];\n        }\n        return p;\n    }\n\n    int size() const { return tr.size(); }\n};\n\ntemplate &lt;class Node&gt;\nstruct ACAutomaton : public trie&lt;Node&gt; {\n    vector&lt;int&gt; fail;\n    ACAutomaton() { this-&gt;tr.push_back(Node()); };\n\n    void BuildAC() {\n        fail.resize(this-&gt;tr.size());\n        queue&lt;int&gt; Q;\n        for (int i = 0; i &lt; 26; i++)\n            if (this-&gt;tr[0][i]) Q.push(this-&gt;tr[0][i]);\n        while (!Q.empty()) {\n            int u = Q.front();\n            Q.pop();\n            for (int i = 0; i &lt; 26; i++) {\n                if (this-&gt;tr[u][i])\n                    fail[this-&gt;tr[u][i]] = this-&gt;tr[fail[u]][i],\n                    Q.push(this-&gt;tr[u][i]);\n                else\n                    this-&gt;tr[u][i] = this-&gt;tr[fail[u]][i];\n            }\n        }\n        return;\n    }\n};\nint main() {\n    std::ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    string s;\n    cin &gt;&gt; s;\n\n    int m;\n    cin &gt;&gt; m;\n    ACAutomaton&lt;TrieNode&gt; ac;\n    vector&lt;int&gt; pos(m + 1);\n    for (int i = 1; i &lt;= m; i++) {\n        string res;\n        cin &gt;&gt; res;\n        pos[i] = ac.add(res);\n    }\n    ac.BuildAC();\n    vector&lt;int&gt; vis(ac.size());\n    for (int i = 1; i &lt;= m; i++) vis[pos[i]] = 1;\n    vector&lt;vector&lt;int&gt;&gt; adj(ac.size());\n    for (int i = 0; i &lt; ac.size(); i++) {\n        if (ac.fail[i] != i) adj[ac.fail[i]].push_back(i);\n    }\n    vector&lt;int&gt; to(ac.size(), -1);\n    queue&lt;int&gt; q;\n    q.push(0);\n    while (!q.empty()) {\n        int u = q.front();\n        q.pop();\n        if ((to[u] == -1) &amp;&amp; vis[u]) {\n            to[u] = u;\n        }\n        for (auto v : adj[u]) {\n            to[v] = to[u];\n            q.push(v);\n        }\n    }\n\n    int p = 0;\n    vector&lt;pair&lt;int, int&gt;&gt; seg;\n\n    for (int i = 0; i &lt; s.size(); i++) {\n        p = ac.tr[p][s[i] - 'a'];\n        if (to[p] == -1) continue;\n        seg.emplace_back(i - ac.tr[to[p]].dep + 1, i);\n    }\n\n    sort(seg.begin(), seg.end(),\n         [&amp;](const pair&lt;int, int&gt;&amp; a, const pair&lt;int, int&gt;&amp; b) {\n             return a.second &lt; b.second;\n         });\n\n    int ans = 0;\n\n    int nowr = -1;\n    for (auto [l, r] : seg) {\n        if (l &gt; nowr) {\n            nowr = max(nowr, r);\n            ans++;\n        }\n    }\n\n    cout &lt;&lt; ans &lt;&lt; endl;\n\n    return 0;\n}\n<\/code><\/pre>\n<\/details>\n<h2>3. AtCoder Regular Contest 151 B<\/h2>\n<p>\u9898\u610f\uff1a<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/atcoder.jp\/contests\/arc151\/tasks\/arc151_b\" title=\"Link\">Link<\/a><br \/>\n\u505a\u6cd5\uff1a\u53ef\u4ee5\u4e00\u6b65\u4e00\u6b65\u5730\u63a8\u5f0f\u5b50\uff0c\u4f46\u662f\u6709\u4e2a\u66f4\u52a0\u5de7\u5999\u7684\u505a\u6cd5\u3002\u8003\u8651\u5230\u5b57\u5178\u5e8f\u4e25\u683c\u5c0f\u4e8e\u548c\u5927\u4e8e\u4e4b\u95f4\u53ef\u4ee5\u901a\u8fc7<span class=\"katex math inline\">i\\leftrightarrow p[i]<\/span>\u5efa\u7acb\u53cc\u5c04\uff0c\u4e8e\u662f\u53ea\u9700\u8981\u8ba1\u7b97\u5b57\u5178\u5e8f\u76f8\u7b49\u7684\u60c5\u51b5\u5373\u53ef\u3002\u663e\u7136\u73af\u5185\u53d6\u76f8\u540c\u6570\u5b57\u53ef\u4ee5\u53d6\u5230\u3002\u8bbe\u6709<span class=\"katex math inline\">c<\/span>\u4e2a\u73af\uff0c\u5219\u7b54\u6848\u4e3a\uff1a<\/p>\n<p><span class=\"katex math multi-line\">ans=\\frac{m^n-m^c}{2}<\/span><\/p>\n<details>\n<summary>Code<\/summary>\n<pre><code class=\"language-cpp line-numbers\">#include &lt;bits\/stdc++.h&gt;\n\nusing namespace std;\nusing i64 = long long;\n\ntemplate &lt;class T&gt;\nT power(T a, int b) {\n    T res = 1;\n    for (; b; b \/= 2, a *= a) {\n        if (b % 2) {\n            res *= a;\n        }\n    }\n    return res;\n}\n\ntemplate &lt;int mod&gt;\nstruct mint {\n    int x;\n    mint() : x(0) {}\n    mint(int64_t y) : x(y &gt;= 0 ? y % mod : (mod - (-y) % mod) % mod) {}\n    mint &amp;operator+=(const mint &amp;p) {\n        if ((x += p.x) &gt;= mod) x -= mod;\n        return *this;\n    }\n    mint &amp;operator-=(const mint &amp;p) {\n        if ((x += mod - p.x) &gt;= mod) x -= mod;\n        return *this;\n    }\n    mint &amp;operator*=(const mint &amp;p) {\n        x = (int)(1LL * x * p.x % mod);\n        return *this;\n    }\n    mint &amp;operator\/=(const mint &amp;p) {\n        *this *= p.inverse();\n        return *this;\n    }\n    mint operator-() const { return mint(-x); }\n    mint operator+(const mint &amp;p) const { return mint(*this) += p; }\n    mint operator-(const mint &amp;p) const { return mint(*this) -= p; }\n    mint operator*(const mint &amp;p) const { return mint(*this) *= p; }\n    mint operator\/(const mint &amp;p) const { return mint(*this) \/= p; }\n    bool operator==(const mint &amp;p) const { return x == p.x; }\n    bool operator!=(const mint &amp;p) const { return x != p.x; }\n    mint inverse() const {\n        int a = x, b = mod, u = 1, v = 0, t;\n        while (b &gt; 0) {\n            t = a \/ b;\n            swap(a -= t * b, b);\n            swap(u -= t * v, v);\n        }\n        return mint(u);\n    }\n    friend ostream &amp;operator&lt;&lt;(ostream &amp;os, const mint &amp;p) { return os &lt;&lt; p.x; }\n    friend istream &amp;operator&gt;&gt;(istream &amp;is, mint &amp;a) {\n        int64_t t;\n        is &gt;&gt; t;\n        a = mint&lt;mod&gt;(t);\n        return (is);\n    }\n    int get() const { return x; }\n    static constexpr int get_mod() { return mod; }\n};\n\nconstexpr int mod = 998244353;\nusing Z = mint&lt;mod&gt;;\nint main() {\n    std::ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    int n, m;\n    cin &gt;&gt; n &gt;&gt; m;\n    if (m == 1) {\n        cout &lt;&lt; 0 &lt;&lt; endl;\n        return 0;\n    }\n\n    vector&lt;int&gt; p(n + 1);\n    for (int i = 1; i &lt;= n; i++) cin &gt;&gt; p[i];\n    vector&lt;Z&gt; fac(n + 1);\n    fac[0] = Z(1);\n    for (int i = 1; i &lt;= n; i++) fac[i] = fac[i - 1] * Z(i);\n    Z ans = 1;\n    vector&lt;int&gt; vis(n + 1);\n    int g = 0;\n    for (int i = 1; i &lt;= n; i++) {\n        if (!vis[i]) {\n            int now = i;\n            vis[now] = 1;\n            int cnt = 1;\n            while (p[now] != i) {\n                now = p[now];\n                vis[now] = 1;\n                cnt++;\n            }\n            g++;\n        }\n    }\n    cout &lt;&lt; Z(power&lt;Z&gt;(m, n) - power&lt;Z&gt;(m, g)) \/ Z(2);\n    return 0;\n}\n<\/code><\/pre>\n<\/details>\n","protected":false},"excerpt":{"rendered":"<p>\u5927\u56db\u8001\u5e74\u4eba\u5b8c\u5168\u6ca1\u6709\u667a\u529b\uff0c\u4e8e\u662f\u505a\u4e00\u4e9bAtcoder\u9898\u6765&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":107,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"http:\/\/yuri3.cn\/index.php?rest_route=\/wp\/v2\/posts\/105"}],"collection":[{"href":"http:\/\/yuri3.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/yuri3.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/yuri3.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/yuri3.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=105"}],"version-history":[{"count":75,"href":"http:\/\/yuri3.cn\/index.php?rest_route=\/wp\/v2\/posts\/105\/revisions"}],"predecessor-version":[{"id":261,"href":"http:\/\/yuri3.cn\/index.php?rest_route=\/wp\/v2\/posts\/105\/revisions\/261"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/yuri3.cn\/index.php?rest_route=\/wp\/v2\/media\/107"}],"wp:attachment":[{"href":"http:\/\/yuri3.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=105"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/yuri3.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=105"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/yuri3.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}