Bruteforcers will be doomed!

Let S S be the sum of the positive integers below 1 0 20 10^{20} which are divisible by 3 or 11.

What is S m o d 1000000007 S \mod 1000000007 ?

Details and Assumptions

  • No numbers in the list of integers divisible by 3 or 11 must be duplicates.


The answer is 368366128.

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

Arjen Vreugdenhil
Nov 24, 2015

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 20 1 10^{20} \equiv 1 mod 3, and from the alternating digit sum/difference that 1 0 20 1 10^{20} \equiv 1 mod 11. It also follows that it is 1 \equiv 1 mod 33. Thus we must find ( k = 1 ( 1 0 20 1 ) / 3 3 k ) + ( k = 1 ( 1 0 20 1 ) / 11 11 k ) ( k = 1 ( 1 0 20 1 ) / 33 33 k ) . \left(\sum_{k=1}^{(10^{20}-1)/3} 3k\right) + \left(\sum_{k=1}^{(10^{20}-1)/11} 11k\right) - \left(\sum_{k=1}^{(10^{20}-1)/33} 33k\right).

Since k = 1 n k = n ( n + 1 ) / 2 , \sum_{k=1}^n k = n(n+1)/2, this expression can be written as ( 1 0 20 1 ) ( 1 0 20 + 2 ) 6 + ( 1 0 20 1 ) ( 1 0 20 + 10 ) 22 ( 1 0 20 1 ) ( 1 0 20 + 32 ) 66 . \frac{(10^{20}-1)(10^{20}+2)}6 + \frac{(10^{20}-1)(10^{20}+10)}{22} - \frac{(10^{20}-1)(10^{20}+32)}{66}.

Time for arithmetic modulo N = 1 000 000 007 = 1 0 9 + 7 N = 1\:000\:000\:007 = 10^9+7 . We use the fact that 1 0 9 7 10^9 \equiv -7 mod N N . Thus 1 0 20 1 = 100 ( 1 0 9 ) 2 1 100 ( 7 ) 2 1 = 4899 mod N , 10^{20}-1 = 100\cdot (10^9)^2 - 1 \equiv 100\cdot (-7)^2 - 1 = 4899\ \text{mod}\ N, and 1 0 20 + 2 6 4902 6 = 817 mod N . \frac{10^{20}+2}6 \equiv \frac{4902}6 = 817\ \text{mod}\ N.

Unfortunately, the last two terms are more difficult. We simplify them to 4899 ( 4910 22 4932 66 ) = 4899 1633 11 mod N , 4899\left(\frac{4910}{22} - \frac{4932}{66}\right) = 4899\cdot\frac{1633}{11}\ \text{mod}\ N, but we must evaluate the fraction modulo N N . The trick is to add multiples of N N to the numerator to make it a multiple 11.

Now 1633 1 + 6 3 + 3 = 5 1633 \equiv -1+6-3+3 = 5 mod 11, and N 1 + 7 = 6 N \equiv -1+7 = 6 mod 11; therefore 1633 + N 5 + 6 0 1633 + N \equiv 5 + 6 \equiv 0 mod 11. Thus we have 1633 11 1 000 001 640 11 = 90 909 240 mod N . \frac{1633}{11} \equiv \frac{1\:000\:001\:640}{11} = 90\:909\:240\ \text{mod}\ N.

At this point we know that the answer is equal to 4899 ( 817 + 90 909 240 ) = 4899 90 910 057 , 4899\cdot (817 + 90\:909\:240) = 4899\cdot 90\:910\:057, modulo N N . Since I used only pen and paper so far, I do this multiplication by hand as well, as 4900 90 910 057 90 910 057 = 445 459 279 300 90 910 057 ; 4900\cdot 90\:910\:057 - 90\:910\:057 = 445\:459\:279\:300 - 90\:910\:057; since we work modulo N = 1 000 000 007 N = 1\:000\:000\:007 , I can subtract 445 billions from the first term provided I also subtract 445 sevens, making the answer 459 279 300 7 445 90 910 057 = 368 366 128 . \equiv 459\:279\:300 - 7\cdot 445 - 90\:910\:057 =\boxed{368\:366\:128}.

Great solution!

Arulx Z - 5 years, 6 months ago
Hasmik Garyaka
Oct 13, 2017

I used windows scientifc calculator to calc n/66(13n+33) and mod 1000000007

Arulx Z
Nov 9, 2015

Easy way to solve it is by doing this (pseudocode) -

1
2
3
4
sum = 0
for x = 1 to 10^20
    if x % 3 = 0 or x % 11 = 0
        sum += x 

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 20 4 ) + ( 1 0 20 1 ) = 3 ( 1 + 2 + 3 ( 1 0 20 4 ) 3 + ( 1 0 20 1 ) 3 ) 3+6+9\dots \left( 10^{ 20 }-4 \right) +\left( 10^{ 20 }-1 \right) =3\left( 1+2+3\dots \frac { \left( 10^{ 20 }-4 \right) }{ 3 } +\frac { \left( 10^{ 20 }-1 \right) }{ 3 } \right)

Similarly, sum of multiples of 11 can be written as

11 + 22 + 33 ( 1 0 20 12 ) + ( 1 0 20 1 ) = 11 ( 1 + 2 + 3 ( 1 0 20 12 ) 11 + ( 1 0 20 1 ) 11 ) 11+22+33\dots \left( 10^{ 20 }-12 \right) +\left( 10^{ 20 }-1 \right) =11\left( 1+2+3\dots \frac { \left( 10^{ 20 }-12 \right) }{ 11 } +\frac { \left( 10^{ 20 }-1 \right) }{ 11 } \right)

Using summation formula , we can express the above sums as

3 × [ ( 10 20 1 3 ) ( 10 20 1 3 + 1 ) ] 2 3\times \frac { \left[ \left( \frac { { 10 }^{ 20 }-1 }{ 3 } \right) \left( \frac { { 10 }^{ 20 }-1 }{ 3 } +1 \right) \right] }{ 2 } and 11 × [ ( 10 20 1 11 ) ( 10 20 1 11 + 1 ) ] 2 11\times \frac { \left[ \left( \frac { { 10 }^{ 20 }-1 }{ 11 } \right) \left( \frac { { 10 }^{ 20 }-1 }{ 11 } +1 \right) \right] }{ 2 }

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
n = 10 ** 20
summ = 0

for x in [3, 11]:
    lar = n / x
    summ += x * (lar * (lar + 1)) / 2

lar = n / 33

summ -= 33 * (lar * (lar + 1)) / 2
print summ % 1000000007

Moderator note:

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 n 2 ( a + l ) \frac n2(a+l) where n , a , l n,a,l are the number of terms in AP, the first term and the last term respectively.

We define a i = 1 0 20 i a_i=\left\lfloor\dfrac{10^{20}}{i}\right\rfloor for positive integer i i . This is equal to the number of positive multiples of i i that are 1 0 20 \leq 10^{20} . We need to compute,

S = 1 2 ( a 3 ( 3 + 3 a 3 ) + a 11 ( 11 + 11 a 11 ) a 33 ( 33 + 33 a 33 ) ) S=\frac{1}{2}\bigg(a_3(3+3a_3)+a_{11}(11+11a_{11})-a_{33}(33+33a_{33})\bigg)

In Python 3, you can do this using the following code :

1
2
3
4
def a(i):
    return (10**20//i)
S=(a(3)*(3+3*a(3))+a(11)*(11+11*a(11))-a(33)*(33+33*a(33)))//2
print(S%1000000007)

You can see the code in action here .

Prasun Biswas - 5 years, 7 months ago

Log in to reply

Great solution. Nice to see that you started learning Python finally ;)

Arulx Z - 5 years, 7 months ago

Log in to reply

Ahaha, thanks! :D

Prasun Biswas - 5 years, 7 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...