Official

C - Long Sequence Editorial by en_translator


Concatenating arrays naively as instructed may violate the execution time limit or memory limit, so we look for an alternative way.

Let us find which element of which integer sequence in \(A\), \(B_K\) is.

By tracking the procedure step-by-step, the sought integer sequence can be identified as \(A_k\) for the largest \(k\) with \(\displaystyle \sum_{j=1}^{k-1} L_jC_j < K\).

Therefore, the answer can be found by the following steps:

  • For \(i=1,2,\ldots,N\) in order, do the following:
    • If \(K \le L_iC_i\), then the sought value is the \(K\)-th element of a \(C_i\)-time repetition of \(A_i\), that is, \(A_{i,(K-1)\bmod L_i + 1}\).
    • Otherwise, replace \(K\) with \(K-L_iC_i\).

The problem can be solved by appropriately implementing the algorithm above.

Sample code (Python)

import sys
input = sys.stdin.readline

n, k = map(int, input().split())
k -= 1
a = []
for i in range(n):
    aa = list(map(int, input().split()))[1:]
    a.append(aa)
c = list(map(int, input().split()))
for i in range(n):
    if k < c[i] * len(a[i]):
        print(a[i][k % len(a[i])])
        break
    k -= c[i] * len(a[i])

posted:
last update: