Skip to content

Instantly share code, notes, and snippets.

@dipta10
Last active August 12, 2021 17:26
Show Gist options
  • Select an option

  • Save dipta10/8d206696f3d8dadd8570898ef5024e71 to your computer and use it in GitHub Desktop.

Select an option

Save dipta10/8d206696f3d8dadd8570898ef5024e71 to your computer and use it in GitHub Desktop.
/*
* SPOJ: ONEZERO - Ones and zeros
* Find the smallest binary digit multiple of given number
* Link:https://www.spoj.com/problems/ONEZERO/en/
* Editorial: https://www.geeksforgeeks.org/find-the-smallest-binary-digit-multiple-of-given-number/
* Solution:
*
*/
#include <stdio.h>
#include <bits/stdc++.h>
#define fin freopen("input", "r", stdin)
using namespace std;
int mod (string t, int n) {
int r = 0;
for (int i = 0; i < (int)t.length(); ++i) {
r = r * 10 + (t[i] - '0');
r %= n;
}
return r;
}
string sol (int n) {
queue <string> q;
set<int> visit;
string t = "1";
q.push(t);
while (!q.empty()) {
t = q.front(); q.pop();
int rem = mod(t, n);
if (rem == 0) return t;
else if (visit.find(rem) == visit.end()) {
visit.insert(rem);
q.push(t + "0");
q.push(t + "1");
}
}
}
int main()
{
int t, x;
cin >> t;
while (t--) {
cin >> x;
cout << sol(x) << endl;
}
return 0;
}
@saurabh-38
Copy link

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment