結果

問題 No.2302 Carry X Times
ユーザー KKT89KKT89
提出日時 2023-05-12 22:17:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 57 ms / 2,000 ms
コード長 3,561 bytes
コンパイル時間 2,517 ms
コンパイル使用メモリ 223,588 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-06 12:21:46
合計ジャッジ時間 4,069 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
5,248 KB
testcase_01 AC 9 ms
5,376 KB
testcase_02 AC 18 ms
5,376 KB
testcase_03 AC 1 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 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 56 ms
5,376 KB
testcase_11 AC 52 ms
5,376 KB
testcase_12 AC 52 ms
5,376 KB
testcase_13 AC 46 ms
5,376 KB
testcase_14 AC 57 ms
5,376 KB
testcase_15 AC 31 ms
5,376 KB
testcase_16 AC 28 ms
5,376 KB
testcase_17 AC 52 ms
5,376 KB
testcase_18 AC 38 ms
5,376 KB
testcase_19 AC 41 ms
5,376 KB
testcase_20 AC 40 ms
5,376 KB
testcase_21 AC 39 ms
5,376 KB
testcase_22 AC 36 ms
5,376 KB
testcase_23 AC 39 ms
5,376 KB
testcase_24 AC 36 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline double time() {
    return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

template <int mod>
struct static_modint {
    using mint = static_modint;
    int x;

    static_modint() : x(0) {}
    static_modint(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

    mint& operator+=(const mint& rhs) {
        if ((x += rhs.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint& rhs) {
        if ((x += mod - rhs.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const mint& rhs) {
        x = (int) (1LL * x * rhs.x % mod);
        return *this;
    }
    mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }

    mint pow(long long n) const {
        mint _x = *this, r = 1;
        while (n) {
            if (n & 1) r *= _x;
            _x *= _x;
            n >>= 1;
        }
        return r;
    }
    mint inv() const { return pow(mod - 2); }

    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }
    friend mint operator+(const mint& lhs, const mint& rhs) {
        return mint(lhs) += rhs;
    }
    friend mint operator-(const mint& lhs, const mint& rhs) {
        return mint(lhs) -= rhs;
    }
    friend mint operator*(const mint& lhs, const mint& rhs) {
        return mint(lhs) *= rhs;
    }
    friend mint operator/(const mint& lhs, const mint& rhs) {
        return mint(lhs) /= rhs;
    }
    friend bool operator==(const mint& lhs, const mint& rhs) {
        return lhs.x == rhs.x;
    }
    friend bool operator!=(const mint& lhs, const mint& rhs) {
        return lhs.x != rhs.x;
    }

    friend ostream &operator<<(ostream &os, const mint &p) {
        return os << p.x;
    }
    friend istream &operator>>(istream &is, mint &a) {
        int64_t t; is >> t;
        a = static_modint<mod>(t);
        return (is);
    }
};

const unsigned int mod = 998244353;
using modint = static_modint<mod>;
modint mod_pow(ll n, ll x) { return modint(n).pow(x); }
modint mod_pow(modint n, ll x) { return n.pow(x); }

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int q; cin >> q;
    while (q--) {
        ll n; cin >> n;
        int x; cin >> x;

        map<pair<pair<ll,ll>,pair<int,int>>, modint> mp;

        auto slv = [&](auto slv, ll n1, ll n2, int k, int x) -> modint {
            if (x < 0) return 0;
            if (!n1 and !n2) {
                return (x == 0);
            }
            if (mp.find({{n1,n2}, {k,x}}) != mp.end()) {
                return mp[{{n1,n2}, {k,x}}];
            }
            modint res = 0;
            for (int a = 0; a < 10; ++a) {
                if (a > n1) break;
                for (int b = 0; b < 10; ++b) {
                    if (b > n2) break;
                    int sum = a+b+k;
                    if (sum >= 10) {
                        res += slv(slv, (n1-a)/10, (n2-b)/10, 1, x-1);
                    }
                    else {
                        res += slv(slv, (n1-a)/10, (n2-b)/10, 0, x);
                    }
                }
            }
            mp[{{n1,n2},{k,x}}] = res;
            return res;
        };

        cout << slv(slv, n, n, 0, x) << "\n";
    }
}
0