-
Notifications
You must be signed in to change notification settings - Fork 2
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
7-H0ngJu #166
Conversation
#include <iostream>
#include <vector>
#include <functional>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
vector<int> memo(11, -1);
memo[0] = 0;
memo[1] = 1;
memo[2] = 2;
memo[3] = 4;
function<int(int)> dp = [&](int x) {
int &m = memo[x];
if (m != -1) {
return m;
}
return m = dp(x - 1) + dp(x - 2) + dp(x - 3);
};
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
cout << dp(n) << '\n';
}
return 0;
} κ³ μνμ ¨μ΅λλ€. μ κ·Έλ κ² λλμ§ μκ°μ ν΄λ³΄μλλ°, κ° κ²½μ°μ μλ₯Ό λμ΄ν΄λ³΄λ©΄
μ¬κΈ°κΉμ§λ μμμ μΌλ‘ ꡬν΄μΌνκ³
4 = 1 + (3) = 2 + (2) = 3 + (1) μΈλ°, μμ μ«μλ₯Ό κ³ μ νλ©΄ 4λ₯Ό λ§λλ κ²½μ°μ μλ 3μ λ§λλ + 2λ₯Ό λ§λλ + 1μ λ§λλ κ²½μ°μ μμ κ°κΈ° λλ¬Έμ dp(x) = dp(x-1) + dp(x-2) + dp(x-3) μ λλ€. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ν DPλ‘ νΈλκ² ν¨μ¬ λΉ λ₯΄κ² λ€μ..
DP λ¬Έμ μμΌλ©΄ μ νμ λ ν¬κ² μ‘μμ£Όμ§..
μ λ Nμ΄ μ΅λ 11μ΄μ΄μ λ°±νΈλνΉμΌλ‘ νμ΄λ΄€μ΄μ.
λ°λ‘ μ΄κ² λ μ¬λΌμ νμλλ° DPλ λ μ¬λ¦¬μ§λ λͺ»νλ€μ ..
import sys
def input(): return sys.stdin.readline().rstrip()
N = int(input())
def dfs(target, visited, now, now_set):
answer = 0
if target == now:
if tuple(now_set) not in visited:
visited.add(tuple(now_set))
return 1
else: return 0
for num in [1,2,3]:
if now - num <= target:
now_set.append(num)
answer += dfs(target, visited, now+num, now_set)
now_set.pop()
return answer
for _ in range(N):
target = int(input())
visited = set()
now_set = list()
print(dfs(target, visited, 0, now_set))
νμ£Όλ λ¬Έμ μ λͺ©μ΄ 1,2,3 λνκΈ°μΈλ° 1,2,3 λλκΈ°λ‘ λμ΄μμ΄μ! |
λ¬ΈλΉλκ³Ό κ°μ λ°©μμΌλ‘ νμ΄λ³΄μμ΅λλ€ ν΅μ¬μ μ λ ₯ μ«μλ€ μ€μμ μ΅λκ° νλμ λν΄μλ§ κ°μ§μλ₯Ό ꡬνκ² λ§λ€μμ΄μ μ΄ μ°μ° νλλ§ νλλΌλ λ€λ₯Έ μλ€μ λν κ°μ§μκ°
memo = {}
def find(v):
if v in memo:
return memo[v]
if v == 0:
return 1
elif v < 0:
return 0
memo[v] = find(v - 1) + find(v - 2) + find(v - 3)
return memo[v]
arr = [int(input()) for _ in range(int(input()))]
# μ΅λκ° νλμ λν΄μλ§ κ°μ§μλ₯Ό ꡬνλ€
find(max(arr))
for x in arr:
print(memo[x]) μ§κΈ λ¬Έμ μμλ μ μ½λλ‘ μμκ°μ ꡬν΄μ§λλ€. μ μ¬ν λ¬Έμ λ‘ 1λ‘ λ§λ€κΈ° μ리μ¦κ° μλλ° μ νμ΄λ³΄μ ¨λ€λ©΄ μ μ£Όν ν΄λ³΄μλκ±° μΆμ²λλ €μ κ°μΈμ μΌλ‘ μ°μ΅μ λμ λ§μ΄ λμλ λ¬Έμ μ λλ€ |
π λ¬Έμ λ§ν¬
1,2,3 λνκΈ°
βοΈ μμλ μκ°
1μκ°
β¨ μλ μ½λ
μ λ DPλ₯Ό λ¨Όμ 곡λΆνκ³ , μ΄ λ¬Έμ λ₯Ό νμμ΅λλ€.
DPλ 'μμμ κ³μ°ν μμ λ°°μ΄μ 미리 μ μ₯ν΄λμ΄μ μ°μ°μλλ₯Ό μ¦κ°μν€λ νλ‘κ·Έλλ°'μ λλ€.
κ·Έλμ DPμ ν΅μ¬μ ν λ² κ³μ°ν 건 λ€μ κ³μ°νμ§ μλ κ²μ΄ μ€μν©λλ€.
μ΄ μ μ μκ°νκ³ λ¬Έμ λ₯Ό λ°λΌλ³΄λ©΄, 1,2,3μΌλ‘ λνλΌ μ μλ λ°©λ²μ μ f(n)μ κ³μ°ν λ,
μ΄λ»κ² νλ©΄ μ΄μ μ κ³μ°νλ κ°μ ν΅ν΄ f(n)μ ꡬν μ μλκ°? μ λν ν΄λ΅μ μ°ΎμΌλ©΄ μ΄ λ¬Έμ λ₯Ό ν΄κ²°ν μ μμ΅λλ€.
λͺ λ² λλμ΄μ§λκ°, 1,2,3μΌλ‘ λͺ λ² κ³±ν΄μ nμ΄ λλκ° μͺ½μΌλ‘ μκ°μ νλ€κ°
λ¨μνκ² μ νμμ μμ±ν΄λ³΄μλλ,
κ²°κ΅ f(n)μ f(n-1) + f(n-2) + f(n-3)ν κ°κ³Ό λμΌνλ€λ κ²°λ‘ μ΄ λμκ³ κ·Έλλ‘ μ½λλ‘ μμ±νμμ΅λλ€.
π μλ‘κ² μκ²λ λ΄μ©