Pseudo-code to find the nth fibonacci number : int fibo(int n) { if (n == 0) { write(0)…
- User
Pseudo-code to find the nth fibonacci number :
int fibo(int n) { if (n == 0) { write(0) return 0 } if (n == 1) { write(1) return 1 } return fibo(n - 1) + fibo(n - 2) } If we call fibo(3), the following happens:
fibo(3) calls fibo(2) and fibo(1) ---->the first call.
fibo(2) calls fibo(1) ---->the second call and fibo(0).
The second call of fibo(1) writes 1 and returns 1.
fibo(0) writes 0 and returns 0.
fibo(2) gets the results of fibo(1) and fibo(0) and returns 1.
The first call of fibo(1) writes 1 and returns 1.
fibo(3) gets the results of fibo(2) and fibo(1) and returns 2.
All in all, 1 will be written twice and 0 will be written once. Now find how many times 0 and 1 will be written for a given integer N. INPUT
The first line contains an integer T, denoting the number of test cases. The next T lines contain an integer N.
OUTPUT
For each test case, print the output in one line which consists of 2 space separated integers. The first integer denotes the number of times 0 gets printed , while the second integer denotes the number of times 1 gets printed.
CONSTRAINTS
1 ≤ T ≤ 50
0 ≤ N ≤ 40
SAMPLE INPUT 2 1 2
SAMPLE OUTPUT 0 1 1 1
Assistant
python# Precompute the number of zeros and ones for each n from 0 to 40 zeros = [0] * 41 ones = [0] * 41 zeros[0] = 1 ones[1] = 1 for i in range(2, 41): zeros[i] = zeros[i-1] + zeros[i-2] ones[i] = ones[i-1] + ones[i-2] # Read input and output results T = int(input()) for _ in range(T): N = int(input()) print(zeros[N], ones[N])
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.