In how many ordered ways can you write the number 666 as a sum of 1's and 2's?
Submit your answer as the last three digits of the result.
As an explicit example, there are 3 ways to write the number 3 as a sum of 1's and 2's, namely: 1 + 1 + 1 , 1 + 2 , 2 + 1 .
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.
I didn't realise the beauty of Fibonacci related recursion this problem had! I did it this way: For any arrangement of 2's and 1's, there can be a max of 6 6 6 1's and 3 3 3 2's. Hence, every possible combination is the involvement of these 1's and 2's. For every 2 we write, we remove 2 1's from the sequence In a sequence with k 2's we see that there are 6 6 6 − 2 k 1's and in total 6 6 6 − k terms Using the stars and bars technique, we see that possible arrangements for a particular number of 2's is: k ! ( 6 6 6 − 2 k ) ! ( 6 6 6 − k ) ! Hence total arrangements are: k = 0 ∑ 3 3 3 k ! ( 6 6 6 − 2 k ) ! ( 6 6 6 − k ) ! Summing it is clearly out of range using common procedure in most architectures. Hence, by using the method of storing the values in an array, it can be easily computed with the answer being: 1 1 0 9 8 6 7 2 7 0 8 5 2 6 9 0 8 6 0 0 7 6 4 8 8 9 5 8 0 7 9 4 5 9 1 1 8 5 7 6 6 7 4 0 1 9 4 4 6 0 4 4 1 4 5 5 6 4 5 0 3 7 4 5 7 9 9 0 7 6 0 9 5 5 7 4 0 5 7 3 9 3 3 3 2 5 9 1 2 6 8 2 0 0 2 2 8 2 7 6 4 9 0 6 1 8 5 2 7 4 3 4 0 6 8 4 7 3 4 8 7 4 4 0 3 2 3 4 1 2 9 0 2 8 4 6 1 2 5 3 The last 3 digits are: 2 5 3
The problem of sum overflow can be avoided easily by taking sum of only last three digits (i.e. modulo 100)
1 2 3 4 5 6 7 8 9 10 |
|
Problem Loading...
Note Loading...
Set Loading...
Relevant wiki: Computational problems solved through Mathematical theory
We can construct a recursive statement for the number of ordered sums that can be constructed. Let this sequence be f n . We have f 1 = 1 and f 2 = 2 . Note how f n = f n − 1 + f n − 2 because all sums adding to n are either all sums adding to n − 2 , with an additional '2' added at the end of each sum, or are either all sums adding to n − 1 , with an additional '1' added at the end of each sum. This makes all the sums of different order. Thus, we have constructed a recursive function as follows:
f n = f n − 1 + f n − 2 , f 1 = 1 , f 2 = 2
From this, we can create a python script to determine f 6 6 6 ( m o d 1 0 0 0 ) :
This outputs 253, which is the answer we desire.
BTW, the above script works really well with large inputs, hence why I used it.