Advertisement
Korotkodul

TG I last

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