Advertisement
Korotkodul

TG I 2

Oct 5th, 2022
692
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.27 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.     return 1 + (t - 1)  / d;
  59. }
  60.  
  61. ll g(ll t) {
  62.     ll k = (t - 1 - c) / d;
  63.     ll a1 = 1 + k * d + c - t;
  64.     ll N = f(t) - (k - 1);
  65.     ll del = S(a1, d * b, N);
  66.     ll res = f(t) * b * c - del;
  67.     return res;
  68. }
  69.  
  70. ll hp(ll t) {
  71.     return g(t) - f(t) * a ;
  72. }
  73.  
  74.  
  75.  
  76. void slv() {
  77.     cin >> a >> b >> c >> d;
  78.     if (a > b * c) {
  79.         cout << -1 << "\n";
  80.         return;
  81.     }
  82.     //tehnarnik po t
  83.     ll ls = 0, rs = 1e5, s1, s2, mn;
  84.     if (sh) {
  85.         rs = 30;
  86.     }
  87.     while (ls + 3 < rs) {
  88.  
  89.         s1 = ls + (rs - ls) / 3;
  90.         s2 = ls + 2 * (rs - ls) / 3;
  91.         if (sh) {
  92.             cout << "ls s1 s2 rs = " << ls << ' ' << s1 << ' ' << s2 << ' ' << rs << "\n";
  93.             cout << "hp(s1) = " << hp(s1) << "\n";
  94.             cout << "hp(s2) = " << hp(s2) << "\n";
  95.         }
  96.         if (hp(s1) < hp(s2)) {
  97.             mn = s1;
  98.             rs = s2;
  99.         } else {
  100.             mn = s2;
  101.             ls = s1;
  102.         }
  103.     }
  104.     if (sh) {
  105.         cout << "mn hp(mn) = " << mn << ' ' << hp(mn) << "\n";
  106.     }
  107.     cout << -hp(mn) << "\n";
  108. }
  109.  
  110.  
  111.  
  112. int main() {
  113.     ios::sync_with_stdio(0);
  114.     cin.tie(0);
  115.     cout.tie(0);
  116.     int t = 1;
  117.     if (!sh) cin >> t;
  118.     for (int go = 0; go < t; ++go) {
  119.         slv();
  120.     }
  121. }
  122.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement