公式

H - TTPC Never Ends 解説 by noya2


Focusing on the first character of the abbreviation, it is determined by the position of cursor \(1\), and the next position the cursor moves to via the update button depends solely on the current cursor position. Since there are only a finite number of cursor positions, the first character of the abbreviation will eventually start changing periodically after a certain year. (This is easier to understand if you consider the movement on a functional graph.)

By applying the same reasoning to the other characters, we can see that for a sufficiently large integer \(Y\), the abbreviation changes periodically from \(Y\) years onwards. Let this period be \(T\).

Whether there exists a last year when the abbreviation is TTPC is equivalent to whether there exists a year in the period where the abbreviation is TTPC. Therefore, the following solution can be considered:

  1. Simulate the machine’s operation up to \(Y\) years from now, and record the last year when the abbreviation is TTPC as \(\text{Ans}\).
  2. Continue the simulation up to \(T\) more years. If there exists a year during this period where the abbreviation becomes TTPC, output NeverEnds.
  3. Otherwise, output \(\text{Ans}\).

Even with large estimates for \(Y\) and \(T\), this solution remains valid. Therefore, let’s estimate the sizes of \(Y\) and \(T\). Since all characters become periodic within \(N\) years, \(Y \le N\). Additionally, since the period of a single character is at most \(N\), we can estimate \(T \le N^4\). In implementation, you can simply set \(Y = N\) and \(T = N^4\).

Hence, this solution operates in \(O(N^4)\) time complexity.

Constant Factor Improvement

By a constant factor improvement, we can also estimate \(T \le (N/4)^4\), allowing us to solve the problem under constraints like \(N \le 200\).

This estimate is derived by considering that the period of four points moving along the functional graph is the least common multiple (LCM) of the periods of the individual points. Since two points within the same connected component share the same period, they do not contribute to increasing the LCM. Therefore, we can assume that the four points belong to different connected components. Let the sizes of these connected components be \(S_1, S_2, S_3, S_4\). Under the constraint \(S_1 + S_2 + S_3 + S_4 \le N\), we can upper-bound \(\mathrm{lcm}(S_1, S_2, S_3, S_4)\). By bounding the product of the sizes and using the arithmetic mean-geometric mean (AM-GM) inequality, we get:

\[\mathrm{lcm}(S_1, S_2, S_3, S_4) \le S_1 S_2 S_3 S_4 \le (N/4)^4.\]

Further Optimization

By detecting the period for each character, this problem can be reduced to TTPC2023 L - Next TTPC 3. In this case, the time complexity is approximately \(O(N^2 \log N)\).

投稿日時:
最終更新: