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

1351: 무한 수열 #4

Open
minsoo0715 opened this issue Sep 3, 2023 · 0 comments
Open

1351: 무한 수열 #4

minsoo0715 opened this issue Sep 3, 2023 · 0 comments
Assignees
Labels
#다이나믹 프로그래밍 동적계획법 문제 @백준 백준(https://www.acmicpc.net) 문제 C/C++ c/c++로 해결한 문제들

Comments

@minsoo0715
Copy link
Owner

minsoo0715 commented Sep 3, 2023

1351 : 무한 수열

소스 코드

아이디어

메모이제이션 문제로 보인다.
대신 수열의 형태가 아래와 같으므로 $N$이 급격하게 줄어드는 경향이 있어 저장할 $A_i$가 띄엄띄엄 존재할 뿐만 아니라, 값의 범위가 long을 초과하기 때문에 배열로 저장하기에는 무리가 있다.
$A_i = A_{i/P} + A_{i/Q} (i ≥ 1)$

따라서 Key-Value로 저장하는 자료구조, map을 사용하면 될 것으로 보인다.

구현

std::vector대신 std::map을 통해 메모이제이션, 재귀 함수를 사용해 top-down 방식으로 구현

#include <iostream>
#include <map>

using ll = long long int;

std::map<ll, ll> dp;
ll p, q;

ll solve(ll n) {
    if(n == 0) {
        return 1;
    }
    if(dp[n] != 0) {
        return dp[n];
    }
    return dp[n] = solve(n/p) + solve(n/q);
}
@minsoo0715 minsoo0715 added @백준 백준(https://www.acmicpc.net) 문제 C/C++ c/c++로 해결한 문제들 #다이나믹 프로그래밍 동적계획법 문제 labels Sep 3, 2023
@minsoo0715 minsoo0715 self-assigned this Sep 3, 2023
@minsoo0715 minsoo0715 added @백준 백준(https://www.acmicpc.net) 문제 C/C++ c/c++로 해결한 문제들 and removed @백준 백준(https://www.acmicpc.net) 문제 C/C++ c/c++로 해결한 문제들 labels Sep 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
#다이나믹 프로그래밍 동적계획법 문제 @백준 백준(https://www.acmicpc.net) 문제 C/C++ c/c++로 해결한 문제들
Projects
None yet
Development

No branches or pull requests

1 participant