Problem 290
Tribonacci Number
Easy · 10 pts · Math, Dynamic Programming
Problem
The Tribonacci sequence is like Fibonacci, but each term is the sum of the three terms before it. It starts with T(0) = 0, T(1) = 1 and T(2) = 1, and then T(n) = T(n - 1) + T(n - 2) + T(n - 3). The first terms are 0, 1, 1, 2, 4, 7, 13, 24, ... Given N, print T(N). A loop that keeps only the last three values computes this quickly. The values grow fast, so use a 64-bit integer; T(70) still fits.
Input
A single integer N.
Output
Print T(N).
Constraints
0 <= N <= 70 (the answer fits in a 64-bit integer)
Example input
4
Example output
4
Explanation
T(3) = T(2) + T(1) + T(0) = 1 + 1 + 0 = 2, and T(4) = T(3) + T(2) + T(1) = 2 + 1 + 1 = 4. So for N = 4 the answer is 4.