ABC258 E - Packing Potatoes
https://atcoder.jp/contests/abc258/tasks/abc258_e (opens in a new tab)
ๆฐด่ฒไธไฝใๅจๆๆงใใใใชใณใฐใ
ๅ็ฎฑใฎใใใใใใฎ้ใใๅๆฐใๅจๆ็ใงใฏใใใใฎใฎใใใใ ใใงใฏใใใใใใฎ็จฎ้กใฎๅบๅฅใใงใใชใใ 1, 7 ใจ 3, 5 ใฏๅ ฑใซใใใใใใฎ้ใใฎๅใจๅๆฐใ็ญใใใใไธญ่บซใๅบๅฅใงใใชใ
ใใใง[* ใใใใใใ ใใ็ฎฑ่ฉฐใใๅงใใๆใๆฌกใฎ็ฎฑใฎๆๅใฎใใใใใใฎ็จฎ้ก ใ]ใ่ใใใจ่ฆ้ใใใใใชใใABC167 D - Teleporter (opens in a new tab) ใจๅใ่ฆ้ ใง่งฃใใใ
ๆๅใฎๅๆๅใฎ้จๅใฎ่ๅฟใงใใใใใใ ใใ็ฎฑ่ฉฐใใๅงใใๆใ็ฎฑใใใฃใฑใใซใชใใฎใฏไฝๅ็ฎใใจใใ่จ็ฎใฏๆ็ดใซใใใจ ใจใชใฃใฆใใพใ๏ผ ใฎๅ ดๅ๏ผใ ใใฎใใ ใ 1 ๅจๆๅใจใใฆใ ใ ใงๅฒใฃใไฝใใซๅฏพใใฆไบๅๆข็ดขใ่กใใใจใง้ซ้ใซๅๆๅใ่กใใใจใใงใใใ
ใพใใ่ใใใฆใใใฎใฏ็ฎฑใซใใใใใใใใใฎๅๆฐใชใฎใงๅฅ้ๆฑใใฆใใๅฟ ่ฆใใใใ
import bisect
import itertools
n, q, x = map(int, input().split())
w = list(map(int, input().split()))
k = [int(input()) for _ in range(q)]
# dob[p][i] = 2^pๅ็ฎใฎ็ฎฑใซๅฏพใใฆใi(mod N)็ช็ฎใฎใใใใใใใ่ฉฐใใฆใใฃใใจใใๆฌกใฎ็ฎฑใซๅ
ฅใใๆๅใฎใใใใใใฎ็ชๅท(mod N)
dob = [[0] * n for _ in range(41)]
# count[i] := i(mod N)็ช็ฎใฎใใใใใใใ่ฉฐใใฆใใๆใ่ใใใใพใงใซๅ
ฅใใใใใใใใฎๅๆฐ
count = [0] * n
sum_w = sum(w)
acc_ww = [0] + list(itertools.accumulate(w + w))
for i in range(n):
target = acc_ww[i] + x % sum_w
index = bisect.bisect_left(acc_ww, target)
dob[0][i] = index % n
count[i] = index - i + x // sum_w * n
for p in range(40):
for i in range(n):
dob[p + 1][i] = dob[p][dob[p][i]]
for k in k:
ans = 0
for shift in range(40):
if (k - 1 >> shift) & 1:
ans = dob[shift][ans]
print(count[ans])