Let f ( n ) = { 4 − n f ( n − 1 ) + f ( n − 2 ) + f ( n − 3 ) if $n$ ≤ 3 if $n$ > 3
What are the last 3 digits of f ( 1 0 1 2 ) ?
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Nice Solution
The idea for calculating big fibonacci numbers is using the matrix formula. (As seen in http://mathworld.wolfram.com/FibonacciQ-Matrix.html)
So we are going to try that same argument. Basically you want some matrix T so that M n ∗ T = M n + 1 where M n − 4 = ⎝ ⎛ f ( n − 2 ) f ( n − 3 ) f ( n − 4 ) f ( n − 1 ) f ( n − 2 ) f ( n − 3 ) f ( n ) f ( n − 1 ) f ( n − 2 ) ⎠ ⎞
It is not hard to see that
T = ⎝ ⎛ 0 1 0 0 0 1 1 1 1 ⎠ ⎞
works. (It is like translating the second column to the first, the third to the second and the sum to the third).
By induction, M n = M 0 × T n .
For convenience, define F(0) = -4, and compute F(4)=3+2+1=6 then
M 0 = ⎝ ⎛ 2 3 − 4 1 2 3 6 1 2 ⎠ ⎞
So we can compute M 1 0 1 2 = M 0 × T 1 0 1 2 .Because we are only interested in the last 3 digits, we take the product always modulo 1000. (Or in Z / 1 0 0 0 Z if you are fancy). The nice thing is that linear functions are still linear modulo 1000, so the computation does not breaks.
And to actually calculate M 0 × T q with q = 1 0 1 2 you use fast exponentiation (ie. recursively T 2 n = ( T n ) 2 and T 2 n + 1 = ( T n ) 2 ∗ T , all in matrix multiplications). Then you multiply M 0 × ( T q ) and so in the lowerleft corner is f ( 1 0 1 2 ) .
As for the actual code, that is left as an exercise to the reader. (Ruby has a gem that already has Matrix implemented, I think)
At first, I tried the naive approach, by writing a program that calculated all the Tribonacci numbers (mod 1000) one by one. (I used Matlab.) However, I realized that this would take like 8-10 hours for my Lenovo intel Core i5--to be honest I actually tried it but decided to stop it when the program was 71% done because I knew there was a better way XD. So, instead, I calculated the first one million Tribonacci numbers, putting them into an array, then identified a number sequence with length four at random (I figured length four would be sufficient). However, I later realized that three is sufficient. Then I wrote a simple code which recorded all the appearances of this number sequence into an array. It was not to my surprise that these appearances were equally spaced as in the Fibonacci sequence (the same four numbers must occur twice in sequence by the pigeonhole principle, and the same pattern must follow each time because we are following the same rule). The spacing between each occurrence was 12400. So, all I needed to do next was to type 10^12 (mod 12400) into Matlab, which gave me an answer of 3600. So, the 10^12th term mod 1000 was the same as the 3600th term mod 1000. The value of the 3600th term mod 1000 was 516. Hence, the answer is 516.
Problem Loading...
Note Loading...
Set Loading...
After a first glance at the problem,I assumed that answer could be acquired with a simple variation of the DP Algorithm for finding the n t h Fibonacci number.The best know Time Complexity of that algorithm is O ( n ) .
But after a second look at the large input of the problem,I realized that my naive approach would never work.Even though it was O ( n ) , it would amount to 1 0 1 2 computations. I figured there must be some sort of pattern that would cut the run-time so I searched for a cycle in the last three digits of the Tribonacci sequence and lo and behold! I found that the last three digits of the Tribonacci sequence repeated on a cycle of length 1 2 4 0 0 .
That reduced the number of computations necessary to 1 0 1 2 mod 1 2 4 0 0 . Hence the answer was just a matter of finding the last three digits of f ( 1 0 1 2 m o d 1 2 4 0 0 ) = f ( 3 6 0 0 ) = 5 1 6 .
The code for finding f ( n )
This finds the length of the cycle
This prints out the final answer 516 using the above code