Argghh this question made me pull out at least 100 hairs from my head. I can now see a bald spot that I can safely attribute to MINDSUM. What a great problem. It has a greedy approach that passes most of the test cases, but fails at a select few. I threw hundreds of test cases at it, and it passed (The same greedy approach many others also took) One where it fails was 2 82.
To top it off, it has another elegant solution that can be solved in multiple ways. Hats off to the setter.
Follow up question: Can you please explain how to solve the first subtask?
@mr__cyborg,for a given n we know that digits will start repeating after certain time.
I first calculated the digitsum of n and stored the number of operation for that particular digit in an array.
then added d to n and did the same thing as above till the digits started repeating and then i printed the minimum number with its number of operation.
@sm1dash (sorry, I don’t have enough rep points to comment directly below your post) –
Your code ( Link ) doesn’t actually traverse a tree, which is the essential requirement to solve the problem.
Here’s the key section, lines 32-41 and 46:
dval=n;
while (dval > 9)
{
dval=digitSum(dval);
op2++;
}
if (!result[dval])
{
result[dval]=op+op2;
}
...
n += d;
As you know, the while loop takes the current value of n and iterates digitSum() repeatedly until a value <= 9 is found. Then if you haven’t already come across that value (i.e. if !result[dval]), you store the number of steps. Then in line 46 you increase n by d and continue to the next pass in the loop.
The problem is that digitSum(n + k · d) (for some k, 0≤k≤9) is not always the shortest solution, which is why a binary tree is needed. At each fork point, your options are to add d to the current value, or apply digitSum() to the current value. This leads to 2^x possibilities, where x is max-number-of-steps. Your loop just covers 10 possibilities.
In the editorial it’s mentioned that the relative order of the add and sumofdigit() operations does matter! Could you please give me one testcase where this matters? My code didn’t take this into account and thus i failed 4 out of 8 tasks → CodeChef: Practical coding for everyone
what is it with 2047. I read a few codes of people and many have used i<2047. Can anyone please tell me. Like in this solution: CodeChef: Practical coding for everyone (this is not my code)
For large integers you’re right. Large integer division in python for example has complexity is something like O(\log{n}^2). For small integers (in this context 32-bit or 64-bit numbers) we can view this as a constant cost (albeit expensive compared to addition or multiplication). In this case we have comparatively small (10^10 fits easily into 64-bits) numbers.
@vivek_1998299 I won’t go into full detail here. The Berlekamp-Massey algorithm can be used to find the shortest linear feedback shift register that fits a sequence. For this purpose, think of finding k coefficients c_i of a relation a_n = c_1 a_{n-1} + c_2 a_{n-2} + \ldots + c_k a_{a-k} such that the relation recreates a given sequence. When the sequence has been found you can rather quickly find the nth term.
So essentially, simulate for a bit, throw the result into Berlekamp-Massey and extrapolate. Here I ignore some pitfalls, but it’s the essential idea.