Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <queue>
- #include <algorithm>
- #include <string>
- #include <stack>
- #include <set>
- #include <map>
- #define pii pair <int, int>
- #define pb(x) push_back(x)
- #include <ctime>
- using namespace std;
- using ll = long long;
- using ld = long double;
- using db = double;
- void cv(vector <int> &v) {
- for (auto x : v) cout << x << ' ';
- cout << "\n";
- }
- void cvl(vector <ll> &v) {
- for (auto x : v) cout << x << ' ';
- cout << "\n";
- }
- void cvv(vector <vector <int> > &v) {
- for (auto x : v) cv(x);
- cout << "\n";
- }
- void cvb(vector <bool> v) {
- for (bool x : v) cout << x << ' ';
- cout << "\n";
- }
- void cvs(vector <string> v) {
- for (auto a : v) {
- cout << a << "\n";
- }
- }
- void cvp(vector <pii> a) {
- for (auto p : a) {
- cout << p.first << ' ' << p.second << "\n";
- }
- cout << "\n";
- }
- ll a, b, c, d;
- bool sh = 0;
- ll S(ll a1, ll d, ll n) {
- return (2 * a1 + d * (n - 1)) * n / 2;
- }
- ll f(ll t) {
- ll res = 1 + (t - 1) / d;
- if (sh) {
- //cout << "f(" << t << ") = " << res << "\n";
- }
- return res;
- }
- bool rand_mode = 1;
- ll hp(ll t) {
- ll res;
- if (t < 1 + d) {
- res = -a + (t - 1) * b;
- return res;
- }
- ll ft = f(t);
- ll most = ft * (b * c - a);
- ll del;
- ll l, r, m, lid, lend; //look for lid
- ll id = (t - 1) / d + 1;
- l = 0;
- r = id;
- while (l + 1 < r) {
- m = (l + r) / 2;
- if (1 + (m - 1) * d + c > t) {
- r = m;
- } else {
- l = m;
- }
- }
- lid = r;
- /*for (int i = 0; i < 10; ++i) {
- if (1 + i * d + c > t) {
- lid = i;
- break;
- }
- }*/
- //lend = 1 + lid * d + c;
- lend = 1 + (lid - 1) * d + c;
- ll nmb = id - (lid - 1);
- ll start = (lend - t) * nmb * b;
- ll a1 = d * b * (nmb - 1);
- ll step = -d * b;
- del = start + S(a1, step, nmb - 1);
- if (nmb - 1 <= 0) {
- del = 0;
- }
- vector <ll> v = {ft, most, del, id, lid, lend, nmb, start, a1, -step};
- if (rand_mode) {
- cout << "ft = " << ft << "\n";
- cout << "most = " << most << "\n";
- cout << "del = " << del << "\n";
- cout << "id = " << id << "\n";
- cout << "lid = " << lid << "\n";
- cout << "lend = " << lend << "\n";
- cout << "nmb = " << nmb << "\n";
- cout << "start = " << start << "\n";
- cout << "a1 = " << a1 << "\n";
- cout << "step = " << step << "\n";
- }
- if (*min_element(v.begin(), v.end()) < 0) {
- cout << "BAD!!\n";
- exit(0);
- }
- //if ()
- /*if (sh) {
- cout << "ft = " << ft << "\n";
- cout << "ft * (b * c - a) = " << ft * (b * c - a) << "\n";
- cout << "lid = " << lid << "\n";
- cout << "lend = " << lend << "\n\n";
- cout << "nmb = " << nmb << "\n";
- cout << "step = " << step << "\n";
- cout << "a1 = " << a1 << "\n";
- cout << "del = " << del << "\n";
- cout << "id = " << id << "\n";
- //cout << "\n";
- }*/
- res = ft * (b * c - a) - del;
- /*if (res <= 0) {
- cout << "BAD res !!!\n";
- cout << "res = " << res << "\n";
- exit(0);
- }*/
- return res;
- }
- /*
- 228 21 11 3
- 3 1 7 3
- */
- void slv() {
- if (!rand_mode) {
- cin >> a >> b >> c >> d;
- } else {
- vector <ll> v = {(ll)1, (ll)1e6};
- a = rand() % 3 + v[rand() % 2];
- b = rand() % 3 + v[rand() % 2];
- c = rand() % 3 + v[rand() % 2];
- d = rand() % 3 + v[rand() % 2];
- cout << "GO\n";
- cout << "a b c d = " << a << ' ' << b << ' ' << c << ' ' << d << "\n";
- if (min(min(a, b), min(c, d)) <= 0) {
- cout << "BAD!!!!\n";
- cout << "a b c d = " << a << ' ' << b << ' ' << c << ' ' << d << "\n";
- exit(0);
- }
- }
- if (a > b * c) {
- cout << -1 << "\n";
- return;
- }
- if (sh) {
- for (ll t = 1; t < 50; ++t) {
- cout << "t = " << 1 + t * d << "\n";
- ll h = hp(1 + t * d);
- cout << "hp = " << h << "\n\n";
- }
- }
- //tehnarnik po t
- /// rt = 1e18 / max(d, b * C - a) - 10
- ll lt = 0, rt = 1e18 / max(d, b * c - a) + 10, t1 = -10, t2 = 100, mn; //still withot noe (t - 1) ::d
- if (sh) {
- //rt = 30;
- }
- while (t1 + 1 != t2) {
- t1 = lt + (rt - lt) / 3;
- t2 = lt + 2 * (rt - lt) / 3;
- if (sh) {
- cout << "lt t1 t2 rt = " << lt << ' ' << t1 << ' ' << t2 << ' ' << rt << "\n";
- cout << "hp(t1) = " << hp(1 + t1 * d) << "\n";
- cout << "hp(t2) = " << hp(1 + t2 * d) << "\n";
- }
- if (hp(1 + t1 * d) < hp(1 + t2 * d)) {
- mn = t1;
- rt = t2;
- } else {
- mn = t2;
- lt = t1;
- }
- }
- if (sh) {
- cout << "mn hp(mn) = " << mn << ' ' << hp(mn) << "\n";
- }
- cout << max(a, -hp(1 + mn * d)) << "\n";
- }
- /*
- 3 1 7 3
- */
- /*
- a b c d = 1000002 3 1000001 1
- BAD res !!!
- */
- int main() {
- /*ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);*/
- srand(time(0));
- int t = rand();
- if (!sh && !rand_mode) cin >> t;
- int cnt = 0;
- while (cnt < t) {
- cnt++;
- slv();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement