forked from sureshmangs/Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1465B - Fair Numbers.cpp
65 lines (50 loc) · 1.33 KB
/
1465B - Fair Numbers.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
/*
We call a positive integer number fair if it is divisible by each of its nonzero digits. For example, 102 is fair (because it is divisible by 1 and 2), but 282 is not, because it isn't divisible by 8. Given a positive integer n. Find the minimum integer x, such that n≤x and x is fair.
Input
The first line contains number of test cases t (1≤t≤103). Each of the next t lines contains an integer n (1≤n≤1018).
Output
For each of t test cases print a single integer — the least fair number, which is not less than n.
Example
inputCopy
4
1
282
1234567890
1000000000000000000
outputCopy
1
288
1234568040
1000000000000000000
Note
Explanations for some test cases:
In the first test case number 1 is fair itself.
In the second test case number 288 is fair (it's divisible by both 2 and 8). None of the numbers from [282,287] is fair, because, for example, none of them is divisible by 8.
*/
#include<bits/stdc++.h>
using namespace std;
bool isFair(long long x){
long long tmp=x;
while(x){
if(x%10!=0 && tmp%(x%10)!=0)return false;
x/=10;
}
return true;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--){
long long n;
cin>>n;
long long x=n;
while(1){
if(isFair(x)) break;
x++;
}
cout<<x<<endl;
}
return 0;
}