๊ด€๋ฆฌ ๋ฉ”๋‰ด

๐Ÿ–ฅ dev-ruby

[๋ฐฑ์ค€-2502] ๋–ก ๋จน๋Š” ํ˜ธ๋ž‘์ด | node.js | silver1 ๋ณธ๋ฌธ

๋ฐฑ์ค€

[๋ฐฑ์ค€-2502] ๋–ก ๋จน๋Š” ํ˜ธ๋ž‘์ด | node.js | silver1

ruby_s 2022. 6. 2. 15:29
728x90
๋ฐ˜์‘ํ˜•
SMALL

๋ฌธ์ œ

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

 

2502๋ฒˆ: ๋–ก ๋จน๋Š” ํ˜ธ๋ž‘์ด

์ฒซ์ค„์— ์ฒซ ๋‚ ์— ์ค€ ๋–ก์˜ ๊ฐœ์ˆ˜ A๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ๊ทธ ๋‹ค์Œ ๋‘˜์งธ ์ค„์—๋Š” ๋‘˜์งธ ๋‚ ์— ์ค€ ๋–ก์˜ ๊ฐœ์ˆ˜ B๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ์ด ๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ D, K์— ๋Œ€ํ•ด์„œ๋Š” ํ•ญ์ƒ ์ •์ˆ˜ A, B (1≤ A ≤ B)๊ฐ€ ์กด์žฌํ•œ๋‹ค. 

www.acmicpc.net

 

ํ’€์ด

function solve() {
  const aDp = [1, 0];
  const bDp = [0, 1];

  for (let i = 2; i < d; i++) {
    aDp[i] = aDp[i - 1] + aDp[i - 2];
    bDp[i] = bDp[i - 1] + bDp[i - 2];
  }
  const aCoef = aDp[d - 1];
  const bCoef = bDp[d - 1];

  for (let i = 1; i <= k; i++) {
    const remain = k - aCoef * i;
    if (remain % bCoef === 0) {
      console.log(`${i}\n${remain / bCoef}`);
      break;
    }
  }
}

const filePath =
  process.platform === "linux" ? "/dev/stdin" : "๋ฐฑ์ค€/silver/2502/testcase.txt";
const input = require("fs")
  .readFileSync(filePath)
  .toString()
  .trim()
  .split("\n");
let [d, k] = input[0].split(" ").map((el) => +el);
solve();

 

์ด ๋ฌธ์ œ๋Š” ๊ทธ๋ƒฅ ์ˆ˜ํ•™์‹์ด๋‹ค.
์ฒ˜์Œ ๋‘ ๊ฐ’์ด A, B๋‹ˆ๊นŒ ๋‘๋ฒˆ์งธ ๊ฐ’์€ A+B, ์„ธ๋ฒˆ์งธ ๊ฐ’์€ B+A+B => A+2B, ๋„ค๋ฒˆ์งธ ๊ฐ’์€ 2A+3B, ๋‹ค์„ฏ๋ฒˆ์งธ ๊ฐ’์€ 3A+5B •••

์ด ๋กœ์ง์„ dp๋ฅผ ์ด์šฉํ•ด์„œ ๊ตฌํ•˜๋ฉด ๋งˆ์ง€๋ง‰ D๋ฒˆ์งธ์— ์˜ค๋Š” ์ˆซ์ž๋“ค์ด ๊ฐ๊ฐ A์™€ B์˜ ๊ณ„์ˆ˜๊ฐ€ ๋  ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿผ ์—ฌ๊ธฐ์„œ๋ถ€ํ„ฐ A๊ฐ’์— 1๋ถ€ํ„ฐ ์ญ‰ ๋Œ€์ž…ํ•ด๋ณด๋ฉด์„œ B๊ฐ’์ด ์˜ฌ๋ฐ”๋ฅธ ์ •์ˆ˜๋กœ ๋‚˜์˜ฌ ๊ฒฝ์šฐ ๋‹ต์„ ์ถœ๋ ฅํ•˜๊ณ  break๋กœ ์ข…๋ฃŒํ•˜๋ฉด ๋!

728x90
๋ฐ˜์‘ํ˜•
LIST