結果

問題 No.2019 Digits Filling for All Substrings
ユーザー kaichou243kaichou243
提出日時 2022-07-22 23:34:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 27 ms / 2,000 ms
コード長 3,090 bytes
コンパイル時間 2,217 ms
コンパイル使用メモリ 204,936 KB
実行使用メモリ 14,216 KB
最終ジャッジ日時 2023-09-17 12:45:37
合計ジャッジ時間 4,156 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 18 ms
10,324 KB
testcase_05 AC 14 ms
8,100 KB
testcase_06 AC 13 ms
7,652 KB
testcase_07 AC 8 ms
6,240 KB
testcase_08 AC 26 ms
14,096 KB
testcase_09 AC 27 ms
14,084 KB
testcase_10 AC 26 ms
14,128 KB
testcase_11 AC 27 ms
14,216 KB
testcase_12 AC 26 ms
14,088 KB
testcase_13 AC 27 ms
14,200 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 3 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 14 ms
7,824 KB
testcase_25 AC 18 ms
9,992 KB
testcase_26 AC 9 ms
6,476 KB
testcase_27 AC 3 ms
4,380 KB
testcase_28 AC 4 ms
4,376 KB
testcase_29 AC 17 ms
9,372 KB
testcase_30 AC 23 ms
12,044 KB
testcase_31 AC 22 ms
11,768 KB
testcase_32 AC 8 ms
5,676 KB
testcase_33 AC 18 ms
9,752 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
template<int MOD> struct Fp{
  ll val;
  constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
    if (val < 0) val += MOD;
  }
  static constexpr int getmod() { return MOD; }
  constexpr Fp operator - () const noexcept {
    return val ? MOD - val : 0;
  }
  constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }
  constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }
  constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }
  constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }
  constexpr Fp& operator += (const Fp& r) noexcept {
    val += r.val;
    if (val >= MOD) val -= MOD;
    return *this;
  }
  constexpr Fp& operator -= (const Fp& r) noexcept {
    val -= r.val;
    if (val < 0) val += MOD;
    return *this;
  }
  constexpr Fp& operator *= (const Fp& r) noexcept {
    val = val * r.val % MOD;
    return *this;
  }
  constexpr Fp& operator /= (const Fp& r) noexcept {
    ll a = r.val, b = MOD, u = 1, v = 0;
    while (b) {
      ll t = a / b;
      a -= t * b, swap(a, b);
      u -= t * v, swap(u, v);
    }
    val = val * u % MOD;
    if (val < 0) val += MOD;
    return *this;
  }
  constexpr bool operator == (const Fp& r) const noexcept {
    return this->val == r.val;
  }
  constexpr bool operator != (const Fp& r) const noexcept {
    return this->val != r.val;
  }
  constexpr bool operator < (const Fp& r) const noexcept {
    return this->val < r.val;
  }
  friend constexpr istream& operator >> (istream& is, Fp<MOD>& x) noexcept {
    is >> x.val;
    x.val %= MOD;
    if (x.val < 0) x.val += MOD;
    return is;
  }
  friend constexpr ostream& operator << (ostream& os, const Fp<MOD>& x) noexcept {
    return os << x.val;
  }
  friend constexpr Fp<MOD> modpow(const Fp<MOD>& a, long long n) noexcept {
    Fp<MOD> res=1,r=a;
    while(n){
      if(n&1) res*=r;
      r*=r;
      n>>=1;
    }
    return res;
  }
  friend constexpr Fp<MOD> modinv(const Fp<MOD>& r) noexcept {
        long long a = r.val, b = MOD, u = 1, v = 0;
        while (b) {
            long long t = a / b;
            a -= t * b, swap(a, b);
            u -= t * v, swap(u, v);
        }
        return Fp<MOD>(u);
  }
  explicit operator bool()const{
		return val;
  }
};
using mint=Fp<998244353>;
int main(){
    int n;
    string S;
    cin>>n>>S;
    vector<vector<mint>> dp(n+1,vector<mint>(3));
    for(int i=1;i<=n;i++){
        if(S[i-1]=='?'){
            for(int j=0;j<3;j++){
                dp[i][j]+=dp[i-1][j]*mint(4);
                dp[i][(j+1)%3]+=dp[i-1][j]*mint(3);
                dp[i][(j+2)%3]+=dp[i-1][j]*mint(3);
            }
            dp[i][0]+=mint(4);
            dp[i][1]+=mint(3);
            dp[i][2]+=mint(3);
        }else{
            for(int j=0;j<3;j++){
                dp[i][(j+S[i-1]-'0')%3]+=dp[i-1][j];
            }
            dp[i][(S[i-1]-'0')%3]+=1;
        }
    }
    mint ans=0;
    for(int r=1;r<=n;r++){
        ans+=dp[r][0];
    }
    cout<<ans<<endl;
}
0