Official

E - Strange Relation Editorial by evima


We will consider the value \(f(A,T)\) for given \(A\) and \(T\). Let \(z_i=A_i+T \times x_i\) here. For the lexicographically greatest \(x\), there is no \(i>1\) such that \(A_1 - T < z_i \leq A_1\). This is because, if there existed such \(i\), choosing the minimum such \(i\) would make the value \(x_i\) greater.

Now, consider obtaining a sequence \(x'_1,x'_2,\cdots,x'_{N-1}\) from the lexicographically greatest \(x_1,x_2\cdots,x_N\) in the following procedure:

  • If \(A_1<z_{i+1}\), let \(x'_i=x_{i+1}-1\); otherwise, let \(x'_i=x_{i+1}\).

Then, the sequence \(x'\) satisfies the conditions in the statement for \((A_2,A_3,\cdots,A_N)\). Additionally, on the other hand, if there is a sequence \(x'\) that satisfies the conditions in the statement for \((A_2,A_3,\cdots,A_N)\), it is possible to obtain \(x\) from that \(x'\). Furthermore, \(x\) is the lexicographically maximum if and only if \(x'\) is the lexicographically maximum. Now, we can compute the value \(f(A,T)\) from \(f((A_2,A_3,\cdots,A_N),T)\).

Let \(g(A,T)\) denote the last element of \(f(A,T)\). Let us consider what the value \(g(A,T)\) will be. It turns out that it only depends on \(g((A_2,A_3,\cdots,A_N),T)\), \(A_1\), and \(A_N\). Thus, by fixing the value of \(A_N\), we can find the number of \((A_i,\cdots,A_N)\) such that \(g((A_i,\cdots,A_N),T)=j\) for each pair \(i,j\) using DP.

Similarly, we can also find the distribution of the values of the \(1\)-st, \(2\)-nd, \(\cdots\), \((N-1)\)-th elements of \(f(A,T)\). The total time complexity will be \(O(N^3K^2)\).

posted:
last update: