Koo_Pung-Kei's blog

By Koo_Pung-Kei, 13 months ago, In English

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

time limit per test: 1 second
memory limit per test: 256 megabytes

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.


My ideas that turned out Time Limitation Exceeded — Brute Force

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.

Hints — Maths
Hints — Dynamic Programming

Re-thoughts after cooldown
  • Vote: I like it
  • +8
  • Vote: I do not like it

| Write comment?
»
13 months ago, hide # |
 
Vote: I like it +4 Vote: I do not like it

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)$$$.

Hint
So how do we define f(x)? (solution)

P.S this is my first time writing something like this, if there are some improvements I can do please tell me ^ ^

»
13 months ago, hide # |
 
Vote: I like it -14 Vote: I do not like it

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.

»
13 months ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

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

»
13 months ago, hide # |
 
Vote: I like it +4 Vote: I do not like it

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.

  • »
    »
    13 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    how did you get this? please tell your observations to deduce this nice proof

    • »
      »
      »
      13 months ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      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:

      1. If you have $$$0$$$, it is already divisible by $$$3$$$.
      2. If you have both $$$1$$$ and $$$2$$$ in your number there must be either $$$12$$$ or $$$21$$$ in your number, which is also divisible by $$$3$$$.
      3. If you have only $$$1$$$s or only $$$2$$$s, then you must have at least three of each $$$111$$$ or $$$222$$$, both are divisible by $$$3$$$.
  • »
    »
    13 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Thanks for your hint! It really helps. I'm stuck in making such conclusions :(

    • »
      »
      »
      13 months ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      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.