传送门
题意
分析
我们从低位开始往高位寻找第一个不合法的数字,然后让这一位+1,然后check整个数是否合法,如果还是不合法的话,继续重复上一步操作
代码
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 10;
const ll mod = 1000000007;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a) {
char c = getchar(); T x = 0, f = 1; while (!isdigit(c)) {if (c == '-')f = -1; c = getchar();}
while (isdigit(c)) {x = (x << 1) + (x << 3) + c - '0'; c = getchar();} a = f * x;
}
int gcd(int a, int b) {return (b > 0) ? gcd(b, a % b) : a;}
int digcnt(int x){
int t = 0;
while(x){
t |= (1 << (x % 10));
x /= 10;
}
return __builtin_popcount(t); //统计二进制下1的个数
}
void solve(){
int n,k;
read(n),read(k);
while(digcnt(n) > k){
int l = 1,r = n;
while(digcnt(r) > k){
l *= 10,r /= 10;
}
l /= 10;
n = ((n / l) + 1) * l;
}
di(n);
}
int main() {
int T;
read(T);
while(T--){
solve();
}
return 0;
}