So I have been at this problem for hours now and have tried everything I could including stress testing with a brute force solution for hours now and I just can't seem to find my mistake.
The question isn't that hard until you look at the source limit. I have managed to squeeze my solution after deleting all white-spaces but now I am at my limit and can't figure out the reason for a wrong answer
#include<bits/stdc++.h>
typedef int64_t q;
q a, b, d, B, f=10;
q s(q x, q l, q p){
if(p > x) return 0;
B = (x / p) % 10;
return (p / f * l * B) + (p * (d < B)) + ((x % p + 1)*(d == B)) + s(x, l+1, p*f);
}
int main(){
while(std::cin >> a >> b) if(a){
if(a > b) std::swap(a,b);
for(d = 1; d < f; d++) std :: cout << s(b, 0, 1) - s(a-1, 0, 1) << " \n"[d == 9];
}
return 0;
}
Variables a
and b
correspond to the two input integers
Variable d
corresponds to a digit from 1 to 9 for which I wish to evaluate the answer
Variable f
is just the constant 10 and has been used only to shorten the code
The function s()
counts the number of occurences of the digit d in numbers <= x
p
is a power of 10 i.e. p = pow(10,l
) where l
is the other parameter of function s
corresponding to the last
Variable 'B' corresponds to the l
'th digit of the number x
starting from the left.
The function is computed recursively. What am I missing?