Problem related to programming

For discussing Olympiad Level Number Theory problems
Hasib
Posts:238
Joined:Fri Dec 10, 2010 11:29 am
Location:খুলনা, বাংলাদেশ
Contact:
Problem related to programming

Unread post by Hasib » Fri Mar 09, 2012 9:30 pm

There is sequence 1,12,123,1234,..., 12345678910,.... Now you are given two integers A and B, you have to find the number of integers from A th number to B th(inclusive) number, which are divisible by 3.
For example, let A=3. B=5. So, the numbers in the sequence are,123, 1234, 12345. And 123, 12345 are divisible by 3. So, the result is 2.

WHAT'S the result while A=10 & B=110?
A man is not finished when he's defeated, he's finished when he quits.

User avatar
*Mahi*
Posts:1175
Joined:Wed Dec 29, 2010 12:46 pm
Location:23.786228,90.354974
Contact:

Re: Problem related to programming

Unread post by *Mahi* » Sat Mar 10, 2012 12:51 am

As far as I remember, this problem can be solved in O(n) complexity. We can easily see $F_i$ is divisible by 3 iff $i=3k/3k+2$. Then it is easy to determine how many $F_i$ is divisible by $3$ for $0\leq i \leq B,A$. And then determining $G_A-G_{B-1}$ if we denote $G_x=\sum^{x}_{i=0,3|F_i} 1$. I would have given my code (as I solved it earlier) But I have lost it so :).
And the answer you needed is 67.
Please read Forum Guide and Rules before you post.

Use $L^AT_EX$, It makes our work a lot easier!

Nur Muhammad Shafiullah | Mahi

User avatar
Masum
Posts:592
Joined:Tue Dec 07, 2010 1:12 pm
Location:Dhaka,Bangladesh

Re: Problem related to programming

Unread post by Masum » Sat Mar 10, 2012 10:44 am

Define a function $\gamma$ by \[\gamma(n)=1\text{ if }n \equiv2\pmod3\]
otherwise, \[\gamma(n)=0\]
The number of such numbers divisible by $3$ is with $a$ digits is \[f(a)=2\lfloor{\frac {a}{3}}\rfloor+\gamma(a)\]
For numbers between $a$ and $b$, the required number is \[f(b)-f(a-1)\]
$O(1)$ solution ;)
One one thing is neutral in the universe, that is $0$.

Post Reply