結果

問題 No.2455 Numbers Dictionary
ユーザー t98slidert98slider
提出日時 2023-09-01 23:39:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 167 ms / 2,000 ms
コード長 1,533 bytes
コンパイル時間 1,838 ms
コンパイル使用メモリ 167,096 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-01 23:39:26
合計ジャッジ時間 4,873 ms
ジャッジサーバーID
(参考情報)
judge14 / judge16
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 94 ms
4,376 KB
testcase_03 AC 103 ms
4,380 KB
testcase_04 AC 114 ms
4,376 KB
testcase_05 AC 96 ms
4,380 KB
testcase_06 AC 125 ms
4,376 KB
testcase_07 AC 167 ms
4,376 KB
testcase_08 AC 35 ms
4,376 KB
testcase_09 AC 123 ms
4,376 KB
testcase_10 AC 120 ms
4,380 KB
testcase_11 AC 125 ms
4,376 KB
testcase_12 AC 98 ms
4,380 KB
testcase_13 AC 126 ms
4,376 KB
testcase_14 AC 111 ms
4,376 KB
testcase_15 AC 125 ms
4,380 KB
testcase_16 AC 26 ms
4,380 KB
testcase_17 AC 123 ms
4,376 KB
testcase_18 AC 6 ms
4,376 KB
testcase_19 AC 7 ms
4,380 KB
testcase_20 AC 126 ms
4,380 KB
testcase_21 AC 126 ms
4,376 KB
testcase_22 AC 104 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

template<typename T>
T ADD(T a, T b){
    T res;
    return __builtin_add_overflow(a, b, &res)? std::numeric_limits<T>::max() : res;
}
template<typename T>
T MUL(T a, T b){
    T res;
    return __builtin_mul_overflow(a, b, &res)? std::numeric_limits<T>::max() : res;
}

void solve(){
    ll n, k;
    cin >> n >> k;
    //n以下かつkより辞書順で小さい
    string t = to_string(k);  
    auto f = [&](ll v, ll k){
        string s = to_string(v);
        ll dp[s.size() + 1][2][2] = {};
        for(int i = 1; i + '0' <= min(s[0], t[0]); i++) dp[1][i + '0' == s[0]][i + '0' == t[0]]++;
        for(int i = 1; i < s.size(); i++){
            for(int j = 0; j < 2; j++){
                int r = j ? s[i] - '0' : 9;
                for(int k = 0; k < 2; k++){
                    r = min(r, k ? (i >= t.size() ? -1 : t[i] - '0') : 9);
                    for(int l = 0; l <= r; l++){
                        dp[i + 1][j & (l == s[i] - '0')][k & (l == t[i] - '0')]
                         += dp[i][j][k];
                    }
                }
            }
        }
        return dp[s.size()][0][0] + dp[s.size()][1][0] + dp[s.size()][0][1] + dp[s.size()][1][1];
    };
    ll d = 9, ans = 0;
    while(d < n){
        ans += f(d, k);
        d = ADD(MUL(d, 10ll), 9ll);
    }
    ans += f(n, k);
    cout << ans << '\n';
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;
    cin >> T;
    while(T--){
        solve();
    }
}
0