Fraction and Sequence - UVA 13041
How many triplet $(a,b,c)$ are there such that $0\le a,b,c \le L$ and $\sum_{x=0}^{\infty}{(ax^{2}+bx+c)\times (\frac{1}{10})^{x+1}} = \frac{p}{q}$ , for given $p,q,L$.
Inside Math:⌗
Suppose, $A = \sum_{x=0}^{\infty}{ax^2\times (\frac{1}{10})^{x+1}}$
$B = \sum_{x=0}^{\infty}{bx\times (\frac{1}{10})^{x+1}}$
$C = \sum_{x=0}^{\infty}{c\times (\frac{1}{10})^{x+1}}$
Hence, $\sum_{x=0}^{\infty}{(ax^{2}+bx+c)\times (\frac{1}{10})^{x+1}} = A+B+C$
Now, we know(Actually we don’t know this, we have to prove this. We’ll try to prove this in some other section) that:
$\sum_{x=0}^{\infty}{x^{2}r^{x+1}} = \frac{r^{2}(r+1)}{(r-1)^{3}} \tag{1}$
$\sum_{x=0}^{\infty}{xr^{x+1}} = \frac{r^{2}}{(r-1)^{2}} \tag{2}$
$\sum_{x=0}^{\infty}{r^{x+1}} = \frac{r}{(r-1)} \tag{3}$
from the above $(1)$, $(2)$ and $(3)$ we can easily find, $A = \frac{11}{3^{6}}$, $B = \frac{1}{3^{4}}$, $C = \frac{1}{3^{2}}$
Hence : \begin{align} \sum_{x=0}^{\infty}{(ax^{2}+bx+c)\times (\frac{1}{10})^{x+1}} &= A+B+C \newline \sum_{x=0}^{\infty}{(ax^{2}+bx+c)\times (\frac{1}{10})^{x+1}} &= a\times \frac{11}{3^{6}}+ b\times \frac{1}{3^{4}} + c\times \frac{1}{3^{2}} = \frac{p}{q} \newline \frac{11a+9b+81c}{3^{6}} &= \frac{p}{q} \newline 11a+9b+81c &= \frac{p\times 3^{6} }{q} \newline 11a+9b &= \frac{p\times 3^{6}}{q} - 81c = r \text{ [Here } q \text{ must be a divisor of }3^6\text{]} \newline \end{align}
Now , $11a+9b=r$ is a linear diophantine equation.
By solving this diophantine equation we will get: $a = 9x-4r$ and $b=5r-11x$ for any integer $x$.
Now , \begin{align} 0\le a \le L \newline 0\le 9x-4r \le L \newline \frac{4r}{9} \le x \le \frac{L+4r}{9} \end{align} Hence , $x \in [\lceil \frac{4r}{9} \rceil , \lfloor \frac{L+4r}{9} \rfloor]$
Again , \begin{align} 0\le b \le L \newline 0\le 5r-11x \le L \newline \frac{5r-L}{11} \le x \le \frac{5r}{11} \end{align} Hence , $x \in [\lceil \frac{5r-L}{11} \rceil , \lfloor \frac{5r}{11} \rfloor]$
Hence , actual value of $x$ will be in range $[\lceil \frac{4r}{9} \rceil , \lfloor \frac{L+4r}{9} \rfloor] \cap [\lceil \frac{5r-L}{11} \rceil , \lfloor \frac{5r}{11} \rfloor] = [max(\lceil \frac{4r}{9} \rceil , \lceil \frac{5r-L}{11} \rceil) , min(\lfloor \frac{L+4r}{9} \rfloor , \lfloor \frac{5r}{11} \rfloor)]$
Solution:⌗
Now we will just iterate over $c \in [0,L]$ , then for each $r = \frac{p\times 3^{6}}{q} - 81c$ we will add $max(min(\lfloor \frac{L+4r}{9} \rfloor , \lfloor \frac{5r}{11} \rfloor) - max(\lceil \frac{4r}{9} \rceil , \lceil \frac{5r-L}{11} \rceil)+1, 0)$ to our answer.
Actually, it is not a good solution, we can optimize it furthermore. But it will pass the problem.