Hello @all,
I haven’t had any time to actually “attend” the cook-off, as I was busy with other things in the meantime… However, I still thought about this problem a bit, as I have read alike statements many times…
My idea was to devise some counting function such that the answer could be obtained in the fashion F®-F(L-1) where L and R stand for the left and right limits of the interval.
I also realized the “pattern” such that:
Sum(1,X) = 1*sum of i for i in (1, 9) + 2*sum of i for i in (10, 99)+ ... + length(X)*sum of i for i in (10^(length(X)), X)
but I couldn’t make any use of it besides this deduction…
I also thought about pre-computing all the values for the sums of numbers with same length and then just play with the offsets… Here’s my Python solution:
def summa(a,b):
if a > b:
return 0;
else:
return (a+b)*(a-b-1)/(-2)
L = [i*summa(pow(10,i-1),pow(10,i)-1) for i in range(1,10)]
i = int(raw_input())
while i > 0:
l,u = [int(x) for x in raw_input().split()]
x = len(str(l))
y = len(str(u))
answerl = 0
answeru = 0;
for j in range(x-1):
answerl += L[j]
answerl += (x*summa(pow(10,x-1),l))
for jj in range(y-1):
answeru += L[jj]
answeru += (y*summa(pow(10,y-1),u))
if len(str(l)) < len(str(u)):
print (answeru-answerl+l)%1000000007
else:
print (answeru-answerl+2*l)%1000000007
i-=1
which obviously gets WA veredict…
I tried to read some solutions (@mugurelionut , @alex_2oo8…) and they all use this same counting function approach…
I would like to know the logic they used in order to devise their correct counting scheme, as the rest is only implementation details…
Any tips will be great!!
Best regards,
Bruno