結果

問題 No.2129 Perfect Binary Tree...?
ユーザー 遭難者遭難者
提出日時 2022-10-27 15:44:46
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 89 ms / 3,000 ms
コード長 3,518 bytes
コンパイル時間 4,884 ms
コンパイル使用メモリ 275,160 KB
実行使用メモリ 24,648 KB
最終ジャッジ日時 2024-07-08 02:29:12
合計ジャッジ時間 6,321 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 7 ms
6,944 KB
testcase_04 AC 8 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 20 ms
9,308 KB
testcase_07 AC 17 ms
8,064 KB
testcase_08 AC 47 ms
15,244 KB
testcase_09 AC 49 ms
15,668 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 12 ms
6,940 KB
testcase_12 AC 46 ms
14,996 KB
testcase_13 AC 88 ms
24,644 KB
testcase_14 AC 53 ms
15,732 KB
testcase_15 AC 52 ms
14,172 KB
testcase_16 AC 86 ms
21,524 KB
testcase_17 AC 10 ms
6,940 KB
testcase_18 AC 6 ms
6,944 KB
testcase_19 AC 9 ms
6,940 KB
testcase_20 AC 15 ms
7,040 KB
testcase_21 AC 9 ms
6,940 KB
testcase_22 AC 10 ms
6,940 KB
testcase_23 AC 86 ms
22,156 KB
testcase_24 AC 89 ms
22,548 KB
testcase_25 AC 30 ms
11,264 KB
testcase_26 AC 7 ms
6,944 KB
testcase_27 AC 22 ms
8,892 KB
testcase_28 AC 47 ms
15,776 KB
testcase_29 AC 50 ms
16,852 KB
testcase_30 AC 88 ms
24,648 KB
testcase_31 AC 47 ms
15,664 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
#define rep(i, n) for (int i = 0; i < n; i++)
#define ll long long
using namespace atcoder;
using namespace std;
#define mint modint998244353
int main()
{
    int n;
    cin >> n;
    int nn = n << 1;
    vector<mint> two(nn + 1);
    two[0] = 1;
    for (int i = 1; i <= nn; i++)
        two[i] = 2 * two[i - 1];
    vector<mint> f(n + 1), g(n + 1);
    for (int i = 1; i <= n; i++)
    {
        f[i] = (i + 3) * two[i] + (i - 3) * two[i << 1];
        g[i] = (i - 2) * two[i] + 2;
    }
    string u, v;
    cin >> u >> v;
    if (u == v)
    {
        cout << f[n].val() << endl;
        return 0;
    }
    int lca = 0;
    for (int i = 0, maxx = min(u.length(), v.length()); i < maxx; i++)
    {
        if (u[i] != v[i])
            break;
        lca++;
    }
    const int m = u.length() + v.length() - 2 * lca + 1;
    vector<int> p(m);
    p[0] = u.length();
    int cnt = 0;
    while (p[cnt] != lca)
    {
        p[cnt + 1] = p[cnt] - 1;
        cnt++;
    }
    const int lca_cnt = cnt;
    while (cnt != m - 1)
    {
        p[cnt + 1] = p[cnt] + 1;
        cnt++;
    }
    for (int i = 0; i < m; i++)
        p[i]--;
    vector<mint> a(m), b(m), c(m);
    a[0] = two[n - p[0]] - 1;
    a[m - 1] = two[n - p[m - 1]] - 1;
    a[lca_cnt] = 1;
    for (int k = 1, d = p[lca_cnt]; k <= d; k++)
        a[lca_cnt] += two[n - k];
    if (lca_cnt == 0 || lca_cnt == m - 1)
        a[lca_cnt] += two[n - p[lca_cnt] - 1] - 1;
    for (int i = 1; i < m - 1; i++)
    {
        if (i == lca_cnt)
            continue;
        a[i] = two[n - 1 - p[i]];
    }
    b[0] = g[n - p[0]];
    b[m - 1] = g[n - p[m - 1]];
    b[lca_cnt] = 0;
    for (int k = 1, d = p[lca_cnt]; k <= d; k++)
        b[lca_cnt] += g[n - k] + (two[n - k] - 1) * (d + 2 - k) + k;
    if (lca_cnt == 0 || lca_cnt == m - 1)
        b[lca_cnt] += g[n - p[lca_cnt] - 1] + two[n - p[lca_cnt] - 1] - 1;
    for (int i = 1; i < m - 1; i++)
    {
        if (i == lca_cnt)
            continue;
        b[i] = g[n - 1 - p[i]] + two[n - 1 - p[i]] - 1;
    }
    c[0] = f[n - p[0]];
    c[m - 1] = f[n - p[m - 1]];
    c[lca_cnt] = 0;
    for (int k = 1, d = p[lca_cnt]; k <= d; k++)
    {
        c[lca_cnt] += f[n - k];
        c[lca_cnt] += g[n - k] * (a[lca_cnt] - two[n - k] + 1);
        c[lca_cnt] += (two[n - k] - 1) * (a[lca_cnt] - two[n - k] + 1);
        c[lca_cnt] += (two[n] - two[n - k]) * (a[lca_cnt] - two[n] + two[n - k]);
    }
    if (lca_cnt == 0 || lca_cnt == m - 1)
    {
        c[lca_cnt] += f[n - p[lca_cnt] - 1];
        c[lca_cnt] += g[n - p[lca_cnt] - 1] * (a[lca_cnt] - two[n - p[lca_cnt] - 1] + 1);
        c[lca_cnt] += (two[n - p[lca_cnt] - 1] - 1) * (a[lca_cnt] - two[n - p[lca_cnt] - 1] + 1);
    }
    for (int i = 1; i < m - 1; i++)
    {
        if (i == lca_cnt)
            continue;
        c[i] = f[n - 1 - p[i]] + g[n - 1 - p[i]] + two[n - 1 - p[i]] - 1;
    }
    mint sum_a = 0, sum_b = 0, sum_c = 0, sum_ab = 0;
    for (mint i : a)
        sum_a += i;
    for (mint i : b)
        sum_b += i;
    for (mint i : c)
        sum_c += i;
    for (int i = 0; i < m; i++)
        sum_ab += a[i] * b[i];
    mint ans = sum_c + sum_a * sum_b - sum_ab;
    vector<mint> aa(m);
    for (int i = 0; i < m; i++)
        aa[m - 1 - i] = a[i];
    vector<mint> tt = convolution(a, aa);
    mint sum = 0;
    for (int j = 1; j < m; j++)
        sum += min(j, m - j) * (tt[m - 1 - j] + tt[2 * m - 1 - j]);
    ans += sum / 2;
    cout << ans.val() << endl;
    return 0;
}
0