-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path122-efficient-exponentiation.js
More file actions
100 lines (81 loc) · 2.09 KB
/
122-efficient-exponentiation.js
File metadata and controls
100 lines (81 loc) · 2.09 KB
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
/**
* Efficient Exponentiation
* Time Complexity: O(T * K^1.5) approx via IDDFS
* Space Complexity: O(log K)
*/
const solutionCache = new Map();
function solve(k) {
if (solutionCache.has(k)) return solutionCache.get(k);
let lowerBound = 0;
let temp = 1;
while (temp < k) {
temp *= 2;
lowerBound++;
}
for (let depth = lowerBound; ; depth++) {
const path = new Int32Array(depth + 1);
path[0] = 1;
if (dfs(1, depth, k, path)) {
const result = Array.from(path);
solutionCache.set(k, result);
return result;
}
}
};
function dfs(currentIdx, maxDepth, target, path) {
const currentVal = path[currentIdx - 1];
if (currentVal === target) {
return true;
}
if (currentIdx > maxDepth) {
return false;
}
const remainingSteps = maxDepth - (currentIdx - 1);
if (currentVal * (1 << remainingSteps) < target) {
return false;
}
for (let i = currentIdx - 1; i >= 0; i--) {
const nextVal = currentVal + path[i];
if (nextVal <= currentVal) break;
if (nextVal > target) continue;
path[currentIdx] = nextVal;
if (dfs(currentIdx + 1, maxDepth, target, path)) {
return true;
}
}
return false;
};
function processData(input) {
const tokens = input.trim().split(/\s+/);
if (tokens.length === 0) return;
const T = parseInt(tokens[0], 10);
let ptr = 1;
for (let i = 0; i < T; i++) {
const k = parseInt(tokens[ptr++], 10);
if (k === 1) {
console.log(0);
continue;
}
const chain = solve(k);
const steps = chain.length - 1;
console.log(steps);
for (let j = 1; j < chain.length; j++) {
const result = chain[j];
const op1 = chain[j - 1];
const op2 = result - op1;
const s1 = op1 === 1 ? "n^1" : `n^${op1}`;
const s2 = op2 === 1 ? "n^1" : `n^${op2}`;
const s3 = `n^${result}`;
console.log(`${s1} * ${s2} = ${s3}`);
}
}
};
process.stdin.resume();
process.stdin.setEncoding("ascii");
let _input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});