Advertisement
Korotkodul

TG I 3

Oct 5th, 2022 (edited)
775
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.66 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 = 1;
  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. 3 1 7 3
  123. */
  124.  
  125.  
  126. void slv() {
  127.     cin >> a >> b >> c >> d;
  128.     if (a > b * c) {
  129.         cout << -1 << "\n";
  130.         return;
  131.     }
  132.     if (sh) {
  133.         for (ll t = 1; t < 15; ++t) {
  134.             cout << "t = " << t << "\n";
  135.             ll h = hp(t);
  136.             cout << "hp = " << h << "\n\n";
  137.         }
  138.     }
  139.     //tehnarnik po t
  140.     /*ll ls = 0, rs = 1e5, s1, s2, mn;
  141.     if (sh) {
  142.         rs = 30;
  143.     }
  144.     while (ls + 3 < rs) {
  145.  
  146.         s1 = ls + (rs - ls) / 3;
  147.         s2 = ls + 2 * (rs - ls) / 3;
  148.         if (sh) {
  149.             cout << "ls s1 s2 rs = " << ls << ' ' << s1 << ' ' << s2 << ' ' << rs << "\n";
  150.             cout << "hp(s1) = " << hp(s1) << "\n";
  151.             cout << "hp(s2) = " << hp(s2) << "\n";
  152.         }
  153.         if (hp(s1) < hp(s2)) {
  154.             mn = s1;
  155.             rs = s2;
  156.         } else {
  157.             mn = s2;
  158.             ls = s1;
  159.         }
  160.     }
  161.     if (sh) {
  162.         cout << "mn hp(mn) = " << mn << ' ' << hp(mn) << "\n";
  163.     }
  164.     cout << -hp(mn) << "\n";*/
  165. }
  166. /*
  167. 3 1 7 3
  168. */
  169.  
  170.  
  171. int main() {
  172.     ios::sync_with_stdio(0);
  173.     cin.tie(0);
  174.     cout.tie(0);
  175.     int t = 1;
  176.     if (!sh) cin >> t;
  177.     for (int go = 0; go < t; ++go) {
  178.         slv();
  179.     }
  180. }
  181.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement