-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathG.cpp
55 lines (40 loc) · 883 Bytes
/
G.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <bits/stdc++.h>
using namespace std;
#define nl '\n'
/*
type1: ap , bp
type2: bq , aq
*/
void run_cases() {
int64_t x, y, a, b;
cin >> x >> y >> a >> b;
if(x > y) swap(x, y);
if(a > b) swap(a, b);
if(a == b) {
cout << min(x, y) / a << '\n';
return;
}
int64_t l = 0, r = 1e9+100;
while(r > l + 1) {
int64_t m = (r + l) / 2;
bool ok = false;
int64_t xx = x - m * a;
int64_t yy = y - m * a;
int64_t diff = (b - a);
ok = xx >= 0 && yy >= 0 && (xx / diff + yy / diff) >= m;
if(ok) {
l = m;
} else {
r = m;
}
}
cout << l << '\n';
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(nullptr);
int tests = 1;
cin >> tests;
for(int test = 1;test <= tests;test++) {
run_cases();
}
}