Codeforces k-Tree





I am unable to figure out why the above code (same logic as editorial) is leading to WA in test case 7. Could someone please help me identify where I am going wrong? I am guessing it’s something to do with the mod, but not too sure.


You need to change the ‘int’ dp[104][2] to ‘long long’ dp[104][2]. This is because the temporary result stored in dp*[k] is of data type int which overflows and wraps around to give negative results and hence wrong answer.

Accepted solution


Thanks @devilhector, I was making this mistake in another problem as well. Finally understood why the WA.