結果

問題 No.2302 Carry X Times
ユーザー NAMIDAIRONAMIDAIRO
提出日時 2023-05-12 23:42:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 12 ms / 2,000 ms
コード長 4,442 bytes
コンパイル時間 1,675 ms
コンパイル使用メモリ 128,800 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-06 13:39:43
合計ジャッジ時間 2,574 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
5,248 KB
testcase_01 AC 7 ms
5,376 KB
testcase_02 AC 8 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 11 ms
5,376 KB
testcase_11 AC 11 ms
5,376 KB
testcase_12 AC 11 ms
5,376 KB
testcase_13 AC 10 ms
5,376 KB
testcase_14 AC 10 ms
5,376 KB
testcase_15 AC 11 ms
5,376 KB
testcase_16 AC 11 ms
5,376 KB
testcase_17 AC 12 ms
5,376 KB
testcase_18 AC 11 ms
5,376 KB
testcase_19 AC 11 ms
5,376 KB
testcase_20 AC 11 ms
5,376 KB
testcase_21 AC 11 ms
5,376 KB
testcase_22 AC 11 ms
5,376 KB
testcase_23 AC 11 ms
5,376 KB
testcase_24 AC 12 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <random>
#include <iomanip>
#include <string>
#include <cmath>
#include <complex>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)

template<class T>
using vi = vector<T>;

template<class T>
using vii = vector<vi<T>>;

template<class T>
using viii = vector<vii<T>>;

template<class T>
using viiii = vector<viii<T>>;

using P = pair<ll, int>;

void chmin(ll & x, ll y) { x = min(x, y); }

void chmax(ll& x, ll y) { x = max(x, y); }

struct mint {
    const long long mod = 998244353;
    long long x;
    mint(long long x_ = 0) : x((x_% mod + mod) % mod) {}

    mint& operator+=(const mint& other) {
        x += other.x;
        if (x >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint& other) {
        x -= other.x;
        if (x < 0) x += mod;
        return *this;
    }
    mint& operator*=(const mint& other) {
        x *= other.x;
        x %= mod;
        return *this;
    }

    mint& operator+=(const long long n) {
        return *this += mint(n);
    }
    mint& operator-=(const long long n) {
        return *this -= mint(n);
    }
    mint& operator*=(const long long n) {
        return *this *= mint(n);
    }

    mint& operator=(const mint& other) {
        x = other.x;
        return *this;
    }
    mint& operator=(const long long n) {
        x = n % mod;
        return *this;
    }

    bool operator==(const mint& other) const {
        return x == other.x;
    }
    bool operator!=(const mint& other) const {
        return x != other.x;
    }

    mint operator-() const {
        mint res(mod - x);
        return res;
    }

    mint operator+(const mint& other) const {
        mint res(x);
        return res += other;
    }
    mint operator-(const mint& other) const {
        mint res(x);
        return res -= other;
    }
    mint operator*(const mint& other) const {
        mint res(x);
        return res *= other;
    }

    mint operator+(const long long n) const {
        mint res(x);
        mint other(n);
        return res += other;
    }
    mint operator-(const long long n) const {
        mint res(x);
        mint other(n);
        return res -= other;
    }
    mint operator*(const long long n) const {
        mint res(x);
        mint other(n);
        return res *= other;
    }

    mint pow(long long n) const {
        if (n == 0) return mint(1);
        mint res = pow(n / 2);
        res *= res;
        if (n % 2) res *= *this;
        return res;
    }
    mint inv() const {
        return pow(mod - 2);
    }
    mint& operator/=(const mint& other) {
        *this *= other.inv();
        return *this;
    }
    mint operator/(const mint& other) const {
        mint res(x);
        return res /= other;
    }
};

mint solve(string &s, int x) {
    int n = (int)s.size();    
    if (x > n) return 0;
    //dp[i][j][k][l]: aがs未満(i)、bがs未満(j)で下に繰り上がりを要求する(k). l回繰り上がりした
    viiii<mint> dp(2, viii<mint>(2, vii<mint>(2, vi<mint>(n + 1)))); 
    dp[0][0][0][0] = 1;
    dp[0][0][1][0] = 1; //here?

    rep(_, n) {
        viiii<mint> prev(2, viii<mint>(2, vii<mint>(2, vi<mint>(n + 1))));
        swap(prev, dp);
        int num = s[_] - '0';
        rep(i, 2) rep(j, 2) rep(k, 2) rep(nk, 2) {
            for (int a = 0; a <= (i ? 9 : num); a++) {
                for (int b = 0; b <= (j ? 9 : num); b++) {
                    for (int l = 0; l <= n; l++) {
                        int carry = 0;
                        if (a + b + nk >= 10) carry = 1;
                        if (k == carry) {
                            if (k == 0) {
                                dp[i || (a < num)][j || (b < num)][nk][l] += prev[i][j][k][l];
                            }
                            else {
                                if(l + 1 <= n) dp[i || (a < num)][j || (b < num)][nk][l + 1] += prev[i][j][k][l];
                            }
                        }
                    }                    
                }
            }
        }
    }

    mint res = 0;
    rep(i, 2) rep(j, 2) res += dp[i][j][0][x];
    return res;
}

int main()
{
    int t;
    cin >> t;
    while (t--) {
        string n; int x;
        cin >> n >> x;
        mint ans = solve(n, x);
        cout << ans.x << endl;
    }
    return 0;
}
0