#include <bits/stdc++.h> #define ll long long using namespace std; ll extended_gcd(ll a, ll b, ll& x, ll& y) { if (a == 0) { x = 0; y = 1; return b; } ll x1, y1; ll d = extended_gcd(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return d; } void get(ll a, ll b, ll c, ll &x0, ll &y0) { extended_gcd(a, b, x0, y0); x0 *= c; y0 *= c; } int main() { ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); ll n; cin >> n; string s; cin >> s; ll cnt = 0; for (char c : s) cnt += (c == '1'); for (ll c = 0; c * 5 <= cnt; c++) { ll a = 0, b = 0; get(3, 4, cnt - c * 5, a, b); if (a < 0) { ll k = -(a - 3) / 4; a += k * 4; b -= k * 3; } int x = n - a - b - c; if (x < 0) continue; a += 4 * x; b -= 3 * x; if (a >= 0 && b >= 0 && c >= 0 && a + b + c == n) { // answer found if (a & 1) { cout << 'a'; a--; } cout << string(a / 2, 'w'); cout << string(b, 'c'); cout << string(c, 'g'); return 0; } } cout << "NIE"; }
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 57 58 59 | #include <bits/stdc++.h> #define ll long long using namespace std; ll extended_gcd(ll a, ll b, ll& x, ll& y) { if (a == 0) { x = 0; y = 1; return b; } ll x1, y1; ll d = extended_gcd(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return d; } void get(ll a, ll b, ll c, ll &x0, ll &y0) { extended_gcd(a, b, x0, y0); x0 *= c; y0 *= c; } int main() { ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); ll n; cin >> n; string s; cin >> s; ll cnt = 0; for (char c : s) cnt += (c == '1'); for (ll c = 0; c * 5 <= cnt; c++) { ll a = 0, b = 0; get(3, 4, cnt - c * 5, a, b); if (a < 0) { ll k = -(a - 3) / 4; a += k * 4; b -= k * 3; } int x = n - a - b - c; if (x < 0) continue; a += 4 * x; b -= 3 * x; if (a >= 0 && b >= 0 && c >= 0 && a + b + c == n) { // answer found if (a & 1) { cout << 'a'; a--; } cout << string(a / 2, 'w'); cout << string(b, 'c'); cout << string(c, 'g'); return 0; } } cout << "NIE"; } |