0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ABC220 C - Long Sequence を解いた

Last updated at Posted at 2021-10-19

abc220c_1.png
abc220c_2.png
abc220c_3.png

X を超えるた項数であることに注意。

abc220c.py
N = int(input())
A = list(map(int,input().split()))
X = int(input())

base = sum(A)
cnt = X//base
ans = 0
#print(cnt)
if cnt == 0:
    for i in range(N):
        ans += A[i]
        if ans > X:
            print(i+1)
            exit()
else:
    num = X%base
    #print(cnt,num)
    for i in range(N):
        ans += A[i]
        if ans > X-base*cnt:
            print(N*cnt+i+1)
            exit()

もう少し簡潔に書いてみよう。

abc220c.py
def solv():
    N = int(input())
    A = list(map(int, input().split()))
    X = int(input())

    cnt = X // sum(A)
    num = sum(A) * cnt

    if cnt == 0:
        buf = 0
        for i in range(N):
            buf += A[i]
            if buf > X:
                print(i + 1)
                break
    else:
        buf = num
        for i in range(N):
            buf += A[i]
            if buf > X:
                print(i + 1 + cnt * N)
                break
solv()#52ms

cnt の数で場合分けしてるけど、
不要じゃね!?っと気付く。

abc220c.py
def solv():
    N = int(input())
    A = list(map(int,input().split()))
    X = int(input())
    cnt = X//sum(A)
    num = sum(A)*cnt
    buf = num
    for i in range(N):
        buf += A[i]
        if buf > X:
            print(i+1+cnt*N)
            break
solv()#56ms

書きながら自分の考えを整理し、ココはまでは大丈夫と
自分をなだめながら本番に臨んでいる自分からすると前者の方が精神的にストレスが少なくて良い。
もっと実力が付けばサクッと後者の記述で切り抜けられるのだろう。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?