如果在你的思路中把 i≠ji \neq ji=j 用这种方式拆开:
请在 n>mn > mn>m 时 swap(n, m),因为此时后一层循环本身没有包括 m mod im \bmod immodi 项。
swap(n, m)