Link to the problem : - B - Mysterious Light
Link to the editorial : - http://agc001.contest.atcoder.jp/data/agc/001/editorial.pdf
I am not able to visualize how that formula comes, can somebody explain in a simpler manner ?
Tagging @l_returns as he is good at math problems.