Last active
August 12, 2021 17:26
-
-
Save dipta10/8d206696f3d8dadd8570898ef5024e71 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * 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; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks