# |
Author |
Problem |
Lang |
Verdict |
Time |
Memory |
Sent |
Judged |
|
126425850 |
Practice:
Tsovak |
1560F2
- 30
|
C++17 (GCC 9-64)
|
Accepted
|
77 ms
|
4216 KB
|
2021-08-19 12:10:15 |
2021-08-19 12:10:15 |
|
// -------------------- Includes -------------------- //
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <stdio.h>
#include <cstdlib>
#include <stdlib.h>
#include <array>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <list>
#include <iterator>
#include <numeric>
#include <complex>
#include <tuple>
#include <utility>
#include <cassert>
#include <assert.h>
#include <climits>
#include <limits.h>
#include <ctime>
#include <time.h>
#include <random>
#include <chrono>
#include <fstream>
using namespace std;
// -------------------- Typedefs -------------------- //
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
// -------------------- Defines -------------------- //
#define pr pair
#define mpr make_pair
#define ff first
#define ss second
#define mset multiset
#define mmap multimap
#define uset unordered_set
#define umap unordered_map
#define umset unordered_multiset
#define ummap unordered_multimap
#define pqueue priority_queue
#define sz(x) (int((x).size()))
#define len(x) (int((x).length()))
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define clr(x) (x).clear()
#define pf push_front
#define pb push_back
#define popf pop_front
#define popb pop_back
#define ft front
#define bk back
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
// -------------------- Constants -------------------- //
const int MAX = int(1e9 + 5);
const ll MAXL = ll(1e18 + 5);
const ll MOD = ll(1e9 + 7);
const ll MOD2 = ll(998244353);
// -------------------- Functions -------------------- //
void fastio()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
return;
}
void precision(int x)
{
cout.setf(ios::fixed | ios::showpoint);
cout.precision(x);
return;
}
ll gcd(ll a, ll b)
{
if (a > b) {
swap(a, b);
}
if (a == 0) {
return b;
}
if (a == b) {
return a;
}
return gcd(b % a, a);
}
ll lcm(ll a, ll b)
{
return a / gcd(a, b) * b;
}
bool is_prime(ll a)
{
int i;
for (i = 2; i * i <= a; i++) {
if (a % ll(i) == 0) {
return false;
}
}
return true;
}
bool is_square(ll a)
{
ll b = ll(sqrt(a));
return (b * b == a);
}
bool is_cube(ll a)
{
ll b = ll(cbrt(a));
return (b * b * b == a);
}
int digit_sum(ll a)
{
int sum = 0;
while (a) {
sum += int(a % 10);
a /= 10;
}
return sum;
}
ll binpow(ll a, int b)
{
ll ans = 1;
while (b) {
if ((b & 1) == 1) {
ans *= a;
}
b >>= 1;
a *= a;
}
return ans;
}
ll binpow_by_mod(ll a, ll b, ll mod)
{
ll ans = 1;
while (b) {
if ((b & 1) == 1) {
ans *= a;
ans %= mod;
}
b >>= 1;
a *= a;
a %= mod;
}
return ans;
}
ll factorial(int a)
{
int i;
ll ans = 1;
for (i = 2; i <= a; i++) {
ans *= ll(i);
}
return ans;
}
ll factorial_by_mod(int a, ll mod)
{
int i;
ll ans = 1;
for (i = 2; i <= a; i++) {
ans *= ll(i);
ans %= mod;
}
return ans;
}
// -------------------- Solution -------------------- //
/*const int N = 200005;
int a[N];
int n;*/
bool check(mset<int> s, int k)
{
set<int> st;
for (auto it = s.begin(); it != s.end(); it++) {
st.insert(*it);
}
return (sz(st) < k);
}
void solve()
{
int i, j;
int n, k;
cin >> n >> k;
//change n into a vector so that the indexes appear
vector<int> v;
while (n) {
v.pb(n % 10); //pb is push_back
n /= 10;
}
reverse(all(v)); //all(v) is v.begin(), v.end()
set<int> p;
for (int l = 0; l < sz(v); l++) { //sz is size
p.insert(v[l]);
if (sz(p) > k) { //if p contains more than k distinct digits
i = l;
break;
}
}
if (sz(p) <= k) { //if p = n
//print n
for (int l = 0; l < sz(v); l++) {
cout << v[l];
}
cout << endl;
return;
}
p.erase(p.find(v[i]));
auto it = p.ub(v[i]); //ub is upper_bound
if (it != p.end()) { //if x exists
int x = *it;
v[i] = x;
for (int l = i + 1; l < sz(v); l++) {
v[l] = *p.begin(); //change the all remaining digits of n into the smallest number in p
}
//print answer
for (int l = 0; l < sz(v); l++) {
cout << v[l];
}
cout << endl;
return;
}
mset<int> s; //mset is multiset
j = i - 1;
for (int l = 0; l < j; l++) {
s.insert(v[l]);
}
for (j; j >= 0; j--) {
int a = v[j];
if (s.find(a) == s.end()) { //s doesn't contain a
v[j] = a + 1;
s.insert(a + 1);
if (check(s, k)) { //if s contain less than k distinct digits
for (int l = j + 1; l < sz(v); l++) {
v[l] = 0;
}
}
else {
for (int l = j + 1; l < sz(v); l++) {
v[l] = *s.begin();
}
}
//print answer
for (int l = 0; l < sz(v); l++) {
cout << v[l];
}
cout << endl;
return;
}
auto it = s.ub(a);
if (it != s.end()) { //if b exists
int b = *it;
v[j] = b;
s.insert(b);
if (check(s, k)) { //if s contain less than k distinct digits
for (int l = j + 1; l < sz(v); l++) {
v[l] = 0;
}
}
else {
for (int l = j + 1; l < sz(v); l++) {
v[l] = *s.begin();
}
}
//print answer
for (int l = 0; l < sz(v); l++) {
cout << v[l];
}
cout << endl;
return;
}
s.erase(s.find(v[j - 1])); //delete digit before a
}
return;
}
void precalc()
{
return;
}
int main()
{
fastio();
precalc();
int tests = 1, tc;
cin >> tests;
for (tc = 1; tc <= tests; tc++) {
solve();
}
//cout << db(clock()) / CLOCKS_PER_SEC << endl;
return 0;
}
/*
# # # # # # # # # # # # # # # # #
# # # # # # # # # #
# # # # # # # # # # # # #
# # # # # # # # # # # #
# # # # # # # # # # # # # #
*/
Click to see test details