Advertisement
Korotkodul

TG J TL

Oct 6th, 2022
727
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.03 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 = 0;
  130.  
  131. void slv() {
  132.     if (!rand_mode) {
  133.         cin >> a >> b >> c >> d;
  134.     } else {
  135.         a = rand() % (ll)1e6 + 3;
  136.         b = rand() % (ll)1e6 + 3;
  137.         c = rand() % (ll)1e6 + 3;
  138.         d = rand() % (ll)1e6 + 3;
  139.     }
  140.     if (a > b * c) {
  141.         cout << -1 << "\n";
  142.         return;
  143.     }
  144.     if (sh) {
  145.         for (ll t = 1; t < 50; ++t) {
  146.             cout << "t = " << 1 + t * d << "\n";
  147.             ll h = hp(1 + t * d);
  148.             cout << "hp = " << h << "\n\n";
  149.         }
  150.     }
  151.     //tehnarnik po t
  152.     ll lt = 0, rt = 1e18 / (d + 10), t1 = -10, t2 = 100, mn; //still withot noe (t - 1) ::d
  153.     if (sh) {
  154.         //rt = 30;
  155.     }
  156.     while (t1 + 1 != t2) {
  157.         t1 = lt + (rt - lt) / 3;
  158.         t2 = lt + 2 * (rt - lt) / 3;
  159.         if (sh) {
  160.             cout << "lt t1 t2 rt = " << lt << ' ' << t1 << ' ' << t2 << ' ' << rt << "\n";
  161.             cout << "hp(t1) = " << hp(1 + t1 * d) << "\n";
  162.             cout << "hp(t2) = " << hp(1 + t2 * d) << "\n";
  163.         }
  164.         if (hp(1 + t1 * d) < hp(1 + t2 * d)) {
  165.             mn = t1;
  166.             rt = t2;
  167.         } else {
  168.             mn = t2;
  169.             lt = t1;
  170.         }
  171.     }
  172.     if (sh) {
  173.         cout << "mn hp(mn) = " << mn << ' ' << hp(mn) << "\n";
  174.     }
  175.     cout << max(a, -hp(1 + mn * d)) << "\n";
  176. }
  177. /*
  178. 3 1 7 3
  179. */
  180.  
  181.  
  182. int main() {
  183.     ios::sync_with_stdio(0);
  184.     cin.tie(0);
  185.     cout.tie(0);
  186.     srand(time(0));
  187.     int t = 1;
  188.     if (!sh) cin >> t;
  189.     for (int go = 0; go < t; ++go) {
  190.         slv();
  191.     }
  192. }
  193.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement