πŸ”₯ Algorithm/Baekjoon

[λ°±μ€€ 11057번] 였λ₯΄λ§‰ 수 - μžλ°”μŠ€ν¬λ¦½νŠΈ(nodejs)

Lennon 2022. 3. 18. 20:12
728x90
λ°˜μ‘ν˜•

https://www.acmicpc.net/problem/11057

 

11057번: 였λ₯΄λ§‰ 수

였λ₯΄λ§‰ μˆ˜λŠ” 수의 μžλ¦¬κ°€ μ˜€λ¦„μ°¨μˆœμ„ μ΄λ£¨λŠ” 수λ₯Ό λ§ν•œλ‹€. μ΄λ•Œ, μΈμ ‘ν•œ μˆ˜κ°€ 같아도 μ˜€λ¦„μ°¨μˆœμœΌλ‘œ μΉœλ‹€. 예λ₯Ό λ“€μ–΄, 2234와 3678, 11119λŠ” 였λ₯΄λ§‰ μˆ˜μ΄μ§€λ§Œ, 2232, 3676, 91111은 였λ₯΄λ§‰ μˆ˜κ°€ μ•„λ‹ˆλ‹€. 수

www.acmicpc.net

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
λ°˜μ‘ν˜•