結果
問題 | No.2019 Digits Filling for All Substrings |
ユーザー |
![]() |
提出日時 | 2022-07-23 18:09:54 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 22 ms / 2,000 ms |
コード長 | 1,637 bytes |
コンパイル時間 | 297 ms |
コンパイル使用メモリ | 42,444 KB |
実行使用メモリ | 5,760 KB |
最終ジャッジ日時 | 2024-07-05 07:52:16 |
合計ジャッジ時間 | 2,060 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 30 |
ソースコード
/* -*- coding: utf-8 -*- * * 2019.cc: No.2019 Digits Filling for All Substrings - yukicoder */ #include<cstdio> #include<algorithm> using namespace std; /* constant */ const int MAX_N = 200000; const int MOD = 998244353; /* typedef */ template<const int MOD> struct MI { int v; MI(): v() {} MI(int _v): v(_v % MOD) {} MI(long long _v): v(_v % MOD) {} MI operator+(const MI m) const { return MI(v + m.v); } MI operator-(const MI m) const { return MI(v + MOD - m.v); } MI operator*(const MI m) const { return MI((long long)v * m.v); } MI &operator+=(const MI m) { return (*this = *this + m); } MI &operator-=(const MI m) { return (*this = *this - m); } MI &operator*=(const MI m) { return (*this = *this * m); } MI pow(int n) const { // a^n % MOD MI pm = 1, a = *this; while (n > 0) { if (n & 1) pm *= a; a *= a; n >>= 1; } return pm; } MI inv() const { return pow(MOD - 2); } MI operator/(const MI m) const { return *this * m.inv(); } MI &operator/=(const MI m) { return (*this = *this / m); } }; typedef MI<MOD> mi; /* global variables */ char s[MAX_N + 4]; mi dp[MAX_N + 1][3]; /* subroutines */ /* main */ int main() { int n; scanf("%d%s", &n, s); for (int i = 0; i < n; i++) { int mindi = 0, maxdi = 9; if (s[i] != '?') mindi = maxdi = s[i] - '0'; for (int di = mindi; di <= maxdi; di++) { for (int j = 0; j < 3; j++) { int j1 = (j + di) % 3; dp[i + 1][j1] += dp[i][j]; } dp[i + 1][di % 3] += 1; } } mi sum = 0; for (int i = 1; i <= n; i++) sum += dp[i][0]; printf("%d\n", sum.v); return 0; }