Hello, Codeforces!
I took a code capability test for a job in a company 4 days before so there's no need to worry about cheats and leaks, and one of the 4 problems has troubled me for so long that I have to come here for guidance and hints today.
The problem is as follows:
Dodo's Lucky Number Lottery
Dodo has discovered a brand-new type of scratch-off lottery ticket that will give prizes to those who scratch Lucky Numbers out. Lucky Numbers are natural numbers with substrings whose values are divisible by $$$3$$$ ($$$0$$$ included).
Given two integers $$$L$$$ and $$$R$$$ $$$(L\le R)$$$, Dodo wants to find out how many Lucky Numbers are there in the range $$$[L,R]$$$. But it's difficult, please help him!
Input
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1\le t\le 2\times10^4)$$$.
Each test case will contain two integers $$$L$$$ and $$$R$$$ $$$(1\le L\le R\le 10^{18})$$$, the left and right end of the range in search of Lucky Numbers.
Output
For each test case, output the number of Lucky Numbers in the given range.
Example
Input
2
11 19
20 40
Output
6
18
Explanation
For the first test case, $$$[12,13,15,16,18,19]$$$ are Lucky Numbers.
For the second test case, $$$[20,21,23,24,26,27,29,30,31,32,33,34,35,36,37,38,39,40]$$$ are Lucky Numbers.
Thanks in advance for your help.
UPD1: There has been solutions that proved correct in the comments. Thanks again for helping hands from all of commenters.








When finding the number of solutions in the range [L,R], we can rewrite it as $$$f(R)-f(L-1)$$$ where $$$f(x)$$$ is a function that returns the number of lucky numbers from 1 to x. Now, the next step for us is to define $$$f(x)$$$.
A key insight to solving this problem is to find what makes a number not lucky
Let's try to do small numbers, trivially for 1-digit numbers, let's group them into their values mod 3
n ≡ 0 (mod 3) [0,3,6,9]
n ≡ 1 (mod 3) [1,4,7]
n ≡ 2 (mod 3) [2,5,8]
this will be useful later but for now we can see that the non-lucky 1-digit numbers are [1,2,4,5,7,8]
For 2-digit numbers, we can see that it's actually just 2 1-digit numbers, and if one of those two numbers is in [0,3,6,9], we can just take the substring containing that number making it automatically happy, you can also generalize this for any n-digit number.
but what about the numbers not containing any digits in [0,3,6,9]? let x be any number in the set [1,4,7] (x ≡ 1 (mod 3)) and let y be any number in [2,5,8] (y ≡ 2 (mod 3)). This gives is 4 numbers in the form of
Recall the divisibility rule of 3, which states that if the sum of the digits of a number is divisible by three, the number itself is also divisible by 3, let's apply this to the numbers above.
xy = 1 + 2 = 0 (mod 3)
yx = 2 + 1 = 0 (mod 3)
xx = 1 + 1 = 2 (mod 3)
yy = 2 + 2 = 1 (mod 3)
this means that only numbers in the form of xx and yy are not lucky numbers, which gives us our list of 2-digit non-lucky numbers: [11, 14, 17, 22, 25, 28, 41, 44, 47, 52, 55, 58, 71, 74, 77, 82, 85, 88]
Moving on to three digit numbers, we have
We can already eliminate those with xy and yx because we have proven that those are lucky, which leaves us with
let's solve their values
xxx = 1 + 1 + 1 = 0 (mod 3)
yyy = 2 + 2 + 2 = 0 (mod 3)
Therefore, this proves that all 3-digit numbers are lucky and subsequently all n-digit numbers (n>3) are also lucky. (because any n-digit number contains a 3-digit number)
So we have 24 non-lucky numbers
[1, 2, 4, 5, 7, 8, 11, 14, 17, 22, 25, 28, 41, 44, 47, 52, 55, 58, 71, 74, 77, 82, 85, 88]
We then just check for each number in this list if they are inside the range of x in f(x), and subtract one if true.
P.S this is my first time writing something like this, if there are some improvements I can do please tell me ^ ^
Good
Problem is too easy, answer will be (r-l)/3 + (r%3 ! = l%3) because all numbers divisible by 3 can be calculated by dividing the range by 3.
I'm almost certainly overcomplicating this but this was my first thought. (EDIT: Reading the other solution I 100 percent overcomplicated it :|, still a good trick to know)
This problem can be solved using digit DP. First, acknowledge that if a number is divisible by 3, the sum of its digits is divisible by 3. This means that when placing a number, we can handle it mod 3 due to how modular addition works.
Now we can get into the dp states. Like most digit dp, we have to keep a state of if we have gone under the number, and also one if we have placed a non zero number, along with the position of the digit that we are placing.
We'll need 3 additional states (note that if there is a suffix that is 0 mod 3, then we would already have a substring divisible by three, and the completed flag would be set) - completed: do we already have a substring that is divisible by 3 - suffix_mod_3_1: from the elements we have already placed, is there any suffix that is equal to 1 mod 3 - suffix_mod_3_2: from the elements we have already placed, is there any suffix that is equal to 2 mod 3
The purpose of the suffix states are the following, imagine that the number that we've created so far is 52. In this case there is a suffix equal to 1 mod 3 (52) and a suffix equal to 2 mod 3 (2). If we were to add a number to the end, let's say a, then - If a mod 3 is 1, we'd have a substring equal to 0 mod 3 (2a) - If a mod 3 is 2, we'd have a substring equal to 0 mod 3 (52a) - If a mod 3 is 3, we'd have a substring equal to 0 mod 3 (a)
Transitions can be handled using the same idea, and from there, it's standard Digit DP.
I probably made a couple of mistakes explaining so my apologies if I did
all numbers over >100 are lucky numbers.
Proof:
Lemma 1: All digits that are multiples of three have digit sums that are multiples of 3. Proof: Look It up.
Let's store an arbritary three digit number x's digits. Each digit will have arbritary value mod 3. Let's take the prefix sum of the digit written left to right. We will get four prefix sums 0, x[0]%3,(x[0]+x[1])%3,(x[0:2])%3. There are three posible values mod 3 (0,1,2), but we have four prefix sums, so by pigeonhole principle since 4 > 3, there is at least one pair of prefixes that have the same sum. Then, by prefix sums there is a substring that has a digit sum that is a multiple of 3, and thus the substring is a multiple of 3.
Therefore, the correct algorithm is brute force to 100, and include all other numbers, which should pass.
how did you get this? please tell your observations to deduce this nice proof
Each digit can be $$$0$$$, $$$1$$$ or $$$2$$$ (modulo 3). If you have at least three digits, there is no pattern that leads to no substring having sum not divisible by $$$3$$$. Let's prove it in a simplest way:
this idea seems nice .can you suggest me some problems similar to this or just give me the probable tag and rating of this problem
No, I haven't seen this problem or any other similar before.
Thanks for your hint! It really helps. I'm stuck in making such conclusions :(
I think the key is to try to play with numbers. I tried 21 (divisible by 3), 22, 23 (has 3), 24 (divisible by 3), 25, 26 (has 6). I think you noticed the pattern here. What is so special about 22, 25 and 28? Let's try another numbers. Obviously, I skip numbers starting with 3 or 6 as they are always "lucky".
Divisibility rule of 3 comes to mind (the number is divisible by 3 if and only if the sum of its digits is divisible by 3), so if you try numbers 13, 14, 15,... it is the same thing as trying 43, 44, 45,.... Therefore, it is more convenient to observe numbers starting with 1: 10 (has 0), 11, 12 (divisible by 3), 13 (has 3), 14, 15 (divisible by 3).
Again the same pattern (almost the same). It is clear now, not lucky numbers are those, who don't have 0, 3, 6,9 in their digits and as remainders of digits 1+2 and 2+1 result in the number which is divisible by 3, 1+1 and 2+2 (in terms of remainders) are left.
Which means that "unlucky" numbers don't have digits divisible by 3 and have digits with the same remainder. As soon as we don't follow these rules, imagine numbers that look like (in terms of remainders of digits): XXXX0XXXX..., XXX12XXX..., XXXX21XXX..., all result in lucky numbers. The last step is either by intuition or by trying to expand the rule for more than 2 digit numbers. 111, 222 results in the number which is divisible by 3 so it is clear that all 3-digit numbers and so on are "lucky".
I hope I made the intuition more clear, but I also think I kinda overcomplicated some of the stuff. That was a nice problem to solve. If you have questions, I will try to answer it.