-
Notifications
You must be signed in to change notification settings - Fork 0
/
onezero.cpp
47 lines (44 loc) · 920 Bytes
/
onezero.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
#include <iostream>
#include <cstring>
#include <string>
#include <queue>
using namespace std;
int str_mod (string str, int num) {
int res = 0;
for (int i = 0; i<str.length(); i++)
res = (res*10 + str[i] - '0')%num;
return res;
}
int main() {
freopen("input.txt", "r", stdin);
int test_cases, num;
bool visited[20001];
cin >> test_cases;
while (test_cases--) {
memset(visited, false, sizeof(visited));
cin >> num;
queue <string> Q;
Q.push("1");
visited[1 % num] = true;
while (!Q.empty()) {
string ele = Q.front();
Q.pop();
if (str_mod(ele, num) == 0) {
cout << ele << endl;
break;
}
string temp = ele;
temp.push_back('0');
ele.push_back('1');
if (!visited[str_mod(temp, num)]) {
Q.push(temp);
visited[str_mod(temp, num)] = true;
}
if (!visited[str_mod(ele, num)]) {
Q.push(ele);
visited[str_mod(ele, num)] = true;
}
}
}
return 0;
}