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

๐˜š๐˜ญ๐˜ฐ๐˜ธ ๐˜ฃ๐˜ถ๐˜ต ๐˜ด๐˜ต๐˜ฆ๐˜ข๐˜ฅ๐˜บ

[๋ฐฑ์ค€] 1309๋ฒˆ: ๋™๋ฌผ์› - C++ ๋ณธ๋ฌธ

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์ค€๋น„/๋ฐฑ์ค€

[๋ฐฑ์ค€] 1309๋ฒˆ: ๋™๋ฌผ์› - C++

.23 2022. 7. 26. 12:56
๋ฌธ์ œ

์–ด๋–ค ๋™๋ฌผ์›์— ๊ฐ€๋กœ๋กœ ๋‘์นธ ์„ธ๋กœ๋กœ N์นธ์ธ ์•„๋ž˜์™€ ๊ฐ™์€ ์šฐ๋ฆฌ๊ฐ€ ์žˆ๋‹ค.

์ด ๋™๋ฌผ์›์—๋Š” ์‚ฌ์ž๋“ค์ด ์‚ด๊ณ  ์žˆ๋Š”๋ฐ ์‚ฌ์ž๋“ค์„ ์šฐ๋ฆฌ์— ๊ฐ€๋‘˜ ๋•Œ, ๊ฐ€๋กœ๋กœ๋„ ์„ธ๋กœ๋กœ๋„ ๋ถ™์–ด ์žˆ๊ฒŒ ๋ฐฐ์น˜ํ•  ์ˆ˜๋Š” ์—†๋‹ค. ์ด ๋™๋ฌผ์› ์กฐ๋ จ์‚ฌ๋Š” ์‚ฌ์ž๋“ค์˜ ๋ฐฐ์น˜ ๋ฌธ์ œ ๋•Œ๋ฌธ์— ๊ณจ๋จธ๋ฆฌ๋ฅผ ์•“๊ณ  ์žˆ๋‹ค.

๋™๋ฌผ์› ์กฐ๋ จ์‚ฌ์˜ ๋จธ๋ฆฌ๊ฐ€ ์•„ํ”„์ง€ ์•Š๋„๋ก ์šฐ๋ฆฌ๊ฐ€ 2*N ๋ฐฐ์—ด์— ์‚ฌ์ž๋ฅผ ๋ฐฐ์น˜ํ•˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ๋ช‡ ๊ฐ€์ง€์ธ์ง€๋ฅผ ์•Œ์•„๋‚ด๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ด ์ฃผ๋„๋ก ํ•˜์ž. ์‚ฌ์ž๋ฅผ ํ•œ ๋งˆ๋ฆฌ๋„ ๋ฐฐ์น˜ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๋„ ํ•˜๋‚˜์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜๋กœ ์นœ๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค.

 

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์šฐ๋ฆฌ์˜ ํฌ๊ธฐ N(1≤N≤100,000)์ด ์ฃผ์–ด์ง„๋‹ค.

 

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ์‚ฌ์ž๋ฅผ ๋ฐฐ์น˜ํ•˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ 9901๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋ฅผ ์ถœ๋ ฅํ•˜์—ฌ๋ผ.


๋™์ ๊ณ„ํš๋ฒ•์„ ์ด์šฉํ•œ ๋ฌธ์ œ

 

์‹์„ ๋•Œ๋ ค๋งž์ถฐ์„œ ํ’€์–ด๋ฒ„๋ ค์„œ

๋‚ด๊ฐ€ ์ƒ๊ฐํ•œ ํ’€์ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค...

i - 1๋ฒˆ์งธ ์ค„์ด X X์ผ๋•Œ

i๋ฒˆ์งธ ์ค„์— ์‚ฌ์ž๋ฅผ ๋‘˜ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ๋Š” X X, X O, O X์˜ ์„ธ๊ฐ€์ง€์ด๊ณ 

i - 1๋ฒˆ์งธ ์ค„์ด O X ๋˜๋Š” X O์ผ๋•Œ

i๋ฒˆ์งธ ์ค„์— ์‚ฌ์ž๋ฅผ ๋‘˜ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ๋Š” ๊ฐ๊ฐ X X, X O ๋˜๋Š” X X, O X ์ด๋‹ค.

 

๊ทธ๋ž˜์„œ i๋ฅผ 1๋ถ€ํ„ฐ ๋Š˜์—ฌ๋œจ๋ ค์„œ ์ •๋ฆฌํ–ˆ์„ ๋•Œ

 

lion[1] = 3

lion[2] = 1 * 3 + 2 * 2 = 7

lion[3] = 3 * 3 + 4 * 2 = 17

lion[4] = 7 * 3 + 10 * 2 = 41

.

.

 

์–ด๋– ํ•œ ๊ทœ์น™์„ ์ฐพ์•˜๊ณ , ๊ทธ๋ฆฌํ•˜์—ฌ ์‹์„ ์ •๋ฆฌํ–ˆ๋”๋‹ˆ ๋‹ค์Œ๊ณผ ๊ฐ™์•˜๋‹ค.

lion[i] = lion[i - 2] * 3 + (lion[i - 1] - lion[i - 2]) * 2

์œ„์˜ ์‹์„ ์ •๋ฆฌํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜์˜จ๋‹ค

lion[i] = lion[i - 2] + lion[i - 1] * 2

 

์ฝ”๋“œ
#include <cstdio>
#define MAX 100001
#define MOD 9901

int DP(int num);
int lion[MAX];

int main(void) {
    int N;
    scanf("%d", &N);

    printf("%d\n", DP(N));
    return 0;
}

int DP(int num) {
    lion[0] = 1;
    lion[1] = 3;
    
    for(int i = 2; i <= num; i++) {
        lion[i] = (lion[i - 2] + lion[i - 1] * 2) % MOD;
    }

    return lion[num];
}
Comments