본문 바로가기
백준

백준 동적계획법 - 1003번 : 피보나치 함수

by 맴썰 2022. 2. 16.

https://www.acmicpc.net/problem/1003

 

1003번: 피보나치 함수

각 테스트 케이스마다 0이 출력되는 횟수와 1이 출력되는 횟수를 공백으로 구분해서 출력한다.

www.acmicpc.net


import java.io.*;
import java.util.*;


public class Main {
	static int[] fivoarr;
	static int[] fivocnt0;
	static int[] fivocnt1;
	public static void main(String[] args) throws IOException{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		st = new StringTokenizer(br.readLine());
		int n = Integer.parseInt(st.nextToken());
		
		for(int i=0; i<n; i++) {
			fivoarr = new int[41];
			fivocnt0 = new int[41];
			fivocnt1 = new int[41];
			fivoarr[0] = 0;
			fivoarr[1] = 1;
			fivocnt0[0] = 1;
			fivocnt1[1] = 1;
			st = new StringTokenizer(br.readLine());
			int num = Integer.parseInt(st.nextToken());
			fivo(num);
			System.out.println(fivocnt0[num] + " " + fivocnt1[num]);
		}
	}
	static int fivo(int a) {
		if(a == 0) {return 0;}
		if(a == 1) {return 1;}
		if(fivoarr[a]==0) {
			fivoarr[a] = fivo(a-2)+fivo(a-1);
			fivocnt0[a] = fivocnt0[a-2]+fivocnt0[a-1];
			fivocnt1[a] = fivocnt1[a-2]+fivocnt1[a-1];
			return fivoarr[a];
		} 
		return fivoarr[a];
	}
}