728x90

dp를 이용하려고 했으나 방법을 몰랐고, product 를 이용했다. 사실 무조건 메모리 초과나 시간 초과가 나는데 숫자가 작아서 성공했다.

import sys
from collections import deque
from itertools import product
 
 
num = int(input())
 
def check(n): 
 
    '''
    # 가능한 1,2,3 개수 찾아서 permutations 돌리기
 
    three = n//3
 
    n = n-(three*3)
 
    two = n//2
 
    n = n-(n//2)
 
    one = n
 
    '''
    num = [1,2,3]
 
    answer = 0
 
    for i in range(1,n+1):
        k = list(product(num, repeat = i))
 
        for i in range(0,len(k)):
            if sum(k[i]) == n:
                answer+=1
 
 
    return answer
 
 
for i in range(num):
    k = int(input())
    print(check(k))
 
 
728x90

+ Recent posts