結果

問題 No.3584 Camouflage Mole
コンテスト
ユーザー marc2825
提出日時 2026-05-30 09:12:42
言語 C++17
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 433 ms / 2,000 ms
コード長 1,731 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,178 ms
コンパイル使用メモリ 214,884 KB
実行使用メモリ 5,888 KB
最終ジャッジ日時 2026-07-10 20:57:22
合計ジャッジ時間 3,499 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

using ll = long long;

const ll MOD = 998244353;

ll modpow(ll a, ll e) {
    ll r = 1;
    while (e > 0) {
        if (e & 1) r = r * a % MOD;
        a = a * a % MOD;
        e >>= 1;
    }
    return r;
}

ll solve_formula(ll N) {
    ll ans = 1;
    ans = ans * (N % MOD) % MOD;
    ans = ans * ((N - 1) % MOD) % MOD;
    ans = ans * ((N - 2) % MOD) % MOD;
    ans = ans * ((N - 3) % MOD) % MOD;

    // divide by 4! = 24
    ans = ans * modpow(24, MOD - 2) % MOD;

    ans = ans * modpow(26, N - 4) % MOD;
    return ans;
}

ll count_mole_subseq(const string& s) {
    // dp[t] := target の先頭 t 文字を作る部分列の個数
    string target = "mole";
    vector<ll> dp(5, 0);
    dp[0] = 1;

    for (char c : s) {
        for (int i = 3; i >= 0; i--) {
            if (target[i] == c) {
                dp[i + 1] += dp[i];
            }
        }
    }

    return dp[4];
}

ll solve_bruteforce(int N) {
    string s(N, 'a');
    ll ans = 0;

    auto dfs = [&](auto self, int pos) -> void {
        if (pos == N) {
            ans += count_mole_subseq(s);
            ans %= MOD;
            return;
        }

        for (char c = 'a'; c <= 'z'; c++) {
            s[pos] = c;
            self(self, pos + 1);
        }
    };

    dfs(dfs, 0);
    return ans;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    ll N;
    cin >> N;

    // 入力制約バリデーション
    assert(4 <= N && N <= 200000);

    ll ans;

    if (N <= 5) {
        // バリデーション用の愚直解
        ans = solve_bruteforce((int)N);
    } else {
        // 本解
        ans = solve_formula(N);
    }

    cout << ans << '\n';
    return 0;
}
0