-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path052-permuted-multiples.js
More file actions
61 lines (50 loc) · 1.19 KB
/
052-permuted-multiples.js
File metadata and controls
61 lines (50 loc) · 1.19 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
/**
* Permuted Multiples
* Time Complexity: O(N * K * D)
* Space Complexity: O(1)
*/
function processData(input) {
input = input.trim().split(/\s+/).map(Number);
const N = input[0];
const K = input[1];
function sig(n) {
const cnt = new Array(10).fill(0);
while (n > 0) {
cnt[n % 10]++;
n = Math.floor(n / 10);
}
return cnt.join('#');
}
let out = [];
for (let x = 1; x <= N; x++) {
const len = ('' + x).length;
const s = sig(x);
let ok = true;
const line = [x];
for (let m = 2; m <= K; m++) {
const v = m * x;
if (('' + v).length !== len) {
ok = false;
break;
}
if (sig(v) !== s) {
ok = false;
break;
}
line.push(v);
}
if (ok) {
out.push(line.join(" "));
}
}
console.log(out.join("\n"));
};
process.stdin.resume();
process.stdin.setEncoding("ascii");
let _input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});