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)
- 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;
- }
- 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 (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;
- return res;
- }
- /*
- 228 21 11 3
- 3 1 7 3
- */
- void slv() {
- cin >> a >> b >> c >> d;
- 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
- ll lt = 0, rt = 1e15, 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
- */
- int main() {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- int t = 1;
- if (!sh) cin >> t;
- for (int go = 0; go < t; ++go) {
- slv();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement