Hello, i just made an AC with this problem and want to give few tips to others.
The main hardship in this problem is a horrible input/output example code, also those provided in this thread are mostly misguiding and wrong, so after a lot of pain and failure attempts by making some random corrections i eventually collected proper input and gathered enought data to finish the algorithm...
Input:
11 10
81 64
5764801 1679616
1 1
2 1
4 1
216 125
125 64
1024 243
371293 248832
1024 1
64 1
1048576 59049
483736625 481890304
0 0
Output:
1 21
9 217
335923 30275911
0 1
1 3
2 7
31 671
21 369
121 3367
22621 1840825
10 2047
6 127
29524 4017157
615441 1931252289
I will try to point out important things to pay attention to:
- since the range of values is undefined i decided to play it safe and used double type of variables,
fprintf(stdout,"%.0lf %.0lf\n",total_idlers,stack_height);
This line generates correct output, just in case someone was wondering if maybe the presence of '\n' in the last line might be the reason of WA
- Instead of making one generic algorithm, try to exclude special cases like:
if (initial_height==workers+1),
if (workers==1), this has two cases, one is when initial height divides by 2 down to 1, (all multipliers of 2) and when it doesnt (eg. 10) in which case i simply print : total_idlers=1; stack_height=initial_height+1;
there is no logic in last case but the debugger seems to like it that way ;D
Now, for the rest of the cases, you are looking for the "divider", when you have it you print the answer and this is where you fail miserably, then start hopelessly banging your head against the wall *,..,*
The reason is that some numbers have several 'working' dividers, and before you move on you have to verify it and if it wont work then for look for another
![:)](./images/smilies/icon_smile.gif)
Basically the initial height and workers should divide exact amount of times by the 'divider' before reaching 1.
I hope that it will be helpfull and that i didint spoilered too much
![:)](./images/smilies/icon_smile.gif)