結果

問題 No.2131 Concon Substrings (COuNt Version)
ユーザー k1suxuk1suxu
提出日時 2022-11-25 22:23:24
言語 C++23(draft)
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,069 ms / 2,000 ms
コード長 8,413 bytes
コンパイル時間 5,220 ms
コンパイル使用メモリ 276,160 KB
実行使用メモリ 495,668 KB
最終ジャッジ日時 2023-07-25 10:34:09
合計ジャッジ時間 12,341 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,356 KB
testcase_02 AC 1 ms
4,356 KB
testcase_03 AC 435 ms
226,624 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 2 ms
4,356 KB
testcase_06 AC 1 ms
4,356 KB
testcase_07 AC 1,069 ms
495,668 KB
testcase_08 AC 246 ms
126,352 KB
testcase_09 AC 1,005 ms
495,596 KB
testcase_10 AC 965 ms
493,224 KB
testcase_11 AC 666 ms
345,184 KB
testcase_12 AC 623 ms
327,372 KB
testcase_13 AC 2 ms
4,356 KB
testcase_14 AC 284 ms
147,120 KB
testcase_15 AC 232 ms
119,352 KB
testcase_16 AC 3 ms
4,356 KB
testcase_17 AC 206 ms
106,480 KB
testcase_18 AC 209 ms
108,080 KB
testcase_19 AC 99 ms
56,064 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

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

#define rep(i,n) for(int i = 0; i < (int)n; i++)
#define FOR(n) for(int i = 0; i < (int)n; i++)
#define repi(i,a,b) for(int i = (int)a; i < (int)b; i++)
#define pb push_back
#define all(x) x.begin(),x.end()
//#define mp make_pair
#define vi vector<int>
#define vvi vector<vi>
#define vll vector<ll>
#define vvll vector<vll>
#define vs vector<string>
#define vvs vector<vs>
#define vc vector<char>
#define vvc vector<vc>
#define pii pair<int,int>
#define pllll pair<ll,ll>
#define vpii vector<pair<int,int>>
#define vpllll vector<pair<ll,ll>>
#define vpis vector<pair<int,string>>
#define vplls vector<pair<ll, string>>
#define vpsi vector<pair<string, int>>
#define vpsll vector<pair<string, ll>>

template<typename T>
void chmax(T &a, const T &b) {a = (a > b? a : b);}
template<typename T>
void chmin(T &a, const T &b) {a = (a < b? a : b);}

using ll = long long;
using ld = long double;
using ull = unsigned long long;

const ll INF = numeric_limits<long long>::max() / 2;
const ld pi = 3.1415926535897932384626433832795028;
const ll mod = 998244353;
int dx[] = {-1, 0, 1, 0, -1, -1, 1, 1};
int dy[] = {0, -1, 0, 1, -1, 1, -1, 1};

#define int long long

template<int MOD>
struct Modular_Int {
    int x;

    Modular_Int() = default;
    Modular_Int(int x_) : x(x_ >= 0? x_%MOD : (MOD-(-x_)%MOD)%MOD) {}

    int val() const {
        return (x%MOD+MOD)%MOD;
    }
    int get_mod() const {
        return MOD;
    }

    Modular_Int<MOD>& operator^=(int d)  {
        Modular_Int<MOD> ret(1);
        int nx = x;
        while(d) {
            if(d&1) ret *= nx;
            (nx *= nx) %= MOD;
            d >>= 1;
        }
        *this = ret;
        return *this;
    }
    Modular_Int<MOD> operator^(int d) const {return Modular_Int<MOD>(*this) ^= d;}
    Modular_Int<MOD> pow(int d) const {return Modular_Int<MOD>(*this) ^= d;}
    
    //use this basically
    Modular_Int<MOD> inv() const {
        return Modular_Int<MOD>(*this) ^ (MOD-2);
    }
    //only if the module number is not prime
    //Don't use. This is broken.
    // Modular_Int<MOD> inv() const {
    //     int a = (x%MOD+MOD)%MOD, b = MOD, u = 1, v = 0;
    //     while(b) {
    //         int t = a/b;
    //         a -= t*b, swap(a, b);
    //         u -= t*v, swap(u, v);
    //     }
    //     return Modular_Int<MOD>(u);
    // }

    Modular_Int<MOD>& operator+=(const Modular_Int<MOD> other) {
        if((x += other.x) >= MOD) x -= MOD;
        return *this;
    }
    Modular_Int<MOD>& operator-=(const Modular_Int<MOD> other) {
        if((x -= other.x) < 0) x += MOD;
        return *this;
    }
    Modular_Int<MOD>& operator*=(const Modular_Int<MOD> other) {
        int z = x;
        z *= other.x;
        z %= MOD;
        x = z;
        if(x < 0) x += MOD;
        return *this;
    }
    Modular_Int<MOD>& operator/=(const Modular_Int<MOD> other) {
        return *this = *this * other.inv();
    }
    Modular_Int<MOD>& operator++() {
        x++;
        if (x == MOD) x = 0;
        return *this;
    }
    Modular_Int<MOD>& operator--() {
        if (x == 0) x = MOD;
        x--;
        return *this;
    }
    
    Modular_Int<MOD> operator+(const Modular_Int<MOD> other) const {return Modular_Int<MOD>(*this) += other;}
    Modular_Int<MOD> operator-(const Modular_Int<MOD> other) const {return Modular_Int<MOD>(*this) -= other;}
    Modular_Int<MOD> operator*(const Modular_Int<MOD> other) const {return Modular_Int<MOD>(*this) *= other;}
    Modular_Int<MOD> operator/(const Modular_Int<MOD> other) const {return Modular_Int<MOD>(*this) /= other;}
    
    Modular_Int<MOD>& operator+=(const int other) {Modular_Int<MOD> other_(other); *this += other_; return *this;}
    Modular_Int<MOD>& operator-=(const int other) {Modular_Int<MOD> other_(other); *this -= other_; return *this;}
    Modular_Int<MOD>& operator*=(const int other) {Modular_Int<MOD> other_(other); *this *= other_; return *this;}
    Modular_Int<MOD>& operator/=(const int other) {Modular_Int<MOD> other_(other); *this /= other_; return *this;}
    Modular_Int<MOD> operator+(const int other) const {return Modular_Int<MOD>(*this) += other;}
    Modular_Int<MOD> operator-(const int other) const {return Modular_Int<MOD>(*this) -= other;}
    Modular_Int<MOD> operator*(const int other) const {return Modular_Int<MOD>(*this) *= other;}
    Modular_Int<MOD> operator/(const int other) const {return Modular_Int<MOD>(*this) /= other;}

    bool operator==(const Modular_Int<MOD> other) const {return (*this).val() == other.val();}
    bool operator!=(const Modular_Int<MOD> other) const {return (*this).val() != other.val();}
    bool operator==(const int other) const {return (*this).val() == other;}
    bool operator!=(const int other) const {return (*this).val() != other;}

    Modular_Int<MOD> operator-() const {return Modular_Int<MOD>(0LL)-Modular_Int<MOD>(*this);}

    //入れ子にしたい
    // friend constexpr istream& operator>>(istream& is, mint& x) noexcept {
    //     int X;
    //     is >> X;
    //     x = X;
    //     return is;
    // }
    // friend constexpr ostream& operator<<(ostream& os, mint& x) {
    //     os << x.val();
    //     return os;
    // }
};

// const int MOD_VAL = 1e9+7;
const int MOD_VAL = 998244353;
using mint = Modular_Int<MOD_VAL>;

istream& operator>>(istream& is, mint& x) {
    int X;
    is >> X;
    x = X;
    return is;
}
ostream& operator<<(ostream& os, mint& x) {
    os << x.val();
    return os;
}

// istream& operator<<(istream& is, mint &a) {
//     int x;
//     is >> x;
//     a = mint(x);
//     return is;
// }
// ostream& operator<<(ostream& os, mint a) {
//     os << a.val();
//     return os;
// }

vector<mint> f = {1}, rf = {1};
void init(int n) {
    f.resize(n, 0);
    rf.resize(n, 0);
    f[0] = 1;
    repi(i, 1, n) f[i] = (f[i - 1] * i);
    repi(i, 0, n) rf[i] = f[i].inv();
}
mint P(int n, int k) {
    assert(n>=k);
    while(n > f.size()-1) {
        f.push_back(f.back() * f.size());
        rf.push_back(f.back().inv());
    }
    return f[n] * f[n-k];
}
mint C(int n, int k) {
    assert(n>=k);
    while(n > f.size()-1) {
        f.push_back(f.back() * f.size());
        rf.push_back(f.back().inv());
    }
    return f[n]*rf[n-k]*rf[k];
}
mint H(int n, int k) {
    assert(n>=1);
    return C(n+k-1, k);
}
mint Cat(int n) {
    return C(2*n, n)-C(2*n, n-1);
}

void solve() {
    int n;
    cin >> n;

    vector<vector<vector<mint>>> dp(n+1, vector<vector<mint>>(n, vector<mint>(3, 0)));
    dp[0][0][0] = 1;

    rep(i, n) {
        rep(k, n) rep(l, 3) dp[i+1][k][l] += dp[i][k][l] * 23;
        rep(j, n) rep(k, 3) rep(l, 3) {
            if(k == l) {
                if(k == 2) {
                    if(j != n-1) dp[i+1][j+1][0] += dp[i][j][2];
                }else {
                    dp[i+1][j][l+1] += dp[i][j][l];
                }
            }else {
                dp[i+1][j][l] += dp[i][j][l];
            }
        }
    }
    
    mint ans = 0;
    rep(i, n) rep(j, 3) ans += dp[n][i][j] * i;
    cout << ans.val() << endl;
    // mint ans = 0;
    // mint minus = 0;
    // for(int i = n/3; i >= 1; i--) {
    //     mint now = C(n, 3*i) * mint(26)^(n-3*i);
    //     ans += (now - minus) * i;
    //     minus += now;
    // }
    // cout << ans.val() << endl;
}

// void naive() {
//     auto score = [](string s) -> int {
//         string t = "con";
//         int tar = 0, ret = 0;
//         rep(i, s.size()) {
//             if(s[i] == t[tar]) {
//                 tar++;
//                 if(tar == 3) {
//                     ret++;
//                     tar = 0;
//                 }
//             }
//         }
//         return ret;
//     };

//     int n;
//     cin >> n;
//     string s = "";
//     int ans = 0;
//     auto dfs = [&](int len, auto self) -> void {
//         if(len == n) {
//             if(score(s) > 0) cout << s << " -> " << score(s) << endl;
//             ans += score(s);
//             return;
//         }
//         rep(i, 26) {
//             s.push_back((char)('a' + i));
//             self(len+1, self);
//             s.pop_back();
//         }
//     };
//     dfs(0, dfs);
//     cout << ans << endl;
// }

signed main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    solve();
    // naive();
    return 0;
}
0