-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path207-integer-partition-equations.js
More file actions
50 lines (40 loc) · 968 Bytes
/
207-integer-partition-equations.js
File metadata and controls
50 lines (40 loc) · 968 Bytes
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
/*
* Integer Partition Equations
* Time Complexity: O(Q * log(b))
* Space Complexity: O(1)
*/
function processData(input) {
const tokens = input.trim().split(/\s+/);
let ptr = 0;
if (ptr >= tokens.length) return;
const Q = parseInt(tokens[ptr++]);
for (let i = 0; i < Q; i++) {
const a = BigInt(tokens[ptr++]);
const b = BigInt(tokens[ptr++]);
let w = 1n;
while (true) {
const p2_w = 1n << w;
const L = p2_w - 1n;
const R = (p2_w << 1n) - 2n;
if (w * b < a * R) {
let min_x = (w * b) / a + 1n;
if (min_x < L) {
min_x = L;
}
const m = min_x * (min_x + 1n);
process.stdout.write(m.toString() + "\n");
break;
}
w++;
}
}
};
process.stdin.resume();
process.stdin.setEncoding("ascii");
let _input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});