-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCF Round 692 Div 2 - Fair Numbers (B).cpp
109 lines (99 loc) · 2.14 KB
/
CF Round 692 Div 2 - Fair Numbers (B).cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Problem Link: https://codeforces.com/contest/1465/problem/B
#include<iostream>
#include<limits.h>
#include<math.h>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<unordered_set>
#include<unordered_map>
#include<algorithm>
using namespace std;
#define ll long long int
#define ull unsigned long long int
#define modulo 1000000007
#define mp make_pair
#define pb push_back
bool check(multiset<int>& st, ull n)
{
for(auto it = st.begin(); it != st.end(); it++)
{
if(n%(*it) != 0) {return false;}
}
return true;
}
void update(multiset<int>& st, ull& n)
{
if(n%10 != 9)
{
int count = st.count(n%10);
st.erase(n%10);
if(count > 1)
{
--count;
while(count > 0) {st.insert(n%10); count--;}
}
n++;
st.insert(n%10);
}
else
{
ull temp = n;
n++;
int allcount = st.count(9);
int count = 0;
while(temp > 0 && temp%10 == 9)
{
count++;
temp /= 10;
}
st.erase(9);
if(temp == 0) {st.insert(1);}
else
{
allcount -= count;
while(allcount > 0)
{
st.insert(9);
allcount--;
}
st.insert((temp%10)+1);
allcount = st.count(temp%10)-1;
st.erase(temp%10);
while(allcount > 0)
{
st.insert(temp%10);
allcount--;
}
}
}
}
int main()
{
ll tests;
cin >> tests;
while(tests--)
{
ull n;
cin >> n;
multiset<int> st {};
ull temp = n;
while(temp > 0)
{
if(temp%10 != 0) {st.insert(temp%10);}
temp /= 10;
}
bool flag = false;
while(!flag)
{
if(check(st,n)) {flag = true; break;}
update(st,n);
}
cout << n;
cout << "\n";
}
return 0;
}