Problem p02618 (python)

Fixed submission:
D = int(input())
for _ in range(D):
    print(1)

Buggy submission (verdict: Runtime Error):
# 焼きなまし（1000から線形に落とす）

from time import time
from math import exp
key = 1
rnd_mod = 12345678901234567891
rnd_x = 9876543210987654321 + 1234567890123456789 * key % rnd_mod
def rnd():
    global rnd_x
    rnd_x = rnd_x**2 % rnd_mod
    return (rnd_x>>10) % (1<<40)
def randrange(a, b=-1):
    if b > 0: return randrange(b-a) + a
    return rnd() % a

def calc():
    s = 0
    last = [-1] * 26
    for i, t in enumerate(A):
        s += S[i][t]
        last[t] = i
        for j, c in enumerate(C):
            s -= c * (i - last[j])
    return s

test_mode = False
ans_sum = 0
if test_mode:
    D = 365
    C = [randrange(101) for a in range(26)]
    S = [[randrange(20001) for a in range(26)] for _ in range(D)]
else:
    D = int(input())
    C = [int(a) for a in input().split()]
    S = [[int(a) for a in input().split()] for _ in range(D)]

for _ in range(5 if test_mode else 1):
    sTime = time()
    if test_mode:
        A = [randrange(26) for _ in range(D)]
    else:
        A = [randrange(26) for _ in range(D)]

    # A = [0] * D
    # A = [0, 16, 12, 13, 12]
    # A = [0, 16, 12, 13, 19]
    ans = calc()
    # print("ans =", ans)
    loop = 0
    ma = 0
    while 1:
        if loop % 100 == 0:
            ttt = time() - sTime
            if ttt > 1.8:
                break
            if test_mode:
                tt = loop / 35000
            else:
                tt = ttt / 1.8
            T = 4000 + -3800 * tt
            # T = 3000 * exp(-tt * 6)
        loop += 1
        d = randrange(D)
        i = A[d]
        for k in range(d)[::-1]:
            if A[k] == i:
                prev = k
                break
        else:
            prev = -1
        for k in range(d + 1, D):
            if A[k] == i:
                prev_ne = k
                break
        else:
            prev_ne = D
        for j in range(26):
            t = S[d][j] - S[d][i]
            for k in range(d)[::-1]:
                if A[k] == j:
                    new = k
                    break
            else:
                new = -1
            for k in range(d + 1, D):
                if A[k] == j:
                    new_ne = k
                    break
            else:
                new_ne = D
            t += C[j] * (d - new) * (new_ne - d) - C[i] * (d - prev) * (prev_ne - d)
            if (T < 1 and t > 0) or (T >= 1 and randrange(1 << 10) < exp(max(min(t / T, 0), -10)) * (1 << 10)):
                A[d] = j
                ans += t
                prev, prev_ne = new, new_ne
                i = j
                if ans > ma:
                    ma = ans
                    maA = A[:]
                if 0:
                    print("A =", A)
                    print("ans =", ans)
                    print("calc =", calc())

    A = maA[:]
    ans = ma
    if test_mode:
        ans_sum += ans
        print(ans)
        print("loop =", loop)
        # print("A =", A)
    else:
        print(*[a + 1 for a in A], sep = "\n")

if test_mode:
    print("average =", ans_sum / 5)

Buggy submission (verdict: Runtime Error):
from time import time
from math import exp
key = 1
rnd_mod = 12345678901234567891
rnd_x = 9876543210987654321 + 1234567890123456789 * key % rnd_mod
def rnd():
    global rnd_x
    rnd_x = rnd_x**2 % rnd_mod
    return (rnd_x>>10) % (1<<40)
def randrange(a, b=-1):
    if b > 0: return randrange(b-a) + a
    return rnd() % a

def calc():
    s = 0
    last = [-1] * 26
    for i, t in enumerate(A):
        s += S[i][t]
        last[t] = i
        for j, c in enumerate(C):
            s -= c * (i - last[j])
    return s

sTime = time()
test_mode = False

if test_mode:
    D = 365
    C = [randrange(101) for a in range(26)]
    S = [[randrange(20001) for a in range(26)] for _ in range(D)]
    A = [randrange(26) for _ in range(D)]
else:
    D = int(input())
    C = [int(a) for a in input().split()]
    S = [[int(a) for a in input().split()] for _ in range(D)]
    A = [randrange(26) for _ in range(D)]

# A = [0] * D
# A = [0, 16, 12, 13, 12]
# A = [0, 16, 12, 13, 19]
ans = calc()
# print("ans =", ans)
loop = 0
ma = 1500000
while 1:
    loop += 1
    if loop % 100 == 0 and time() - sTime > 1.8:
        break
    T = 3000 * max(1 - loop / 35000, 0.1)
    d = randrange(D)
    i = A[d]
    for k in range(d)[::-1]:
        if A[k] == i:
            prev = k
            break
    else:
        prev = -1
    for k in range(d + 1, D):
        if A[k] == i:
            prev_ne = k
            break
    else:
        prev_ne = D
    for j in range(26):
        t = S[d][j] - S[d][i]
        for k in range(d)[::-1]:
            if A[k] == j:
                new = k
                break
        else:
            new = -1
        for k in range(d + 1, D):
            if A[k] == j:
                new_ne = k
                break
        else:
            new_ne = D
        t += C[j] * (d - new) * (new_ne - d) - C[i] * (d - prev) * (prev_ne - d)
        if (T < 1 and t > 0) or (T >= 1 and randrange(1 << 10) < exp(max(min(t / T, 0), -10)) * (1 << 10)):
            A[d] = j
            ans += t
            prev, prev_ne = new, new_ne
            i = j
            if ans > ma:
                ma = ans
                maA = A[:]
            if 0:
                print("A =", A)
                print("ans =", ans)
                print("calc =", calc())
            
A = maA
ans = ma
if test_mode:
    print(ans)
    print("A =", A)
    print("loop =", loop)
else:
    print(*[a + 1 for a in A], sep = "\n")

Buggy submission (verdict: Runtime Error):
from time import time
from math import exp
key = 1
rnd_mod = 12345678901234567891
rnd_x = 9876543210987654321 + 1234567890123456789 * key % rnd_mod
def rnd():
    global rnd_x
    rnd_x = rnd_x**2 % rnd_mod
    return (rnd_x>>10) % (1<<40)
def randrange(a, b=-1):
    if b > 0: return randrange(b-a) + a
    return rnd() % a

def calc():
    s = 0
    last = [-1] * 26
    for i, t in enumerate(A):
        s += S[i][t]
        last[t] = i
        for j, c in enumerate(C):
            s -= c * (i - last[j])
    return s

sTime = time()
D = int(input())
C = [int(a) for a in input().split()]
S = [[int(a) for a in input().split()] for _ in range(D)]
A = [randrange(26) for _ in range(D)]
# A = [0] * D
# A = [0, 16, 12, 13, 12]
# A = [0, 16, 12, 13, 19]
ans = calc()
# print("ans =", ans)
while time() - sTime < 1.8:
    T = 1000 * max(1 - (time() - sTime) / 1.5, 0)
    d = randrange(D)
    i = A[d]
    for k in range(d)[::-1]:
        if A[k] == i:
            prev = k
            break
    else:
        prev = -1
    for k in range(d + 1, D):
        if A[k] == i:
            prev_ne = k
            break
    else:
        prev_ne = D
    for j in range(26):
        t = S[d][j] - S[d][i]
        for k in range(d)[::-1]:
            if A[k] == j:
                new = k
                break
        else:
            new = -1
        for k in range(d + 1, D):
            if A[k] == j:
                new_ne = k
                break
        else:
            new_ne = D
        t += C[j] * (d - new) * (new_ne - d) - C[i] * (d - prev) * (prev_ne - d)
        if (T < 1 and t > 0) or (T >= 1 and randrange(1 << 10) < exp(t / T) * (1 << 10)):
            A[d] = j
            ans += t
            prev, prev_ne = new, new_ne
            i = j
            if 0:
                print("A =", A)
                print("ans =", ans)
                print("calc =", calc())
            
            

print(*[a + 1 for a in A], sep = "\n")
if 0:
    print(ans)
    print("A =", A)