Recently I am solving this problem from AtCoder Regular Contest 061: https://atcoder.jp/contests/arc061/tasks/arc061_d
However, I do not come up with any idea. I have tried to look at others' submissions since this contest does not have an English editorial, but I still cannot figure out what the mathematical formulas written in those codes mean.
Hope you guys can help me with the solution for this problem. Many thanks!