結果

問題 No.3304 INCREASE decrease
ユーザー Kude
提出日時 2025-10-05 15:22:49
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 82 ms / 2,000 ms
コード長 3,219 bytes
コンパイル時間 3,433 ms
コンパイル使用メモリ 305,948 KB
実行使用メモリ 7,716 KB
最終ジャッジ日時 2025-10-05 15:23:17
合計ジャッジ時間 7,066 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using mint = modint998244353;

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  vector<pair<int, mint>> trans1[10][1 << 3], trans1_last[10][1 << 3],
                          trans2[10][1 << 3], trans2_last[10][1 << 3];
  rep(n, 10) {
    rep(s, 1 << 2) {
      int cnt[1 << 2]{};
      rep(x, 10) rep(z, 10) {
        if (~s & 1 && x > z) continue;
        if (~s & 2 && z > n) continue;
        int ns = s;
        if (x < z) ns |= 1;
        if (z < n) ns |= 2;
        cnt[ns]++;
      }
      rep(ns, 1 << 2) if (cnt[ns]) trans1[n][s].emplace_back(ns, cnt[ns]);
    }
  }
  rep(n, 10) {
    rep(s, 1 << 2) {
      int cnt[1 << 2]{};
      rep(x, 10) rep(z, 10) {
        if (~s & 1 && x > z) continue;
        if (~s & 2 && z > n) continue;
        if (x % 2 != z % 2) continue;
        int ns = s;
        if (x < z) ns |= 1;
        if (z < n) ns |= 2;
        cnt[ns]++;
      }
      rep(ns, 1 << 2) if (cnt[ns]) trans1_last[n][s].emplace_back(ns, cnt[ns]);
    }
  }
  rep(n, 10) {
    rep(s, 1 << 2) {
      int cnt[1 << 2]{};
      rep(x, 10) rep(z, 10) {
        if (~s & 1 && x < z) continue;
        if (~s & 2 && z > n) continue;
        int ns = s;
        if (x > z) ns |= 1;
        if (z < n) ns |= 2;
        cnt[ns]++;
      }
      rep(ns, 1 << 2) if (cnt[ns]) trans2[n][s].emplace_back(ns, cnt[ns]);
    }
  }
  rep(n, 10) {
    rep(s, 1 << 2) {
      int cnt[1 << 2]{};
      rep(x, 10) rep(z, 10) {
        if (~s & 1 && x < z) continue;
        if (~s & 2 && z > n) continue;
        if (x % 2 != z % 2) continue;
        int ns = s;
        if (x > z) ns |= 1;
        if (z < n) ns |= 2;
        cnt[ns]++;
      }
      rep(ns, 1 << 2) if (cnt[ns]) trans2_last[n][s].emplace_back(ns, cnt[ns]);
    }
  }
  int tt;
  cin >> tt;
  while (tt--) {
    string s;
    int k;
    cin >> s >> k;
    int n = s.size();
    mint dp[4];
    dp[0] = 1;
    rep(i, n - k) {
      int x = s[i] - '0';
      mint ndp[4];
      rep(s, 4) {
        mint v = dp[s];
        for (auto [ns, c] : (i == n - k - 1 ? trans1_last[x][s] : trans1[x][s])) ndp[ns] += c * v;
      }
      swap(dp, ndp);
    }
    dp[0] = dp[1];
    dp[1] = 0;
    dp[2] = dp[3];
    dp[3] = 0;
    for (int i = n - k; i < n; i++) {
      int x = s[i] - '0';
      mint ndp[4];
      rep(s, 4) {
        mint v = dp[s];
        for (auto [ns, c] : (i == n - 1 ? trans2_last[x][s] : trans2[x][s])) ndp[ns] += c * v;
      }
      swap(dp, ndp);
    }
    cout << dp[3].val() << '\n';
  }
}
0