Advertisement
Korotkodul

TG J stress 2

Oct 6th, 2022
743
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.61 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.     if (del < 0) {
  104.         cout << "BAD del\n";
  105.         exit(0);
  106.     }
  107.     //if ()
  108.     /*if (sh) {
  109.         cout << "ft = " << ft << "\n";
  110.         cout << "ft * (b * c - a) = " << ft * (b * c - a) << "\n";
  111.         cout << "lid = " << lid << "\n";
  112.         cout << "lend = " << lend << "\n\n";
  113.  
  114.         cout << "nmb = " << nmb << "\n";
  115.         cout << "step = " << step << "\n";
  116.         cout << "a1 = " << a1 << "\n";
  117.         cout << "del = " << del << "\n";
  118.         cout << "id = " << id << "\n";
  119.         //cout << "\n";
  120.     }*/
  121.  
  122.     res = ft * (b * c - a) - del;
  123.     if (res <= 0) {
  124.         cout << "BAD res !!!\n";
  125.         exit(0);
  126.     }
  127.     return res;
  128. }
  129.  
  130. /*
  131. 228 21 11 3
  132.  
  133.  
  134. 3 1 7 3
  135. */
  136.  
  137. bool rand_mode = 1;
  138.  
  139. void slv() {
  140.     if (!rand_mode) {
  141.         cin >> a >> b >> c >> d;
  142.     } else {
  143.         vector <ll> v = {(ll)1, (ll)1e6};
  144.         a = rand() % 3 + v[rand() % 2];
  145.         b = rand() % 3 + v[rand() % 2];
  146.         c = rand() % 3 + v[rand() % 2];
  147.         d = rand() % 3 + v[rand() % 2];
  148.         cout << "GO\n";
  149.         cout << "a b c d = " << a << ' ' << b << ' ' << c << ' ' << d << "\n";
  150.         if (min(min(a, b), min(c, d)) <= 0) {
  151.             cout << "BAD!!!!\n";
  152.             cout << "a b c d = " << a << ' ' << b << ' ' << c << ' ' << d << "\n";
  153.             exit(0);
  154.         }
  155.     }
  156.     if (a > b * c) {
  157.         cout << -1 << "\n";
  158.         return;
  159.     }
  160.     if (sh) {
  161.         for (ll t = 1; t < 50; ++t) {
  162.             cout << "t = " << 1 + t * d << "\n";
  163.             ll h = hp(1 + t * d);
  164.             cout << "hp = " << h << "\n\n";
  165.         }
  166.     }
  167.     //tehnarnik po t
  168.     /// rt = 1e18 / max(d, b * C - a) - 10
  169.     ll lt = 0, rt = 1e18 / max(d, b * c - a) + 10, t1 = -10, t2 = 100, mn; //still withot noe (t - 1) ::d
  170.     if (sh) {
  171.         //rt = 30;
  172.     }
  173.     while (t1 + 1 != t2) {
  174.         t1 = lt + (rt - lt) / 3;
  175.         t2 = lt + 2 * (rt - lt) / 3;
  176.         if (sh) {
  177.             cout << "lt t1 t2 rt = " << lt << ' ' << t1 << ' ' << t2 << ' ' << rt << "\n";
  178.             cout << "hp(t1) = " << hp(1 + t1 * d) << "\n";
  179.             cout << "hp(t2) = " << hp(1 + t2 * d) << "\n";
  180.         }
  181.         if (hp(1 + t1 * d) < hp(1 + t2 * d)) {
  182.             mn = t1;
  183.             rt = t2;
  184.         } else {
  185.             mn = t2;
  186.             lt = t1;
  187.         }
  188.     }
  189.     if (sh) {
  190.         cout << "mn hp(mn) = " << mn << ' ' << hp(mn) << "\n";
  191.     }
  192.     cout << max(a, -hp(1 + mn * d)) << "\n";
  193. }
  194. /*
  195. 3 1 7 3
  196. */
  197.  
  198.  
  199. int main() {
  200.     /*ios::sync_with_stdio(0);
  201.     cin.tie(0);
  202.     cout.tie(0);*/
  203.     srand(time(0));
  204.     int t = rand();
  205.     if (!sh && !rand_mode) cin >> t;
  206.     while (1) {
  207.         slv();
  208.     }
  209. }
  210.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement