結果

問題 No.2019 Digits Filling for All Substrings
ユーザー milanis48663220milanis48663220
提出日時 2022-07-22 22:28:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 24 ms / 2,000 ms
コード長 2,721 bytes
コンパイル時間 1,178 ms
コンパイル使用メモリ 124,040 KB
実行使用メモリ 14,276 KB
最終ジャッジ日時 2023-09-17 10:54:29
合計ジャッジ時間 2,782 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 15 ms
10,376 KB
testcase_05 AC 12 ms
8,072 KB
testcase_06 AC 11 ms
7,544 KB
testcase_07 AC 8 ms
6,288 KB
testcase_08 AC 22 ms
14,196 KB
testcase_09 AC 23 ms
14,196 KB
testcase_10 AC 23 ms
14,276 KB
testcase_11 AC 23 ms
14,220 KB
testcase_12 AC 24 ms
14,216 KB
testcase_13 AC 24 ms
14,208 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 13 ms
7,800 KB
testcase_25 AC 17 ms
10,124 KB
testcase_26 AC 10 ms
6,476 KB
testcase_27 AC 3 ms
4,376 KB
testcase_28 AC 3 ms
4,380 KB
testcase_29 AC 15 ms
9,344 KB
testcase_30 AC 21 ms
12,140 KB
testcase_31 AC 21 ms
11,900 KB
testcase_32 AC 7 ms
5,708 KB
testcase_33 AC 16 ms
9,584 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>
#include <atcoder/modint>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}

using mint = atcoder::modint998244353;

ostream& operator<<(ostream& os, const mint& m){
    os << m.val();
    return os;
}

mint pow(mint a, ll n) {
    assert(n >= 0);
	mint ans = 1;
    while (n > 0) {
        if (n&1) ans = ans*a;
        a = a*a;
        n >>= 1;
    }
	return ans;
}

/**
 * f(x) = g(x)*h(x) + r(x)となる(h(x), r(x))を求めます
 */ 
template<typename T>
pair<vector<T>, vector<T>> naive_divide(vector<T> f, vector<T> g){
    int n = f.size()-1;
    int m = g.size()-1;
    if(n < m) return make_pair(vector<T>(1, T(0)), f);
    int k = n-m;
    vector<T> h(k+1);
    T iv = T(1)/g[m];
    for(int i = k; i >= 0; i--){
        h[i] = f[i+m]*iv;
        for(int j = 0; j <= m; j++){
            f[i+j] -= g[j]*h[i];
        }
    }
    return make_pair(h, f);
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n; cin >> n;
    string s; cin >> s;
    auto dp = vec2d(n+1, 3, mint(0));
    dp[0][0] = 1;
    vector<int> cnt = {4, 3, 3};
    mint ans = 0;
    for(int i = 0; i < n; i++){
        if(s[i] == '?'){
            for(int j = 0; j < 3; j++){
                for(int k = 0; k < 3; k++){
                    dp[i+1][(j+k)%3] += dp[i][j]*cnt[k];
                }
            }
        }else{
            int r = (s[i]-'0')%3;
            for(int j = 0; j < 3; j++){
                dp[i+1][(j+r)%3] += dp[i][j];
            }
        }
        ans += dp[i+1][0];
        dp[i+1][0] += 1;
    }
    cout << ans << endl;
}
0