Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <cmath>
- #include <memory.h>
- #include <ctype.h>
- #include <vector>
- #include <stack>
- #include <queue>
- #include <map>
- #include <algorithm>
- using namespace std;
- int n;
- int sum(string s)
- {
- int r = 0;
- for (int i = 0; i < n; i++)
- if (s[i] == '1')
- r += i+1;
- return r;
- }
- int main()
- {
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "rt", stdin);
- freopen("output.txt", "wt", stdout);
- #endif
- cin >> n;
- string s, t;
- int k;
- while (getline(cin, t))
- {
- s = "";
- for (int i = 0; i < t.length(); i++)
- if (t[i] == '0' || t[i] == '1')
- s += t[i];
- k = s.length();
- if (k == 0)
- continue;
- if (k == n)
- {
- if (sum(s) % (n+1) == 0)
- cout << s << "\n";
- else
- for (int i = n-1; i >= 0; i--)
- if (s[i] == '1')
- {
- s[i] = '0';
- if (sum(s) % (n+1) == 0)
- {
- cout << s << "\n";
- break;
- }
- s[i] = '1';
- }
- } else
- if (k > n)
- {
- for (int i = 0; i < k; i++)
- {
- t = s.substr(0,i) + s.substr(i+1);
- if (sum(t) % (n+1) == 0)
- {
- cout << t << "\n";
- break;
- }
- }
- } else
- {
- for (int i = 0; i <= k; i++)
- for (char c = '0'; c <= '1'; c++)
- {
- t = s.substr(0,i) + c + s.substr(i);
- if (sum(t) % (n+1) == 0)
- {
- cout << t << "\n";
- break;
- }
- }
- }
- if (cin.eof())
- break;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement