結果

問題 No.2302 Carry X Times
ユーザー AngrySadEightAngrySadEight
提出日時 2023-04-11 19:06:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 71 ms / 2,000 ms
コード長 1,856 bytes
コンパイル時間 831 ms
コンパイル使用メモリ 83,900 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-16 16:05:25
合計ジャッジ時間 2,850 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
5,248 KB
testcase_01 AC 20 ms
5,376 KB
testcase_02 AC 22 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 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 70 ms
5,376 KB
testcase_11 AC 69 ms
5,376 KB
testcase_12 AC 70 ms
5,376 KB
testcase_13 AC 67 ms
5,376 KB
testcase_14 AC 66 ms
5,376 KB
testcase_15 AC 68 ms
5,376 KB
testcase_16 AC 69 ms
5,376 KB
testcase_17 AC 71 ms
5,376 KB
testcase_18 AC 69 ms
5,376 KB
testcase_19 AC 68 ms
5,376 KB
testcase_20 AC 69 ms
5,376 KB
testcase_21 AC 68 ms
5,376 KB
testcase_22 AC 67 ms
5,376 KB
testcase_23 AC 69 ms
5,376 KB
testcase_24 AC 70 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <map>
#include <cassert>
using namespace std;
typedef long long ll;
ll mod = 998244353;

ll calc(ll a_max, ll b_max, ll x, ll hascarry, map<pair<pair<ll,ll>, pair<ll,ll>>, ll> &mp){
    //cout << a_max << " " << b_max << " " << x << " " << hascarry << endl;
    ll ans = 0;
    if (a_max == 0 && b_max == 0){
        if (x == 0){
            return 1LL;
        }
        else{
            return 0LL;
        }
    }
    for (ll i = 0; i < 10; i++){
        for (ll j = 0; j < 10; j++){
            if (a_max - i < 0) continue;
            if (b_max - j < 0) continue;
            pair<ll,ll> p1 = pair<ll,ll>((a_max - i) / 10, (b_max - j) / 10);
            ll next_carry = (i + j + hascarry) / 10;
            pair<ll,ll> p2 = pair<ll,ll>(x - next_carry, next_carry);
            //cout << (a_max - i) / 10 << " " << (b_max - j) / 10 << " " << x - next_carry << " " << next_carry << endl;
            if (mp.find(pair<pair<ll,ll>,pair<ll,ll>>(p1, p2)) != mp.end()){
                ans = (ans + mp[pair<pair<ll,ll>, pair<ll,ll>>(p1, p2)]) % mod;
            }
            else{
                mp[pair<pair<ll,ll>, pair<ll,ll>>(p1, p2)] += calc((a_max - i) / 10, (b_max - j) / 10, x - next_carry, next_carry, mp);
                ans = (ans + mp[pair<pair<ll,ll>, pair<ll,ll>>(p1, p2)]) % mod;
            }
        }
    }
    return ans;
}

int main(){
    ll T;
    cin >> T;
    assert((1 <= T && T <= 10));
    while(T--){
        ll N, X;
        cin >> N >> X;
        assert((1 <= N && N <= 1000000000000000000));
        assert((0 <= X && X <= 18));
        // 桁 DP で,「その桁まで見て,ここまで繰り上がり回数が X 回で,より大きいことが確定しているか」で考える
        map<pair<pair<ll,ll>, pair<ll,ll>>, ll> mp;
        cout << calc(N, N, X, 0, mp) << endl;
    }
}
0