-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathspecialMultiple.cpp
65 lines (60 loc) · 1.28 KB
/
specialMultiple.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
// https://www.hackerrank.com/challenges/special-multiple
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 5555;
bool was[N];
int x[N], pr[N], pd[N];
int main()
{
int tt;
cin >> tt;
while (tt--)
{
int n;
cin >> n;
for (int i = 0; i < n; i++)
was[i] = false;
int b = 1, e = 1;
x[1] = 9 % n;
was[x[1]] = true;
pr[x[1]] = -1;
pd[x[1]] = -1;
while (b <= e)
{
int nx = (x[b] * 10 + 0) % n;
if (!was[nx])
{
e++;
x[e] = nx;
was[nx] = true;
pr[nx] = x[b];
pd[nx] = 0;
}
nx = (x[b] * 10 + 9) % n;
if (!was[nx])
{
e++;
x[e] = nx;
was[nx] = true;
pr[nx] = x[b];
pd[nx] = 9;
}
b++;
}
int p = 0;
string res = "";
while (pr[p] != -1)
{
res += (char)(pd[p] + 48);
p = pr[p];
}
res += "9";
reverse(res.begin(), res.end());
cout << res << endl;
}
return 0;
}