2024 ICPC 陕西省赛 G. Disappearing Number

G. Disappearing Number

time limit per test: 1 second

memory limit per test: 256 megabytes

uuku is learning digital dynamic programming.

But one day, he found that the x in the nine digits of 1,2,…,9 had disappeared, which means that all numbers containing the digit x had disappeared.

This made him very panicked because it had a significant impact on the number of statistics.

Now, he hopes you can help him find out where the number n ranks among the natural numbers that have not disappeared in this situation.

Input

There are multiple test cases. The first line of the input contains an integer T (1≤T≤10^{5}), indicating the number of test cases.

For each test case:

The first line contains two positive integers n and x (0≤n≤10^{18}, 1≤x≤9).

It's guaranteed that n doesn't include x.

Output

For each test case, output a number indicating the ranking of n.

Example

input

5

9 4

99 7

12345678 9

9475632111234123 8

998244353114514 7

output

9

81

6053445

1758041005111510

205404686678741

Note

For the first example, the natural sequence becomes 0,1,2,3,5,6,7,8,9, where the number 9 is the 9 th.

【思路分析】

分治+类二分+数学。只需要计数0到n中含x的数即可,为了计算方便,我们将n拆分为多个首位为1其余位为0的整数。注意到以下规律:我们设S_{n} = 10^{n}T_{n}为从0到S_{n}中含x的数的计数。有T_{n} = 10T_{n-1}+S_{n}-T_{n-1}。由于我们对n进行了拆分,T_{n}首位不一定为1,需要特判,T_{n}调整后得K_{n}。答案即为\sum_{0}^{n} K_{n}

#include <iostream>
#include <vector>
#include <unordered_map>
#include <map>
#include <cmath>
#include <algorithm>
#include <climits>
#include <stack>
#include <cstring>
#include <iomanip>

#define i64 long long

using namespace std;

i64 n, x;

i64 cal(i64 num) {
    if (num==0) return 0;
    i64 res = 0, pows = 10, ss = 0;
    string nums = to_string(num);
    i64 tmp = num / (nums[0] - '0');
    while (pows != tmp * 10) {
        if (res == 0) {
            res = 1;
            ss = pows - res;
            pows *= 10;
            continue;
        }
        res = res * 10 + ss;
        ss = pows - res;
        pows *= 10;
    }
    if (nums[0] - '0' > x) res = res * (nums[0] - '0' - 1) + pows / 10;
    else res *= nums[0] - '0';
    return res;
}

void solve() {
    i64 res = 0;
    cin >> n >> x;
    if (n == 0) {
        cout << 1 << endl;
        return;
    }
    i64 tmp = pow(10, to_string(n).size() - 1), cnt = 0;
    string ns = to_string(n);
    while (tmp != 0) {
        res += cal(tmp * (ns[cnt++] - '0'));
        tmp /= 10;
    }
    cout << n - res + 1 << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值