-
Notifications
You must be signed in to change notification settings - Fork 0
/
2002F.cpp
282 lines (240 loc) · 6.15 KB
/
2002F.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#ifdef DBG
#include "debug.h"
#else
#define dbg(...)
#define dbg_export(...)
#endif
i64 n, m, l, f;
/* Snippet Header */
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
/* Snippet BEGIN */
class NumberTheory
{
public:
NumberTheory(int n) : n(n), min_divisor(n + 1, 0) { init(); }
vector<int> factorize(int x)
{
vector<int> ret;
while (x != 1) {
int xp = min_divisor[x];
ret.push_back(min_divisor[x]);
while (x % xp == 0)
x /= xp;
}
return ret;
}
vector<int> &all_primes() { return primes; }
bool is_prime(int n) { return min_divisor[n] == n; }
// Returns x^(-1) (mod m)
private:
int n;
vector<int> min_divisor;
vector<int> primes;
void init()
{
for (int i = 2; i <= n; i++) {
if (min_divisor[i] == 0) {
min_divisor[i] = i;
primes.push_back(i);
}
for (int j : primes) {
if (i * j > n)
break;
min_divisor[i * j] = j;
if (i % j == 0) {
break;
} else {
}
}
}
}
static int pow(int a, int n, int mod)
{
int res = 1;
while (n) {
if (n & 1) {
res = (i64)res * a % mod;
}
a = (i64)a * a % mod;
n >>= 1;
}
return res;
}
};
// Return [ x, y, d ]
// a * x + b * y = d = gcd(a, b)
//
// All solution x' and y':
// x' = x + k * (b / d)
// y' = y - k * (a / d)
tuple<i64, i64, i64> extended_gcd(i64 a, i64 b)
{
if (b == 0)
return {1, 0, a};
auto [x, y, d] = extended_gcd(b, a % b);
return {y, x - a / b * y, d};
}
// [Tutorial] Chinese Remainder Theorem
// https://codeforces.com/blog/entry/61290
// x == a1 (mod n1)
// x == a2 (mod n2)
// Return [ ret, lcm(n1, n2) ]
// Solution:
// x == ret (mod lcm(n1, n2))
// 0 <= ret < lcm(n1, n2)
optional<tuple<i64, i64>> chinese_remainder(i64 a1, i64 n1, i64 a2, i64 n2)
{
// x = k1 * n1 + a1 = k2 * n2 + a2
// n1 * (-k1) + n2 * k2 = a1 - a2
auto [k1, k2, d] = extended_gcd(n1, n2);
k1 = -k1;
#ifdef DBG
assert(n1 * (-k1) + n2 * k2 == d);
#endif
if ((a1 - a2) % d != 0) {
return nullopt;
}
i64 scaled = (a1 - a2) / d;
i64 n2_ = n2 / d;
i64 lcm = n1 * n2_;
// x = (a1 + k1 * n1 % lcm) % lcm
// Note that k1 * n1 overflows (because k1~1e18, n1~1e9)
//
// Note that X * n1 % lcm
// = X * n1 % (n1 * n2 / d) % lcm
// = X * (n2 / d) * n1 % lcm <-- avoid overflow
i64 x = (a1 + k1 % n2_ * scaled % n2_ * n1 % lcm) % lcm;
if (x < lcm)
x += lcm;
return {{x, lcm}};
}
// https://stackoverflow.com/questions/63436490/divide-integers-with-floor-ceil-and-outwards-rounding-modes-in-c
i64 floor_div(i64 a, i64 b)
{
// auto res = lldiv(a, b);
i64 d = a / b;
i64 r = a % b;
bool quotientNegative = (b < 0) != (a < 0);
return d - (r != 0 && quotientNegative);
}
i64 ceil_div(i64 a, i64 b)
{
i64 d = a / b;
i64 r = a % b;
bool quotientPositive = (b >= 0) == (a >= 0);
return d + (r != 0 && quotientPositive);
}
// https://stackoverflow.com/questions/1903954/is-there-a-standard-sign-function-signum-sgn-in-c-c
template <typename T> int sgn(T val) { return (T(0) < val) - (val < T(0)); }
// Away from the zero
i64 up_div(i64 a, i64 b)
{
i64 d = a / b;
i64 r = a % b;
signed char quotientSgn = sgn(a) * sgn(b);
return d + (r != 0) * quotientSgn;
}
i64 down_div(i64 a, i64 b) { return a / b; }
// Given that x == a (mod m)
// NOT require that 0 <= a < m
// Returns how many x fall in [l, r]
i64 count_in_between(i64 a, i64 m, i64 l, i64 r)
{
assert(l <= r);
assert(m > 0);
i64 k_min = ceil_div(l - a, m);
i64 k_max = floor_div(r - a, m);
if (k_max < k_min) {
return 0;
}
return k_max - k_min + 1;
}
// Find the min x that satisfies x == a (mod m) and x >= l
i64 find_min_x(i64 a, i64 m, i64 l)
{
i64 k_min = ceil_div(l - a, m);
return a + k_min * m;
}
// Find the max x that satisfies x == a (mod m) and x <= r
i64 find_max_x(i64 a, i64 m, i64 r)
{
i64 k_max = floor_div(r - a, m);
return a + k_max * m;
}
/* Snippet END */
#ifdef DBG
NumberTheory number_theory(100000);
#else
NumberTheory number_theory((int)2e7 + 5);
#endif
i64 solve()
{
assert(n <= m);
assert(n >= 2);
auto &primes = number_theory.all_primes();
auto iter = upper_bound(primes.begin(), primes.end(), m);
iter--;
int q = *iter;
auto iter2 = upper_bound(primes.begin(), primes.end(), min((int)n, q - 1));
if (iter2 != primes.begin()) {
iter2--;
// require: gdb(i, p) == 1 for all i in [q, m]
while (*iter2 > 2 && q / *iter2 < m / *iter2) {
iter2--;
}
}
int p = *iter2;
if (p == 2)
p = 1;
else {
assert(q % p != 0);
}
dbg(p, n, q, m);
i64 ret = 0;
vector<vector<char>> vis(n - p + 1, vector<char>(m - q + 1));
for (int op = 0; p + op <= n; op++)
for (int oq = 0; q + oq <= m; oq++) {
int pp = p + op;
int qq = q + oq;
if (pp >= qq)
continue;
auto &visit = vis[op][oq];
if (op == 0 || oq == 0) {
visit = true;
} else {
int g = gcd(pp, qq);
if (g == 1) {
visit = vis[op - 1][oq] || vis[op][oq - 1];
}
}
if (visit) {
dbg(pp, qq);
i64 value = l * pp + f * qq;
ret = max(ret, value);
if (pp <= m && qq <= n) {
i64 value = l * qq + f * pp;
ret = max(ret, value);
}
}
}
return ret;
}
int main()
{
#ifndef DBG
ios::sync_with_stdio(false);
cin.tie(nullptr);
#endif
int t;
cin >> t;
while (t--) {
cin >> n >> m >> l >> f;
cout << solve() << endl;
}
return 0;
}