結果

問題 No.1195 数え上げを愛したい(文字列編)
ユーザー ForestedForested
提出日時 2020-08-24 16:34:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,452 ms / 3,000 ms
コード長 4,585 bytes
コンパイル時間 2,542 ms
コンパイル使用メモリ 215,120 KB
実行使用メモリ 34,792 KB
最終ジャッジ日時 2024-11-06 10:23:01
合計ジャッジ時間 40,190 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2,379 ms
23,468 KB
testcase_01 AC 2,276 ms
23,532 KB
testcase_02 AC 2,373 ms
23,540 KB
testcase_03 AC 553 ms
9,912 KB
testcase_04 AC 715 ms
12,792 KB
testcase_05 AC 2,452 ms
34,792 KB
testcase_06 AC 2 ms
6,820 KB
testcase_07 AC 2 ms
6,820 KB
testcase_08 AC 262 ms
6,824 KB
testcase_09 AC 2,374 ms
23,340 KB
testcase_10 AC 1,164 ms
13,952 KB
testcase_11 AC 2,326 ms
22,820 KB
testcase_12 AC 2,356 ms
22,152 KB
testcase_13 AC 1,179 ms
16,000 KB
testcase_14 AC 1,166 ms
13,184 KB
testcase_15 AC 1,150 ms
13,952 KB
testcase_16 AC 1,148 ms
13,312 KB
testcase_17 AC 537 ms
7,936 KB
testcase_18 AC 2,353 ms
22,060 KB
testcase_19 AC 2,347 ms
22,060 KB
testcase_20 AC 1,165 ms
16,172 KB
testcase_21 AC 2,335 ms
22,668 KB
testcase_22 AC 1,151 ms
15,616 KB
testcase_23 AC 2 ms
6,820 KB
testcase_24 AC 2 ms
6,816 KB
testcase_25 AC 2 ms
6,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// Template
#include <bits/stdc++.h>
#define rep_override(x, y, z, name, ...) name
#define rep2(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep3(i, l, r) for (int i = (int)(l); i < (int)(r); ++i)
#define rep(...) rep_override(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)
#define per(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
constexpr int inf = 1001001001;
constexpr ll INF = 3003003003003003003;
template <typename T> inline bool chmin(T& x, const T& y) {if (x > y) {x = y; return 1;} return 0;}
template <typename T> inline bool chmax(T& x, const T& y) {if (x < y) {x = y; return 1;} return 0;}
struct IOSET {IOSET() {cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(10);}} ioset;

// Number-Theoretical Transform
// for example, (998244353, 31), (1012924417, 198), (1811939329, 136)
template <int mod, int base>
struct NumberTheoreticalTransform {
    vector<ll> zeta, inv_zeta;
    NumberTheoreticalTransform() {
        size_t exponents = 0;
        int tmp = mod - 1;
        while (not (tmp & 1)) {
            tmp >>= 1;
            ++exponents;
        }
        zeta.resize(exponents + 1);
        inv_zeta.resize(exponents + 1);
        zeta[exponents] = base;
        inv_zeta[exponents] = mod_pow(base, mod - 2);
        for (size_t i = exponents; i > 0; --i) {
            zeta[i - 1] = zeta[i] * zeta[i] % mod;
            inv_zeta[i - 1] = inv_zeta[i] * inv_zeta[i] % mod;
        }
    }
    ll mod_pow(ll x, ll y) {
        if (not y) return 1;
        ll a = mod_pow(x, y >> 1);
        if (y & 1) return a * a % mod * x % mod;
        return a * a % mod;
    }
    inline ll add(ll x, ll y) {
        if ((x += y) >= mod) x -= mod;
        return x;
    }
    void dft(vector<ll> &a, size_t exponents) {
        size_t m = 1 << exponents, now_e = exponents;
        while (m > 1) {
            for (size_t i = 0; i < a.size() / m; ++i) {
                ll now = 1;
                for (size_t j = 0; j < m / 2; ++j) {
                    ll l = a[m * i + j];
                    ll r = a[m * i + j + m / 2];
                    a[m * i + j] = add(l, r);
                    a[m * i + j + m / 2] = add(l, mod - r) * now % mod;
                    now = now * zeta[now_e] % mod;
                }
            }
            m >>= 1;
            --now_e;
        }
    }
    void idft(vector<ll> &a, size_t exponents) {
        size_t m = 2, now_e = 1;
        while (m <= a.size()) {
            for (size_t i = 0; i < a.size() / m; ++i) {
                ll now = 1;
                for (size_t j = 0; j < m / 2; ++j) {
                    ll l = a[m * i + j];
                    ll r = a[m * i + j + m / 2] * now % mod;
                    a[m * i + j] = add(l, r);
                    a[m * i + j + m / 2] = add(l, mod - r);
                    now = now * inv_zeta[now_e] % mod;
                }
            }
            m <<= 1;
            ++now_e;
        }
    }
    vector<ll> multiply(vector<ll> f, vector<ll> g) {
        size_t siz = 1, exp = 0;
        while (siz < f.size() + g.size()) {
            siz *= 2;
            ++exp;
        }
        vector<ll> nf(siz, 0), ng(siz, 0);
        for (size_t i = 0; i < f.size(); ++i) nf[i] = f[i];
        for (size_t i = 0; i < g.size(); ++i) ng[i] = g[i];
        dft(nf, exp);
        dft(ng, exp);
        for (size_t i = 0; i < siz; ++i) nf[i] = nf[i] * ng[i] % mod;
        idft(nf, exp);
        ll inv = mod_pow(siz, mod - 2);
        for (size_t i = 0; i < siz; ++i) nf[i] = nf[i] * inv % mod;
        return nf;
    }
};

// Main
constexpr int MOD = 998244353, ROOT = 31;

int main() {
    NumberTheoreticalTransform<MOD, ROOT> ntt;
    
    string s;
    cin >> s;
    
    vector<int> cnt(26, 0);
    for (char c: s) ++cnt[c - 'a'];
    sort(all(cnt));
    
    vector<ll> fact(s.size() + 1), inv_fact(s.size() + 1);
    fact[0] = 1;
    rep(i, s.size()) fact[i + 1] = fact[i] * (i + 1) % MOD;
    inv_fact[s.size()] = ntt.mod_pow(fact[s.size()], MOD - 2);
    per(i, s.size()) inv_fact[i] = inv_fact[i + 1] * (i + 1) % MOD;
    
    vector<ll> dp(s.size() + 1, 0);
    dp[0] = 1;
    rep(i, 26) {
        vector<ll> f(s.size() + 1), g(cnt[i] + 1);
        rep(j, s.size() + 1) f[j] = dp[j] * inv_fact[j] % MOD;
        rep(j, cnt[i] + 1) g[j] = inv_fact[j] % MOD;
        vector<ll> m = ntt.multiply(f, g);
        rep(i, s.size() + 1) dp[i] = m[i] * fact[i] % MOD;
    }
    
    ll ans = 0;
    rep(i, 1, s.size() + 1) ans += dp[i];
    ans %= MOD;
    cout << ans << "\n";
    return 0;
}
0