I recommend using std::unordered_map for your lookup table.
http://www.cplusplus.com/reference/unordered_map/unordered_map/
tpb wrote: |
---|
I think there is a fundamental flaw in your algorithm.
Your code relies on, e.g., m(20) = m(10) + m(6) + m(5) |
This isn't quite true. For sufficiently large N, N < m(N/2)+m(N/3)+m(N/4). 100,000 is definitely large enough, but OP should be careful coding like this. When OP corrects line 7 I think he probably gets the right answer. I think you misunderstood the problem as your algorithm isn't correct.
An input of 5 should return 4 (your code says 5). |
An input of N should never return less than N.
An input of 20 should return 23 (your code says 21). |
An input of 20 returns 21. You used the wrong algorithm:
20/2+20/3+20/4 = 10+6+5 = 21 (improvement, trade for 3 more coins)
10/2+10/3+10/4 = 5+3+2 = 10 (no improvement, exchange for $)
6/2+6/3+6/4 = 3+2+1 = 6 (no improvement, exchange for $)
5/2+5/3+5/4 = 2+1+1 = 4 (no improvement exchange for $)
Edit: I signed up for online judge and submitted my own solution to the problem which worked. My program works up to inputs of about 10^17.