Advertisement
Korotkodul

TG J TL stress

Oct 6th, 2022
771
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <queue>
  5. #include <algorithm>
  6. #include <string>
  7. #include <stack>
  8. #include <set>
  9. #include <map>
  10. #define pii pair <int, int>
  11. #define pb(x) push_back(x)
  12. #include <ctime>
  13. using namespace std;
  14. using ll = long long;
  15. using ld = long double;
  16. using db = double;
  17. void cv(vector <int> &v) {
  18.     for (auto x : v) cout << x << ' ';
  19.     cout << "\n";
  20. }
  21.  
  22. void cvl(vector <ll> &v) {
  23.     for (auto x : v) cout << x << ' ';
  24.     cout << "\n";
  25. }
  26.  
  27.  
  28. void cvv(vector <vector <int> > &v) {
  29.     for (auto x : v) cv(x);
  30.     cout << "\n";
  31. }
  32.  
  33. void cvb(vector <bool> v) {
  34.     for (bool x : v) cout << x << ' ';
  35.     cout << "\n";
  36. }
  37.  
  38. void cvs(vector <string>  v) {
  39.     for (auto a : v) {
  40.         cout << a << "\n";
  41.     }
  42. }
  43.  
  44. void cvp(vector <pii> a) {
  45.     for (auto p : a) {
  46.         cout << p.first << ' ' << p.second << "\n";
  47.     }
  48.     cout << "\n";
  49. }
  50.  
  51. ll a, b, c, d;
  52. bool sh = 0;
  53.  
  54. ll S(ll a1, ll d, ll n) {
  55.     return (2 * a1 + d * (n - 1)) * n / 2;
  56. }
  57.  
  58. ll f(ll t) {
  59.     ll res = 1 + (t - 1)  / d;
  60.     if (sh) {
  61.         //cout << "f(" << t << ") = " << res << "\n";
  62.     }
  63.     return res;
  64. }
  65.  
  66. ll hp(ll t) {
  67.     ll res;
  68.     if (t < 1 + d) {
  69.         res = -a + (t - 1) * b;
  70.         return res;
  71.     }
  72.     ll ft = f(t);
  73.     ll most = ft * (b * c - a);
  74.     ll del;
  75.     ll l, r, m, lid, lend; //look for lid
  76.     ll id = (t - 1) / d + 1;
  77.     l = 0;
  78.     r = id;
  79.     while (l + 1 < r) {
  80.         m = (l + r) / 2;
  81.         if (1 + (m - 1) * d + c > t) {
  82.             r = m;
  83.         } else {
  84.             l = m;
  85.         }
  86.     }
  87.     lid = r;
  88.     /*for (int i = 0; i < 10; ++i) {
  89.         if (1 + i * d + c > t) {
  90.             lid = i;
  91.             break;
  92.         }
  93.     }*/
  94.  
  95.     //lend = 1 + lid * d + c;
  96.     lend = 1 + (lid - 1) * d + c;
  97.  
  98.     ll nmb = id - (lid - 1);
  99.     ll start = (lend - t) * nmb * b;
  100.     ll a1 = d * b * (nmb - 1);
  101.     ll step = -d * b;
  102.     del = start + S(a1, step, nmb - 1);
  103.  
  104.     /*if (sh) {
  105.         cout << "ft = " << ft << "\n";
  106.         cout << "ft * (b * c - a) = " << ft * (b * c - a) << "\n";
  107.         cout << "lid = " << lid << "\n";
  108.         cout << "lend = " << lend << "\n\n";
  109.  
  110.         cout << "nmb = " << nmb << "\n";
  111.         cout << "step = " << step << "\n";
  112.         cout << "a1 = " << a1 << "\n";
  113.         cout << "del = " << del << "\n";
  114.         cout << "id = " << id << "\n";
  115.         //cout << "\n";
  116.     }*/
  117.  
  118.     res = ft * (b * c - a) - del;
  119.     return res;
  120. }
  121.  
  122. /*
  123. 228 21 11 3
  124.  
  125.  
  126. 3 1 7 3
  127. */
  128.  
  129. bool rand_mode = 1;
  130.  
  131. void slv() {
  132.     srand(time(0));
  133.     if (!rand_mode) {
  134.         cin >> a >> b >> c >> d;
  135.     } else {
  136.         ll small = 10, big = 1e6;
  137.         a = rand() % (ll)1e6 + big;
  138.         b = rand() % (ll)1e6 + big;
  139.         c = rand() % (ll)1e6 + big;
  140.         d = rand() % (ll)1e6 + big;
  141.         cout << "rand  b c d = " << a << ' ' << b << ' ' << c << ' ' << d << "\n";
  142.     }
  143.     if (a > b * c) {
  144.         cout << -1 << "\n";
  145.         return;
  146.     }
  147.     if (sh) {
  148.         for (ll t = 1; t < 50; ++t) {
  149.             cout << "t = " << 1 + t * d << "\n";
  150.             ll h = hp(1 + t * d);
  151.             cout << "hp = " << h << "\n\n";
  152.         }
  153.     }
  154.     //tehnarnik po t
  155.     /// rt = 1e18 / max(d, b * C - a) - 10
  156.     ll lt = 0, rt = 1e18 / max(d, b * c - a) - 10   , t1 = -10, t2 = 100, mn; //still withot noe (t - 1) ::d
  157.     if (sh) {
  158.         //rt = 30;
  159.     }
  160.     while (t1 + 1 != t2) {
  161.         t1 = lt + (rt - lt) / 3;
  162.         t2 = lt + 2 * (rt - lt) / 3;
  163.         if (sh) {
  164.             cout << "lt t1 t2 rt = " << lt << ' ' << t1 << ' ' << t2 << ' ' << rt << "\n";
  165.             cout << "hp(t1) = " << hp(1 + t1 * d) << "\n";
  166.             cout << "hp(t2) = " << hp(1 + t2 * d) << "\n";
  167.         }
  168.         if (hp(1 + t1 * d) < hp(1 + t2 * d)) {
  169.             mn = t1;
  170.             rt = t2;
  171.         } else {
  172.             mn = t2;
  173.             lt = t1;
  174.         }
  175.     }
  176.     if (sh) {
  177.         cout << "mn hp(mn) = " << mn << ' ' << hp(mn) << "\n";
  178.     }
  179.     if (rand_mode) {
  180.         cout << "RAND ANS:\n";
  181.     }
  182.     cout << max(a, -hp(1 + mn * d)) << "\n";
  183. }
  184. /*
  185. 3 1 7 3
  186. */
  187.  
  188.  
  189. int main() {
  190.     ios::sync_with_stdio(0);
  191.     cin.tie(0);
  192.     cout.tie(0);
  193.     srand(time(0));
  194.     int t = 1;
  195.     if (!sh && !rand_mode) cin >> t;
  196.     for (int go = 0; go < t; ++go) {
  197.         slv();
  198.     }
  199. }
  200.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement