Advertisement
pb_jiang

cf379d wa

Jan 2nd, 2023
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.38 KB | None | 0 0
  1. #include <assert.h>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. #define dbg(...) logger(#__VA_ARGS__, __VA_ARGS__)
  5. template <typename... Args> void logger(string vars, Args &&... values)
  6. {
  7.     cerr << vars << " = ";
  8.     string delim = "";
  9.     (..., (cerr << delim << values, delim = ", "));
  10.     cerr << endl;
  11. }
  12.  
  13. template <class T> inline auto vv(int m) { return vector<vector<T>>(m, vector<T>(m)); }
  14. template <class T> inline auto vv(int m, int n) { return vector<vector<T>>(m, vector<T>(n)); }
  15. template <class T, T init> inline auto vv(int m) { return vector<vector<T>>(m, vector<T>(m, init)); }
  16. template <class T, T init> inline auto vv(int m, int n) { return vector<vector<T>>(m, vector<T>(n, init)); }
  17.  
  18. template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
  19.  
  20. using ll = long long;
  21. using pii = pair<int, int>;
  22. using vl = vector<ll>;
  23. using vi = vector<int>;
  24.  
  25. ll extgcd(ll a, ll b, ll &m, ll &n)
  26. {
  27.     if (a == 0) {
  28.         m = 0, n = 1;
  29.         return b;
  30.     }
  31.     ll ret = extgcd(b % a, a, m, n);
  32.     ll tmp = m;
  33.     m = n - (b / a) * m;
  34.     n = tmp;
  35.     return ret;
  36. }
  37.  
  38. string multiple(ll x)
  39. {
  40.     dbg(x);
  41.     string ret = "";
  42.     string cur = "AC";
  43.     while (x) {
  44.         if (x & 1)
  45.             ret = ret + cur;
  46.         cur = cur + cur;
  47.         x /= 2;
  48.     }
  49.     return ret;
  50. }
  51.  
  52. int main(int argc, char **argv)
  53. {
  54.     int k, x, n, m;
  55.     cin >> k >> x >> n >> m;
  56.     ll a1 = 1, a2 = 1, a12 = 1, a21 = 0;
  57.     ll b1 = 1, b2 = 2, b12 = 1, b21 = 1;
  58.     for (int i = 3; i <= k; ++i) {
  59.         ll c1 = a1 + b1, c2 = a2 + b2;
  60.         ll c12 = a12 + b12, c21 = a21 + b21 + (i + 1 % 2);
  61.         a1 = b1, a2 = b2, a12 = b12, a21 = b21;
  62.         b1 = c1, b2 = c2, b12 = c12, b21 = c21;
  63.     }
  64.  
  65.     ll f1, f2;
  66.     ll g12 = extgcd(b1, b2, f1, f2);
  67.     assert(b1 * f1 + b2 * f2 == g12);
  68.     assert(g12 > 0);
  69.     if (x % g12 == 0) {
  70.         ll k = x / g12;
  71.         assert(k > 0);
  72.         ll lb1 = -f1 / b2, lb2 = (f2 - m / 2 / k) / b1;
  73.         ll ub1 = (n / 2 / k - f1) / b2, ub2 = f2 / b1;
  74.         if (max(lb1, lb2) <= min(ub1, ub2)) {
  75.             ll k1 = max(lb1, lb2);
  76.             // ll k1 = min(ub1, ub2);
  77.             string s1 = multiple((f1 + k1 * b2) * k);
  78.             string s2 = multiple((f2 - k1 * b1) * k);
  79.             s1 += string(n - s1.size(), 'D');
  80.             s2 += string(m - s2.size(), 'D');
  81.             cout << s1 << '\n' << s2 << '\n';
  82.             return 0;
  83.         }
  84.     }
  85.     if ((x - b12) % g12 == 0) {
  86.         ll k = (x - b12) / g12;
  87.         n -= 1;
  88.         m -= 1;
  89.         ll lb1 = -f1 / b2, lb2 = (f2 - m / 2 / k) / b1;
  90.         ll ub1 = (n / 2 / k - f1) / b2, ub2 = f2 / b1;
  91.         if (max(lb1, lb2) <= min(ub1, ub2)) {
  92.             ll k1 = max(lb1, lb2);
  93.             string s1 = multiple((f1 + k1 * b2) * k);
  94.             string s2 = multiple((f2 - k1 * b1) * k);
  95.             s1 += string(n - s1.size(), 'D');
  96.             s2 += string(m - s2.size(), 'D');
  97.             s1 += 'A';
  98.             s2 = "C" + s2;
  99.             cout << s1 << '\n' << s2 << '\n';
  100.             return 0;
  101.         }
  102.     }
  103.     if ((x - b21) % g12 == 0) {
  104.         ll k = (x - b12) / g12;
  105.         n -= 1;
  106.         m -= 1;
  107.         ll lb1 = -f1 / b2, lb2 = (f2 - m / 2 / k) / b1;
  108.         ll ub1 = (n / 2 / k - f1) / b2, ub2 = f2 / b1;
  109.         if (max(lb1, lb2) <= min(ub1, ub2)) {
  110.             ll k1 = max(lb1, lb2);
  111.             string s1 = multiple((f1 + k1 * b2) * k);
  112.             string s2 = multiple((f2 - k1 * b1) * k);
  113.             s1 += string(n - s1.size(), 'D');
  114.             s2 += string(m - s2.size(), 'D');
  115.             s2 += 'A';
  116.             s1 = "C" + s2;
  117.             cout << s1 << '\n' << s2 << '\n';
  118.             return 0;
  119.         }
  120.     }
  121.     if ((x - b12 - b21) % g12 == 0) {
  122.         ll k = (x - b12) / g12;
  123.         n -= 2;
  124.         m -= 2;
  125.         ll lb1 = -f1 / b2, lb2 = (f2 - m / 2 / k) / b1;
  126.         ll ub1 = (n / 2 / k - f1) / b2, ub2 = f2 / b1;
  127.         if (max(lb1, lb2) <= min(ub1, ub2)) {
  128.             ll k1 = max(lb1, lb2);
  129.             string s1 = multiple((f1 + k1 * b2) * k);
  130.             string s2 = multiple((f2 - k1 * b1) * k);
  131.             s1 += string(n - s1.size(), 'D');
  132.             s2 += string(m - s2.size(), 'D');
  133.             s1 = "C" + s1 + "A";
  134.             s2 = "C" + s2 + "A";
  135.             cout << s1 << '\n' << s2 << '\n';
  136.             return 0;
  137.         }
  138.     }
  139.     cout << "Happy new year!" << endl;
  140.     return 0;
  141. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement