Adding Ones And Twos

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 1 + 1 + 1, 1 + 2, 2 + 1 .


The answer is 253.

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.

3 solutions

Sharky Kesa
Jun 27, 2016

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 f_n . We have f 1 = 1 f_1=1 and f 2 = 2 f_2=2 . Note how f n = f n 1 + f n 2 f_n=f_{n-1}+f_{n-2} because all sums adding to n n are either all sums adding to n 2 n-2 , with an additional '2' added at the end of each sum, or are either all sums adding to n 1 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 f_n=f_{n-1}+f_{n-2},\ \quad f_1=1,\ \quad f_2=2

From this, we can create a python script to determine f 666 ( m o d 1000 ) f_{666} \pmod{1000} :

1
2
3
4
5
6
def f(n, computed={1: 1, 2: 2}):
    if n not in computed:
        computed[n] = (f(n-1, computed) + f(n-2, computed))%1000
    return computed[n]

print(f(666))

This outputs 253, which is the answer we desire.

BTW, the above script works really well with large inputs, hence why I used it.

Kunal Gupta
Jul 2, 2016

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 666 666 1's and 333 333 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 k 2's we see that there are 666 2 k 666-2k 1's and in total 666 k 666-k terms Using the stars and bars technique, we see that possible arrangements for a particular number of 2's is: ( 666 k ) ! k ! ( 666 2 k ) ! \frac{(666-k)!}{k!(666-2k)!} Hence total arrangements are: k = 0 333 ( 666 k ) ! k ! ( 666 2 k ) ! \displaystyle \sum_{k=0}^{333}\frac{(666-k)!}{k!(666-2k)!} 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: 11098672708526908600764889580794591185766740194460441455645037457990760955740573933325912682002282764906185274340684734874403234129028461253 11098672708526908600764889580794591185766740194460441455645037457990760955740573933325912682002282764906185274340684734874403234129028461253 The last 3 digits are: 253 253

The problem of sum overflow can be avoided easily by taking sum of only last three digits (i.e. modulo 100)

Rushikesh Jogdand - 4 years, 11 months ago
Rushikesh Jogdand
Jul 10, 2016
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
def f(n):
    p=1
    for i in range(1,n+1):
        p*=i
    return p
s=0
for num2 in range(0,334):
    s+=int(int(f(666-num2)//f(666-2*num2))//f(num2))%1000
    #use integer division to avoid OverflowError
print(s%1000)

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...