Would you count it manually? part 3

How many positive integers of 20 20 digits chosen from the set { 1 , 2 , 3 , 6 , 9 } \left\{ {1,2,3,6,9} \right\} are divisible by 3 3 ?


The answer is 31789144579259.

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.

2 solutions

Daniel Turizo
Nov 10, 2015

Let a n a_n be the amount of positive integers divisible by 3 3 with n n digits, all selected from the set S = { 1 , 2 , 3 , 6 , 9 } S = \left\{ {1,2,3,6,9} \right\} . Now suppose that x n 1 x_{n-1} is a positive integer with n 1 n-1 digits, all selected from S S . Depending on the remainder modulo 3 3 of x n 1 x_{n-1} , it is possible to create a new number x n x_n that is divisible by 3. Now:

  • If x n 1 3 1 x_{n - 1} \equiv _3 1 , then add the digit 2 2 .
  • If x n 1 3 2 x_{n - 1} \equiv _3 2 , then add the digit 1 1 .
  • If x n 1 3 0 x_{n - 1} \equiv _3 0 , then either add the digit 3 3 , 6 6 or 9 9 .

It is clear that there are 5 n 1 5^{n-1} integers of the form x n 1 x_{n-1} , of which only a n 1 a_{n-1} are divisible by 3. Then: a n = 1 ( 5 n 1 a n 1 ) + 3 a n 1 a n = 5 n 1 + 2 a n 1 a_n = 1 \cdot \left( {5^{n - 1} - a_{n - 1} } \right) + 3 \cdot a_{n - 1} \\ a_n = 5^{n - 1} + 2a_{n - 1} And, clearly: a 1 = 3 a_1 = 3 Expanding a n a_n n 1 n-1 times: a n = 5 n 1 + 2 5 n 2 + + 2 n 2 5 + 2 n 1 a 1 a n = 5 n 1 + 2 5 n 2 + + 2 n 2 5 + 2 n 1 3 a n = 2 n + k = 0 n 1 2 k 5 n 1 k a n = 2 n + 2 n 5 n 2 5 a n = 5 n + 2 n + 1 3 a_n = 5^{n - 1} + 2 \cdot 5^{n - 2} + \ldots + 2^{n - 2} \cdot 5 + 2^{n - 1} \cdot a_1 \\ a_n = 5^{n - 1} + 2 \cdot 5^{n - 2} + \ldots + 2^{n - 2} \cdot 5 + 2^{n - 1} \cdot 3 \\ a_n = 2^n + \sum\limits_{k = 0}^{n - 1} {2^k \cdot 5^{n - 1 - k} } \\ a_n = 2^n + \frac{{2^n - 5^n }}{{2 - 5}} \\ a_n = \frac{{5^n + 2^{n + 1} }}{3} The solution of the problem is a 20 a_{20} : a 20 = 5 20 + 2 21 3 = 31789144579259 a_{20} = \frac{{5^{20} + 2^{21} }}{3} = \boxed{31789144579259}

Og Astorga Diaz
Nov 11, 2015

Let S = { 1 , 2 , 3 , 6 , 9 } S = \{1,2,3,6,9\} be the set of our valid digits.

We define concatenation σ : Z + × S Z + \sigma : { { Z } }^{ + }\times S \rightarrow { { Z } }^{ + } as σ ( a , b ) = 10 a + b \quad \sigma (a,b)=10a+b

Suppose that x x is a positive integer of d d digits and x m ( m o d 3 ) x\equiv m \quad ({ mod } \quad 3) .

It's clear that σ ( x , a i ) \sigma(x,a_{ i }) is a positive integer of d + 1 d+1 digits and σ ( x , a i ) 10 m + a i ( m o d 3 ) \sigma(x,a_{ i })\equiv 10m+a_{ i }\quad (mod\quad 3)

Now let F ( d , m ) F(d, m) be the amount of positive integers of d d digits n i n_{i} such that n i m ( m o d 3 ) n_{ i }\equiv m \quad ({ mod } \quad 3)

F ( 1 , m ) = t h e n u m b e r o f d i g i t s i n S c o n g r u e n t w i t h m m o d u l o 3. F(1,\quad m)\quad =\quad the\quad number\quad of\quad digits\quad in\quad S\quad congruent\quad with\quad m\quad modulo\quad 3. F ( d , m ) = a S F ( d 1 , ( m a ) ( 10 1 ) m o d 3 ) F(d,\quad m)\quad =\quad \sum _{ a\in { S } }{ F(d-1,\quad (m-a)*({10}^{-1}) mod\quad 3) }

What this problem asks is to compute F ( 20 , 0 ) F(20,0)

We can compute this recursive function with Dynamic Programming approach

C++11 code

#include <iostream>
#include <vector>
#include <cstring>

std::vector<int> digits = {1,2,3,6,9};
long long DP[10][3];

int main() {
    memset(DP, 0, sizeof DP);

    DP[0][0] = 1LL;
    for (int i=0; i<=20; i++) {
        for (int j=0; j<3; j++) {
            for(auto it: digits) {
                DP[i+1][(j*10+it)%3] += DP[i][j];
            }
        }
    }

    std::cout << DP[20][0] << std::endl;

    return 0;
}

The program above prints the value of F ( 20 , 0 ) F(20, 0) wich is 31789144579259 \boxed{31789144579259}

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...