How many positive integers of 2 0 digits chosen from the set { 1 , 2 , 3 , 6 , 9 } are divisible by 3 ?
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.
Let S = { 1 , 2 , 3 , 6 , 9 } be the set of our valid digits.
We define concatenation σ : Z + × S → Z + as σ ( a , b ) = 1 0 a + b
Suppose that x is a positive integer of d digits and x ≡ m ( m o d 3 ) .
It's clear that σ ( x , a i ) is a positive integer of d + 1 digits and σ ( x , a i ) ≡ 1 0 m + a i ( m o d 3 )
Now let F ( d , m ) be the amount of positive integers of d digits n i such that n i ≡ m ( m o d 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 ( d , m ) = a ∈ S ∑ F ( d − 1 , ( m − a ) ∗ ( 1 0 − 1 ) m o d 3 )
What this problem asks is to compute F ( 2 0 , 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 ( 2 0 , 0 ) wich is 3 1 7 8 9 1 4 4 5 7 9 2 5 9
Problem Loading...
Note Loading...
Set Loading...
Let a n be the amount of positive integers divisible by 3 with n digits, all selected from the set S = { 1 , 2 , 3 , 6 , 9 } . Now suppose that x n − 1 is a positive integer with n − 1 digits, all selected from S . Depending on the remainder modulo 3 of x n − 1 , it is possible to create a new number x n that is divisible by 3. Now:
It is clear that there are 5 n − 1 integers of the form x n − 1 , of which only 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 And, clearly: a 1 = 3 Expanding a n 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 − 5 2 n − 5 n a n = 3 5 n + 2 n + 1 The solution of the problem is a 2 0 : a 2 0 = 3 5 2 0 + 2 2 1 = 3 1 7 8 9 1 4 4 5 7 9 2 5 9