function getCount(n, k) {
if (k < 1) return 0;
const map = new Map();
function calc(f, n, k) {
if (n < f * k || n > 9 * k) {
return 0;
}
if (k < 2) {
return 1;
}
const key = `${f}_${n}_${k}`;
const saved = map.get(key);
if (saved != null) { return saved; }
let count = 0;
for (let i = f; i <= 9; ++i) {
count += calc(i, n - i, k - 1);
}
map.set(key, count);
return count;
}
return calc(1, n, k);
}
function getMin(n, k) {
if (k < 1 || n < k || n > 9 * k) {
return 0;
}
let r = 0;
while (k > 1 && n - 1 <= 9 * (k - 1)) {
r = r * 10 + 1;
n--;
k--;
}
k--;
r = r * 10 + n - 9 * k;
while (k > 0) {
r = r * 10 + 9;
k--;
}
return r;
}
function getMax(n, k) {
if (k < 1 || n < k || n > 9 * k) {
return 0;
}
const base = Math.floor(n / k);
const head = k - n % k;
let r = 0;
for (i = 0; i < k; ++i) {
r = r * 10 + base + (i < head ? 0 : 1);
}
return r;
}
function luckyNumbers(n, k) {
const max = getMax(n, k);
if (!max) return [0];
return [getCount(n, k), getMin(n, k), max];
}
alert(luckyNumbers(10, 3));