Let S be the sum of the positive integers below 1 0 2 0 which are divisible by 3 or 11.
What is S m o d 1 0 0 0 0 0 0 0 0 7 ?
Details and Assumptions
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.
Great solution!
I used windows scientifc calculator to calc n/66(13n+33) and mod 1000000007
Easy way to solve it is by doing this (pseudocode) -
1 2 3 4 |
|
However, the code cannot be executed in a feasible amount of time due to the size of the search. A much better way would be to use some knowledge of Number Theory and Combinatorics.
Sum of multiples of 3 can be written as
3 + 6 + 9 … ( 1 0 2 0 − 4 ) + ( 1 0 2 0 − 1 ) = 3 ( 1 + 2 + 3 … 3 ( 1 0 2 0 − 4 ) + 3 ( 1 0 2 0 − 1 ) )
Similarly, sum of multiples of 11 can be written as
1 1 + 2 2 + 3 3 … ( 1 0 2 0 − 1 2 ) + ( 1 0 2 0 − 1 ) = 1 1 ( 1 + 2 + 3 … 1 1 ( 1 0 2 0 − 1 2 ) + 1 1 ( 1 0 2 0 − 1 ) )
Using summation formula , we can express the above sums as
3 × 2 [ ( 3 1 0 2 0 − 1 ) ( 3 1 0 2 0 − 1 + 1 ) ] and 1 1 × 2 [ ( 1 1 1 0 2 0 − 1 ) ( 1 1 1 0 2 0 − 1 + 1 ) ]
We can now add these two.
However, we overcounted. We need to subtract all the multiples of 33 from the sum to obtain the answer, which can be done using the same way as described above. Finally, modulo the answer by 1000000007.
While this may sound incredibly hard to compute, computers can do it well under a second.
Solution -
1 2 3 4 5 6 7 8 9 10 11 |
|
Apart from the ugly numbers in the problem, simple standard approach otherwise.
Maybe choosing a different modulus would be better, where you exploit the sum of the arithmetic progression.
There's a relatively simpler way that uses the fact that the sum of the terms in an AP is equal to 2 n ( a + l ) where n , a , l are the number of terms in AP, the first term and the last term respectively.
We define a i = ⌊ i 1 0 2 0 ⌋ for positive integer i . This is equal to the number of positive multiples of i that are ≤ 1 0 2 0 . We need to compute,
S = 2 1 ( a 3 ( 3 + 3 a 3 ) + a 1 1 ( 1 1 + 1 1 a 1 1 ) − a 3 3 ( 3 3 + 3 3 a 3 3 ) )
In Python 3, you can do this using the following code :
1 2 3 4 |
|
You can see the code in action here .
Log in to reply
Great solution. Nice to see that you started learning Python finally ;)
Problem Loading...
Note Loading...
Set Loading...
Who needs a computer? I used pen and paper :)
We add all multiples of 3 and all multiples of 11 separately. Then we subtract the multiples of 33.
From the digit sum we see that 1 0 2 0 ≡ 1 mod 3, and from the alternating digit sum/difference that 1 0 2 0 ≡ 1 mod 11. It also follows that it is ≡ 1 mod 33. Thus we must find ⎝ ⎛ k = 1 ∑ ( 1 0 2 0 − 1 ) / 3 3 k ⎠ ⎞ + ⎝ ⎛ k = 1 ∑ ( 1 0 2 0 − 1 ) / 1 1 1 1 k ⎠ ⎞ − ⎝ ⎛ k = 1 ∑ ( 1 0 2 0 − 1 ) / 3 3 3 3 k ⎠ ⎞ .
Since ∑ k = 1 n k = n ( n + 1 ) / 2 , this expression can be written as 6 ( 1 0 2 0 − 1 ) ( 1 0 2 0 + 2 ) + 2 2 ( 1 0 2 0 − 1 ) ( 1 0 2 0 + 1 0 ) − 6 6 ( 1 0 2 0 − 1 ) ( 1 0 2 0 + 3 2 ) .
Time for arithmetic modulo N = 1 0 0 0 0 0 0 0 0 7 = 1 0 9 + 7 . We use the fact that 1 0 9 ≡ − 7 mod N . Thus 1 0 2 0 − 1 = 1 0 0 ⋅ ( 1 0 9 ) 2 − 1 ≡ 1 0 0 ⋅ ( − 7 ) 2 − 1 = 4 8 9 9 mod N , and 6 1 0 2 0 + 2 ≡ 6 4 9 0 2 = 8 1 7 mod N .
Unfortunately, the last two terms are more difficult. We simplify them to 4 8 9 9 ( 2 2 4 9 1 0 − 6 6 4 9 3 2 ) = 4 8 9 9 ⋅ 1 1 1 6 3 3 mod N , but we must evaluate the fraction modulo N . The trick is to add multiples of N to the numerator to make it a multiple 11.
Now 1 6 3 3 ≡ − 1 + 6 − 3 + 3 = 5 mod 11, and N ≡ − 1 + 7 = 6 mod 11; therefore 1 6 3 3 + N ≡ 5 + 6 ≡ 0 mod 11. Thus we have 1 1 1 6 3 3 ≡ 1 1 1 0 0 0 0 0 1 6 4 0 = 9 0 9 0 9 2 4 0 mod N .
At this point we know that the answer is equal to 4 8 9 9 ⋅ ( 8 1 7 + 9 0 9 0 9 2 4 0 ) = 4 8 9 9 ⋅ 9 0 9 1 0 0 5 7 , modulo N . Since I used only pen and paper so far, I do this multiplication by hand as well, as 4 9 0 0 ⋅ 9 0 9 1 0 0 5 7 − 9 0 9 1 0 0 5 7 = 4 4 5 4 5 9 2 7 9 3 0 0 − 9 0 9 1 0 0 5 7 ; since we work modulo N = 1 0 0 0 0 0 0 0 0 7 , I can subtract 445 billions from the first term provided I also subtract 445 sevens, making the answer ≡ 4 5 9 2 7 9 3 0 0 − 7 ⋅ 4 4 5 − 9 0 9 1 0 0 5 7 = 3 6 8 3 6 6 1 2 8 .