Advertisement
Korotkodul

TG J stress 3

Oct 6th, 2022
812
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.34 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. bool rand_mode = 1;
  67.  
  68. ll hp(ll t) {
  69.     ll res;
  70.     if (t < 1 + d) {
  71.         res = -a + (t - 1) * b;
  72.         return res;
  73.     }
  74.     ll ft = f(t);
  75.     ll most = ft * (b * c - a);
  76.     ll del;
  77.     ll l, r, m, lid, lend; //look for lid
  78.     ll id = (t - 1) / d + 1;
  79.     l = 0;
  80.     r = id;
  81.     while (l + 1 < r) {
  82.         m = (l + r) / 2;
  83.         if (1 + (m - 1) * d + c > t) {
  84.             r = m;
  85.         } else {
  86.             l = m;
  87.         }
  88.     }
  89.     lid = r;
  90.     /*for (int i = 0; i < 10; ++i) {
  91.         if (1 + i * d + c > t) {
  92.             lid = i;
  93.             break;
  94.         }
  95.     }*/
  96.  
  97.     //lend = 1 + lid * d + c;
  98.     lend = 1 + (lid - 1) * d + c;
  99.  
  100.     ll nmb = id - (lid - 1);
  101.     ll start = (lend - t) * nmb * b;
  102.     ll a1 = d * b * (nmb - 1);
  103.     ll step = -d * b;
  104.     del = start + S(a1, step, nmb - 1);
  105.     if (nmb - 1 <= 0) {
  106.         del = 0;
  107.     }
  108.     vector <ll> v = {ft, most, del, id, lid, lend, nmb, start, a1, -step};
  109.     if (rand_mode) {
  110.         cout << "ft = " << ft << "\n";
  111.         cout << "most = " << most << "\n";
  112.         cout << "del = " << del << "\n";
  113.         cout << "id = " << id << "\n";
  114.         cout << "lid = " << lid << "\n";
  115.         cout << "lend = " << lend << "\n";
  116.         cout << "nmb = " << nmb << "\n";
  117.         cout << "start = " << start << "\n";
  118.         cout << "a1 = " << a1 << "\n";
  119.         cout << "step = " << step << "\n";
  120.     }
  121.     if (*min_element(v.begin(), v.end()) < 0) {
  122.         cout << "BAD!!\n";
  123.         exit(0);
  124.     }
  125.     //if ()
  126.     /*if (sh) {
  127.         cout << "ft = " << ft << "\n";
  128.         cout << "ft * (b * c - a) = " << ft * (b * c - a) << "\n";
  129.         cout << "lid = " << lid << "\n";
  130.         cout << "lend = " << lend << "\n\n";
  131.  
  132.         cout << "nmb = " << nmb << "\n";
  133.         cout << "step = " << step << "\n";
  134.         cout << "a1 = " << a1 << "\n";
  135.         cout << "del = " << del << "\n";
  136.         cout << "id = " << id << "\n";
  137.         //cout << "\n";
  138.     }*/
  139.  
  140.     res = ft * (b * c - a) - del;
  141.     /*if (res <= 0) {
  142.         cout << "BAD res !!!\n";
  143.         cout << "res = " << res << "\n";
  144.         exit(0);
  145.     }*/
  146.     return res;
  147. }
  148.  
  149. /*
  150. 228 21 11 3
  151.  
  152.  
  153. 3 1 7 3
  154. */
  155.  
  156.  
  157.  
  158. void slv() {
  159.     if (!rand_mode) {
  160.         cin >> a >> b >> c >> d;
  161.     } else {
  162.         vector <ll> v = {(ll)1, (ll)1e6};
  163.         a = rand() % 3 + v[rand() % 2];
  164.         b = rand() % 3 + v[rand() % 2];
  165.         c = rand() % 3 + v[rand() % 2];
  166.         d = rand() % 3 + v[rand() % 2];
  167.         cout << "GO\n";
  168.         cout << "a b c d = " << a << ' ' << b << ' ' << c << ' ' << d << "\n";
  169.         if (min(min(a, b), min(c, d)) <= 0) {
  170.             cout << "BAD!!!!\n";
  171.             cout << "a b c d = " << a << ' ' << b << ' ' << c << ' ' << d << "\n";
  172.             exit(0);
  173.         }
  174.     }
  175.     if (a > b * c) {
  176.         cout << -1 << "\n";
  177.         return;
  178.     }
  179.     if (sh) {
  180.         for (ll t = 1; t < 50; ++t) {
  181.             cout << "t = " << 1 + t * d << "\n";
  182.             ll h = hp(1 + t * d);
  183.             cout << "hp = " << h << "\n\n";
  184.         }
  185.     }
  186.     //tehnarnik po t
  187.     /// rt = 1e18 / max(d, b * C - a) - 10
  188.     ll lt = 0, rt = 1e18 / max(d, b * c - a) + 10, t1 = -10, t2 = 100, mn; //still withot noe (t - 1) ::d
  189.     if (sh) {
  190.         //rt = 30;
  191.     }
  192.     while (t1 + 1 != t2) {
  193.         t1 = lt + (rt - lt) / 3;
  194.         t2 = lt + 2 * (rt - lt) / 3;
  195.         if (sh) {
  196.             cout << "lt t1 t2 rt = " << lt << ' ' << t1 << ' ' << t2 << ' ' << rt << "\n";
  197.             cout << "hp(t1) = " << hp(1 + t1 * d) << "\n";
  198.             cout << "hp(t2) = " << hp(1 + t2 * d) << "\n";
  199.         }
  200.         if (hp(1 + t1 * d) < hp(1 + t2 * d)) {
  201.             mn = t1;
  202.             rt = t2;
  203.         } else {
  204.             mn = t2;
  205.             lt = t1;
  206.         }
  207.     }
  208.     if (sh) {
  209.         cout << "mn hp(mn) = " << mn << ' ' << hp(mn) << "\n";
  210.     }
  211.     cout << max(a, -hp(1 + mn * d)) << "\n";
  212. }
  213. /*
  214. 3 1 7 3
  215. */
  216.  
  217. /*
  218. a b c d = 1000002 3 1000001 1
  219. BAD res !!!
  220. */
  221.  
  222. int main() {
  223.     /*ios::sync_with_stdio(0);
  224.     cin.tie(0);
  225.     cout.tie(0);*/
  226.     srand(time(0));
  227.     int t = rand();
  228.     if (!sh && !rand_mode) cin >> t;
  229.     int cnt = 0;
  230.     while (cnt < t) {
  231.         cnt++;
  232.         slv();
  233.     }
  234. }
  235.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement