Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

16-Redish03 #68

Merged
merged 3 commits into from
Mar 5, 2024
Merged

16-Redish03 #68

merged 3 commits into from
Mar 5, 2024

Conversation

Redish03
Copy link
Collaborator

@Redish03 Redish03 commented Feb 27, 2024

πŸ”— 문제 링크

μ‰¬μš΄ 계단 수

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

1h

✨ μˆ˜λ„ μ½”λ“œ

β“λ¬Έμ œ 이해

  • 45656은 μΈμ ‘ν•œ λͺ¨λ“  자리의 차이가 1이닀. 이런 수λ₯Ό 계단 수라고 ν•œλ‹€.
  • N이 μ£Όμ–΄μ§ˆ λ•Œ, 길이가 N인 계단 μˆ˜κ°€ λͺ‡ 개 μžˆλŠ”μ§€ κ΅¬ν•΄λ³΄μž. 0으둜 μ‹œμž‘ν•˜λŠ” μˆ˜λŠ” 계단 μˆ˜κ°€ μ•„λ‹ˆλ‹€.

β—λ¬Έμ œ μ ‘κ·Ό

  • μ²˜μŒμ—” κ°€μž₯ κ°„λ‹¨ν•œ λ°©λ²•μœΌλ‘œ μž¬κ·€ ν•¨μˆ˜λ₯Ό μ§°λ‹€.
  • λ§Œμ•½μ— μ΄μ „μ˜ μžλ¦¬κ°€ 0이면, λ§Œλ“€μ–΄μ§ˆ 수 μžˆλŠ” 계단 μˆ˜λŠ” ν•˜λ‚˜μ΄λ‹€. (ex. XX0이면, XX01 만 κ°€λŠ₯ν•˜λ‹€.)
  • λ§ˆμ°¬κ°€μ§€λ‘œ. μ΄μ „μ˜ μžλ¦¬κ°€ 9이면 λ§Œλ“€μ–΄μ§ˆ 수 μžˆλŠ” 계단 μˆ˜λŠ” ν•˜λ‚˜μ΄λ‹€. (ex. XX9이면, XX98만 κ°€λŠ₯ν•˜λ‹€.)
  • 0κ³Ό 9μ™Έμ˜ μˆ«μžλ“€μ€ λ‹€μŒμ˜ 계단 수λ₯Ό 2개 κ°€μ§ˆ 수 μžˆλ‹€. 예λ₯Ό λ“€μ–΄ μ΄μ „μ˜ μžλ¦Ώμˆ˜κ°€ XX3이면 XX2와 XX4κ°€ λ§Œλ“€μ–΄μ§ˆ 수 μžˆλ‹€.
  • 이런 뢀뢄을 μ΄μš©ν•΄ μž¬κ·€ ν•¨μˆ˜λ₯Ό μ§°λ‹€.
#include <iostream>

using namespace std;

int N;

int number_cnt = 0;
int num_cnt_arr[] = {1, 2, 2, 2, 2, 2, 2, 2, 2, 1};

void dfs(int count, int prev_num)
{
    if (count == N || prev_num < 0 || prev_num > 9)
    {
        return;
    }

    number_cnt += num_cnt_arr[prev_num];
    dfs(count + 1, prev_num - 1);
    dfs(count + 1, prev_num + 1);
}

int main()
{
    cin >> N;
    if (N == 1)
    {
        cout << 9;
        return 0;
    }
    for (int i = 1; i <= 9; i++)
    {
        dfs(1, i);
    }

    cout << number_cnt;
}
  • μ‹œκ°„μ΄ˆκ³Όκ°€ λ–΄λ‹€. γ…Žγ…Ž
  • λΉ λ₯΄κ²Œ 포기

2️⃣ 2트

  • 0μ΄λž‘ 9, 그리고 λ‚˜λ¨Έμ§€ 수둜 λ‚˜λˆ„λŠ” 기본적인 접근은 κ°™λ‹€.

  • κ·ΈλŸ¬λ‚˜ DP기법을 μ μš©ν•œλ‹€. 이 λ•Œ, dp λ°°μ—΄μ—λŠ” κΉŠμ΄μ™€ 맨 끝 μˆ˜μ— λ”°λ₯Έ 계단 수의 개수λ₯Ό μ €μž₯ν•œλ‹€. 즉,
    dp[길이 (N)][맨 λ§ˆμ§€λ§‰ 수(M)] = 길이가 N이고 맨 λ§ˆμ§€λ§‰ μˆ˜κ°€ M 일 λ•Œμ˜ 계단 수의 개수
    κ°€ λœλ‹€.

  • μ˜ˆμ‹œλ₯Ό ν†΅ν•΄μ„œ 이해해 보자. N = 2라면,

  • κ°€μž₯ λ¨Όμ € N = 2λ₯Ό 보기 전에 N = 1일 λ•Œλ₯Ό λ¨Όμ € 생각해야 ν•œλ‹€.

    • N = 1이면, {0,1,2,3,4,5,6,7,8,9}λŠ” 0을 μ œμ™Έν•˜κ³  λͺ¨λ‘ 1개의 계단 μˆ˜μ΄λ‹€. (예제 μ—μ„œ, N = 1이면 좜λ ₯은 9이닀.)
    • 그럼 dp[1][각 자리수] = 1이 될 것이닀.
  • N = 2 일 λ•Œλ₯Ό 보자.

    • 맨 끝 μˆ«μžκ°€ 0이라면, μœ„μ˜ 첫 번째 μ‹œλ„ 처럼 μ§μ „μ˜ μžλ¦¬κ°€ 1μ΄μ—¬μ•Όλ§Œ 개수λ₯Ό μΆ”κ°€ ν•  수 μžˆλ‹€.
      • 즉, 10만 κ°€λŠ₯ν•˜λ‹€. μ΄λŠ” N의 κ°œμˆ˜κ°€ λŠ˜μ–΄λ‚˜λ„ λ§ˆμ°¬κ°€μ§€μ΄λ‹€. N = 3이라도 μ΄μ „μ˜ μˆ˜κ°€ 1이라면, _10만 κ°€λŠ₯ν•˜κΈ° λ•Œλ¬Έμ—, κ°œμˆ˜λŠ” dp[N - 1][1] 의 κ°œμˆ˜μ™€ κ°™λ‹€!
    • μ΄λŠ” 맨 끝 μˆ«μžκ°€ 9일 λ•Œ λ˜ν•œ 적용 κ°€λŠ₯ν•˜λ‹€.
      • κ°€μž₯ 졜근의 μˆ˜κ°€ 8일 λ•Œλ§Œ κ°€λŠ₯ν•˜κΈ° λ•Œλ¬Έμ—, μœ„μ™€ λ§ˆμ°¬κ°€μ§€λ‘œ
      • dp[N][9] = dp[N - 1][8]κ³Ό κ°™λ‹€.!
    • 그럼 0κ³Ό 9κ°€ 아닐 λ•ŒλŠ” μ–΄λ–»κ²Œ ν•΄μ•Όν• κΉŒ.
      • 계단 μˆ˜λŠ” 1차이가 λ‚˜μ•Όν•˜λ―€λ‘œ, 이전 μˆ˜κ°€ ν˜„μž¬μ˜ μˆ˜λ³΄λ‹€ 1만큼 크고 1만큼 μž‘μ€ 수의 경우의 수λ₯Ό 더해야 ν•œλ‹€.
      • 예λ₯Ό λ“€μ–΄μ„œ N = 2, M = 3이라면, 3은 2와 4κ°€ 1 차이 λ‚˜λ―€λ‘œ, 이전 길이 (N - 1)에 이전 μˆ˜κ°€ 2와 4인 경우의 수λ₯Ό 더해쀀닀.
      • dp[N][M] = dp[N - 1][M - 1] + dp[N - 1][M + 1]둜 ν‘œν˜„ ν•  수 μžˆλ‹€.

μœ„ λ‘œμ§μ„ μ•„λž˜μ™€ 같이 ν‘œν˜„ ν•  수 μžˆλ‹€.

핡심 둜직

    for (int i = 2; i <= N; i++)
    {
        for (int j = 0; j <= 9; j++)
        {
            if (j == 0)
            {
                dp[i][j] = dp[i - 1][1];
            }
            else if (j == 9)
            {
                dp[i][j] = dp[i - 1][8];
            }
            else
            {
                dp[i][j] = (dp[i - 1][j - 1] + dp[i - 1][j + 1]);
            }
            dp[i][j] %= DVD;
        }
    }

전체 μ½”λ“œ

#include <iostream>
#define DVD 1000000000

using namespace std;

int N;

long long number_cnt = 0;
long long dp[101][10] = {
    0,
};

int main()
{
    cin >> N;
    // N = 1일 λ•Œ, dp λ°°μ—΄ μ΄ˆκΈ°ν™” ꡬ문 
    for (int i = 1; i < 10; i++)
    {
        dp[1][i] = 1;
    }
    for (int i = 2; i <= N; i++)
    {
        for (int j = 0; j <= 9; j++)
        {
            if (j == 0)
            {
                dp[i][j] = dp[i - 1][1];
            }
            else if (j == 9)
            {
                dp[i][j] = dp[i - 1][8];
            }
            else
            {
                dp[i][j] = (dp[i - 1][j - 1] + dp[i - 1][j + 1]);
            }
            dp[i][j] %= DVD;
        }
    }

    for (int i = 0; i < 10; i++)
    {
        number_cnt += dp[N][i];
        number_cnt %= DVD;
    }

    cout << number_cnt;
}

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

μ‰¬λ‹ˆκΉŒ ν•˜λ‚˜λ„ λͺ¨λ₯΄κ²Ÿλ‹Ή γ…Žγ…Ž

@9kyo-hwang
Copy link

image

γ…‹γ…‹γ…‹γ…‹γ…‹γ…‹

Copy link
Member

@miniron-v miniron-v left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ•„ 이 λ¬Έμ œλ„ μž¬λ°Œμ—ˆλŠ”λ°...γ…‹γ…‹γ…‹γ…‹ κ³¨λ“œ 계단 수 문제 ν’€λ €λ‹€κ°€ λ¨Όμ € ν’€μ—ˆλ˜ λ¬Έμ œμ˜€μ£ .

μ΄λ ‡κ²Œ ν’€λ©΄ λ©”λͺ¨λ¦¬λ„ κ·Ήν•œμœΌλ‘œ μ•„λ‚„ 수 μžˆλ‹΅λ‹ˆλ‹€~~~

#include <iostream>
#include <vector>

#define MAX 1000000000

int main() {
	int n;
	std::cin >> n;

	int grid[2][12] = { 0 };

	for (int i = 2; i <= 10; i++) {
		grid[0][i] = 1;
	}

	int k = 0;
	for (int i = 2; i <= n; i++) {
		k = (k == 0);
		for (int j = 1; j <= 10; j++) {
			grid[k][j] = (grid[!k][j - 1] + grid[!k][j + 1]) % MAX;
		}
	}

	int answer = 0;
	for (int i = 1; i <= 10; i++) {
		answer += grid[k][i];
		answer %= MAX;
	}
	std::cout << answer;
}

@Redish03
Copy link
Collaborator Author

μ•„ 이 λ¬Έμ œλ„ μž¬λ°Œμ—ˆλŠ”λ°...γ…‹γ…‹γ…‹γ…‹ κ³¨λ“œ 계단 수 문제 ν’€λ €λ‹€κ°€ λ¨Όμ € ν’€μ—ˆλ˜ λ¬Έμ œμ˜€μ£ .

μ΄λ ‡κ²Œ ν’€λ©΄ λ©”λͺ¨λ¦¬λ„ κ·Ήν•œμœΌλ‘œ μ•„λ‚„ 수 μžˆλ‹΅λ‹ˆλ‹€~~~

#include <iostream>
#include <vector>

#define MAX 1000000000

int main() {
	int n;
	std::cin >> n;

	int grid[2][12] = { 0 };

	for (int i = 2; i <= 10; i++) {
		grid[0][i] = 1;
	}

	int k = 0;
	for (int i = 2; i <= n; i++) {
		k = (k == 0);
		for (int j = 1; j <= 10; j++) {
			grid[k][j] = (grid[!k][j - 1] + grid[!k][j + 1]) % MAX;
		}
	}

	int answer = 0;
	for (int i = 1; i <= 10; i++) {
		answer += grid[k][i];
		answer %= MAX;
	}
	std::cout << answer;
}

λ„λŒ€μ²΄ μ–Όλ§ˆλ‚˜ μ•„λΌλŠ”κ±°μ•Ό...!!γ…‹γ…‹γ…‹γ…‹γ…‹

@miniron-v
Copy link
Member

λ„λŒ€μ²΄ μ–Όλ§ˆλ‚˜ μ•„λΌλŠ”κ±°μ•Ό...!!γ…‹γ…‹γ…‹γ…‹γ…‹

애끼고 살아야지...!

Copy link
Member

@bomik0221 bomik0221 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ˜€λžœλ§Œμ— ν•˜λ©΄ μ›λž˜ 잘 ν•˜λ˜κ²ƒλ„ λ­”κ°€ μ• λ§€ν•˜κΈ° 마련이죠.. κ³ μƒν•˜μ…¨μŠ΅λ‹ˆλ‹€!

@@ -0,0 +1,47 @@
#include <iostream>
#define DVD 1000000000
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ν˜Ήμ‹œ DVDμ—λŠ” 뜻이 λ”°λ‘œ μžˆλ‚˜μš”???

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ•„λ§ˆ Dividend(ν”Όμ œμˆ˜, λ‚˜λˆ”λ‹Ήν•˜λŠ” 수)λ₯Ό μ˜λ―Έν•΄μ„œ DVD라 지은 게 μ•„λ‹κΉŒ... μ‹Άλ„€μš”

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

divide λ§žμŠ΅λ‹ˆλ‹€γ…
ㅏ

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

divideμ˜€κ΅°!

Copy link
Collaborator

@2secondag 2secondag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ½”λ“œ 잘 λ΄€μŠ΅λ‹ˆλ‹€ 문제 μ΄ν•΄ν•˜λŠ” 것도 μ˜€λž˜κ±Έλ Έλ„€μš©.... μ§„μ§œ dpλŠ” μ–΄λ ΅λ‹€ γ…Žγ…Žγ…Ž,,,,
μˆ˜κ³ ν•˜μ…¨μŠ΅λ‹ˆλ‹€~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants