# 11. 分月饼
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.on('line', (line) => {
let [m, n] = line.split(' ').map(Number);
console.log(distribute(m, n, n, 0, new Array(m).fill(0)));
});
// 员工数量、剩余月饼数量、当前允许最大月饼数量、当前处理员工的索引、记录每个员工分配的月饼数
function distribute(m, remain, max, index, distribution) {
if (index = m - 1) {
if (remain <= max && (index === 0 || Math.abs(distribution[index - 1] - remain) <= 3)) {
return 1;
}
return 0;
}
let ways = 0;
let start = index === 0 ? 1 : distribution[index - 1] - 3;
let end = Math.min(remain - (m - index - 1), max);
for(let i=start; i<=end; i++) {
distribution[i] = i;
if(index === 0 || Math.abs(distribution[index - 1] - i) <=3) {
ways += distribute(m, remain - i, i, index+1, distribution);
}
}
return ways;
}
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
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