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≤), indicating the number of test cases.
For each test case:
The first line contains two positive integers n and x (0≤n≤, 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的整数。注意到以下规律:我们设,
为从0到
中含x的数的计数。有
。由于我们对n进行了拆分,
首位不一定为1,需要特判,
调整后得
。答案即为
。
#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;
}