-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathONEZERO.cpp
More file actions
56 lines (46 loc) · 738 Bytes
/
ONEZERO.cpp
File metadata and controls
56 lines (46 loc) · 738 Bytes
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
// Problem : ONEZERO from SPOJ
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int par[25001];
char dig[25001];
void solve(){
int n,i,v1,v2,end,cur;
string res = "";
cin>>n;
for(i=0;i<=n;i++)par[i] = -1,dig[i] = '0';
par[1] = 1;
queue<int> q;
q.push(1);
while(!q.empty()){
cur = q.front();
q.pop();
v1 = (cur*10)%n;
v2 = (v1+1)%n;
if(par[v1] == -1){
par[v1] = cur;
dig[v1] = '0';
q.push(v1);
}
if(par[v2] == -1){
par[v2] = cur;
dig[v2] = '1';
q.push(v2);
}
}
end = 0;
while(par[end]!=end){
res+=dig[end];
end = par[end];
}
res+='1';
reverse(res.begin(),res.end());
cout<<res<<"\n";
return ;
}
int main(){
int t;
cin>>t;
while(t--)solve();
return 0;
}