Comments

s=abcbadeeabc...

For problem D, when n = m =10^6 and t =10^7, O(n(logn)^2) is really same as O(tlogm). But to my surprise, the former get TLE!

More precisely, we should consider the size relation between 2n-3 and m, rather than 2n. As jo_on put it above, overflow mod m may happen in some situation. In fact, the boundary condition is that the sum of the two largest differences(in absolute value) of element pairs equals to m*d. That is pairs (s,s+(n-1)d) and (s,s+(n-2)d), whose sum of differences is (2n-3)d.So we should consider the size relation between 2n-3 and m. Once 2n-3<m, the insecurity, namely overflow mod m won't happen.

In short, when 2n-1=m, the case must be safe.In practice, whether 2n or 2n-3, both of them can get accepted.

Ps: My AC code with 2n 24488586 and 2n-3 24491759.