F - Guess The Number 2 解説 by spheniscine


Consider the sequence \(P = (4, 9, 5, 7, 11, 13, 17, 19, 23)\) (the first \(9\) prime numbers, except with the first two squared). Their sum is \(\Sigma = 108\), and their product is \(\Pi = 1\ 338\ 557\ 220\). They are also pairwise relatively-prime, so their LCM is equal to their product.

If we can obtain \(N \text{ mod } p\) for all \(p \in P\), we can restore \(N \text{ mod } \Pi\) using Chinese remainder theorem and Garner’s algorithm, and since \(\Pi\) is greater than the maximum value of \(N\), we then know the value of \(N\) the judge chose.

To do this, we make \(A\) a permutation of length \(\Sigma\) consisting of the concatenation of \(9\) cycles, with the \(i\)-th cycle being of size \(P_i\). We can then know \(N \text{ mod } P_i\) by looking at how much each cycle has been “rotated”.

Note the AtCoder library contains an implementation of Garner’s algorithm as crt in its math module.

投稿日時:
最終更新: