Skip to content

Commit

Permalink
Problem ONEZERO from SPOJ
Browse files Browse the repository at this point in the history
  • Loading branch information
mani1996 committed Oct 19, 2016
1 parent 2951b96 commit ba359be
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions ONEZERO.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}

0 comments on commit ba359be

Please sign in to comment.