結果

問題 No.2455 Numbers Dictionary
ユーザー milanis48663220milanis48663220
提出日時 2023-09-01 23:14:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 418 ms / 2,000 ms
コード長 3,028 bytes
コンパイル時間 1,757 ms
コンパイル使用メモリ 129,132 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-09-01 23:14:54
合計ジャッジ時間 10,074 ms
ジャッジサーバーID
(参考情報)
judge14 / judge16
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 393 ms
4,384 KB
testcase_03 AC 400 ms
4,380 KB
testcase_04 AC 405 ms
4,384 KB
testcase_05 AC 391 ms
4,380 KB
testcase_06 AC 417 ms
4,384 KB
testcase_07 AC 410 ms
4,384 KB
testcase_08 AC 130 ms
4,380 KB
testcase_09 AC 409 ms
4,380 KB
testcase_10 AC 409 ms
4,380 KB
testcase_11 AC 410 ms
4,384 KB
testcase_12 AC 405 ms
4,384 KB
testcase_13 AC 418 ms
4,380 KB
testcase_14 AC 401 ms
4,380 KB
testcase_15 AC 406 ms
4,384 KB
testcase_16 AC 120 ms
4,384 KB
testcase_17 AC 403 ms
4,380 KB
testcase_18 AC 34 ms
4,380 KB
testcase_19 AC 37 ms
4,380 KB
testcase_20 AC 416 ms
4,384 KB
testcase_21 AC 415 ms
4,388 KB
testcase_22 AC 393 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}

ll sub_solve(vector<int> mx, vector<int> target){
    int len = mx.size();
    auto dp = vec3d(len, 2, 2, 0ll);
    for(int x = 1; x < mx[0]; x++){
        if(x < target[0]){
            dp[0][0][0]++;
        }else if(x == target[0]){
            dp[0][0][1]++;
        }
    }
    if(mx[0] < target[0]){
        dp[0][1][0]++;
    }else if(mx[0] == target[0]){
        dp[0][1][1]++;
    }
    // cout << "====" << endl;
    // debug_value(mx[0])
    // print_vector(dp[0][0]);
    // print_vector(dp[0][1]);
    int target_len = target.size();
    for(int i = 1; i < len; i++){
        for(int j = 0; j <= 1; j++){
            for(int k = 0; k <= 1; k++){
                int t = target.size() > i ? target[i] : -1;
                for(int x = 0; x <= 9; x++){
                    // 大きくなってしまう
                    if(k == 1 && x > t) continue;
                    if(j == 1 && x > mx[i]) continue;
                    int jj = (j == 0 || x < mx[i]) ? 0 : 1;
                    int kk = (k == 0 || x < t) ? 0 : 1;
                    dp[i][jj][kk] += dp[i-1][j][k];
                }
            }
        }
    }
    ll ans = dp[len-1][0][0]+dp[len-1][1][0];
    if(mx.size() < target.size()){
        ans += dp[len-1][0][1]+dp[len-1][1][1];
    }
    // debug_value(ans)
    return ans;
}

void solve(){
    ll n, k; cin >> n >> k;
    string ns = to_string(n);
    string ks = to_string(k);
    vector<int> nv, kv;
    for(char c: ns) nv.push_back(c-'0');
    for(char c: ks) kv.push_back(c-'0');
    ll ans = 0;
    for(int len = 1; len < ns.size(); len++){
        ans += sub_solve(vector(len, 9), kv);
    }
    ans += sub_solve(nv, kv);
    ans++;
    cout << ans << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int t; cin >> t;
    while(t--) solve();
}
0