Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.69 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     int n;
  7.     cin >> n;
  8.     for(int i = 0; i < n; i++) {
  9.         long long x, y, a, b;
  10.         cin >> x >> y >> a >> b;
  11.         long long l = -1, r = 1e12;
  12.         while(r - l > 1) {
  13.             long long m = (r + l) / 2;
  14.             if((long double)m * m + m - 2 * (long double)(y - x) / (a + b) >= 0.0) r = m;
  15.             else l = m;
  16.         }
  17.         long long curx = (long long)x + (long long) a * r * (r + 1) / 2, cury = (long long)y - (long long) b * r * (r + 1) / 2;
  18.         long long ans = curx;
  19.         for (int j = 1; j < 3; j++) {
  20.             cury += b * r;
  21.             if (cury <= curx) {
  22.                 ans = curx;
  23.             }
  24.             curx -= a * r;
  25.             if (cury <= curx) {
  26.                 ans = curx;
  27.             }
  28.             r--;
  29.         }
  30.         cout << ans << endl;
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement