π₯ Algorithm/Baekjoon
[λ°±μ€ 11057λ²] μ€λ₯΄λ§ μ - μλ°μ€ν¬λ¦½νΈ(nodejs)
Lennon
2022. 3. 18. 20:12
728x90
λ°μν
https://www.acmicpc.net/problem/11057
const { off } = require('process');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let input;
rl.on('line', function (line) {
input = +line;
rl.close();
}).on('close', function () {
const dp = Array.from(Array(input + 1), () => new Array());
dp[1] = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
dp[2] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
for (let i = 3; i <= input; i++) {
for (let j = 0; j < dp[i - 1].length; j++) {
const value = [...dp[i - 1]];
dp[i][j] = value.slice(0, j + 1).reduce((acc, cur) => acc + cur) % 10007;
}
}
console.log(dp[input].reduce((acc, cur) => acc + cur) % 10007);
});
μ νμ μΈ DPλ¬Έμ μ΄λ€. μ¬μ΄ DPλ¬Έμ μμ κ°μ₯ λ§μ΄ μ¬μ©νλ μ©λ²μΈ κ² κ°λ€.
N | ||||||||||
1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
2 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
3 | 1 | 3 | 6 | 10 | 15 | 21 | 28 | 36 | 45 | 55 |
4 | 1 | 4 | 10 | 20 | 35 | 56 | 84 | 120 | 165 | 220 |
Nμ κ·Έλ₯ μ λΆλΆμ indexλ§νΌ λν κ°μ λ£μ΄μ£Όλ©΄ λλ€.
ex) Nμ΄ 4μΈ κ²½μ°μμ 3λ²μ§Έ μΈλ±μ€μΈ 20μ Nμ΄ i-1μΈ 3μμ 1+3+6+10 ν κ²°κ³Όμ κ°μ!
μ½λ©ν μ€νΈμμ DPλ¬Έμ λ₯Ό λ΄€μ λ λμ λλ λλμ΄ λ λ€λ©΄ μμ²λΌ λμνλ μ§ νμΈν΄λ³΄μ
728x90
λ°μν