Problem p03291 (python)

Fixed submission:
s = input()
a, d = [0], [0]
for char in s[:-1]:
    if char == 'A':
        a.append(a[-1] + 1)
    else:
        a.append(a[-1])
    if char == '?':
        d.append(d[-1] + 1)
    else:
        d.append(d[-1])

c, e = [0], [0]
for char in s[::-1][:-1]:
    if char == 'C':
        c.append(c[-1] + 1)
    else:
        c.append(c[-1])
    if char == '?':
        e.append(e[-1] + 1)
    else:
        e.append(e[-1])
c = c[::-1]
e = e[::-1]

ret = 0
mod = 10 ** 9 + 7
for char, aa, dd, cc, ee in zip(s, a, d, c, e):
    if char in 'B?':
        tmp1 = aa * pow(3, dd, mod)
        if dd != 0:
            tmp1 += dd * pow(3, dd - 1, mod)

        tmp2 = cc * pow(3, ee, mod)
        if ee != 0:
            tmp2 += ee * pow(3, ee - 1, mod)

        ret += tmp1 * tmp2
        ret %= mod

print(ret)

Buggy submission (verdict: Runtime Error):
s = input()

def get_sum(J, J3, I, L, L3, K):
    total1 = int(I * J3 * 3 + J * J3)%  (10 ** 9 + 7)
    total2 = int(K * L3 * 3 + L * L3)%  (10 ** 9 + 7)
    return total1 * total2

n_q = 0
n_c = 0
for i in range(len(s)):
    if s[i] == '?':
        n_q += 1
    elif s[i] == 'C':
        n_c += 1

n_q_right = [0]
n_a_right = [0]
n_c_right = [0]
for i in range(len(s) - 1):
    if s[i] == '?':
        n_q_right.append(n_q_right[-1] + 1)
        n_a_right.append(n_a_right[-1])
        n_c_right.append(n_c_right[-1])
    elif s[i] == 'A':
        n_q_right.append(n_q_right[-1])
        n_a_right.append(n_a_right[-1] + 1)
        n_c_right.append(n_c_right[-1])
    elif s[i] == 'C':
        n_q_right.append(n_q_right[-1])
        n_a_right.append(n_a_right[-1])
        n_c_right.append(n_c_right[-1] + 1)
    elif s[i] == 'B':
        n_q_right.append(n_q_right[-1])
        n_a_right.append(n_a_right[-1])
        n_c_right.append(n_c_right[-1])

total = 0
P3_right = 1/3.0
P3_left = 3 ** (n_q - 1)
for i in range(len(s)):
    if s[i] == '?':
        P3_left /= 3
        _n_q_left = n_q - n_q_right[i] - 1
        _n_c_left = n_c - n_c_right[i]
        total += get_sum(n_q_right[i], P3_right, n_a_right[i], _n_q_left, P3_left, _n_c_left)
        P3_right = P3_right * 3  % (10 ** 9 + 7)
    elif s[i] == 'B':
        _n_q_left = n_q - n_q_right[i]
        _n_c_left = n_c - n_c_right[i]
        total += get_sum(n_q_right[i], P3_right, n_a_right[i], _n_q_left, P3_left, _n_c_left)



print(total % (10 ** 9 + 7))

Buggy submission (verdict: Runtime Error):
s = input()

def get_sum(J, J3, I, L, L3, K):
    total1 = int(I * J3 + J * J3 / 3)%  (10 ** 9 + 7)
    total2 = int(K * L3 + L * L3 / 3)%  (10 ** 9 + 7)
    return total1 * total2

n_q = 0
n_c = 0
for i in range(len(s)):
    if s[i] == '?':
        n_q += 1
    elif s[i] == 'C':
        n_c += 1

n_q_right = [0]
n_a_right = [0]
n_c_right = [0]
for i in range(len(s) - 1):
    if s[i] == '?':
        n_q_right.append(n_q_right[-1] + 1)
        n_a_right.append(n_a_right[-1])
        n_c_right.append(n_c_right[-1])
    elif s[i] == 'A':
        n_q_right.append(n_q_right[-1])
        n_a_right.append(n_a_right[-1] + 1)
        n_c_right.append(n_c_right[-1])
    elif s[i] == 'C':
        n_q_right.append(n_q_right[-1])
        n_a_right.append(n_a_right[-1])
        n_c_right.append(n_c_right[-1] + 1)
    elif s[i] == 'B':
        n_q_right.append(n_q_right[-1])
        n_a_right.append(n_a_right[-1])
        n_c_right.append(n_c_right[-1])

total = 0
P3_right = 1
P3_left = 3 ** n_q
for i in range(len(s)):
    if s[i] == '?':
        P3_left /= 3
        _n_q_left = n_q - n_q_right[i] - 1
        _n_c_left = n_c - n_c_right[i]
        total += get_sum(n_q_right[i], P3_right, n_a_right[i], _n_q_left, P3_left, _n_c_left) %  (10 ** 9 + 7)
        P3_right = P3_right * 3  % (10 ** 9 + 7)
    elif s[i] == 'B':
        _n_q_left = n_q - n_q_right[i]
        _n_c_left = n_c - n_c_right[i]
        total += get_sum(n_q_right[i], P3_right, n_a_right[i], _n_q_left, P3_left, _n_c_left) %  (10 ** 9 + 7)



print(total % (10 ** 9 + 7))

Buggy submission (verdict: Runtime Error):
s = input()

def get_sum(J, J3, I, L, L3, K):
    total1 = int(I * J3 + J * J3 / 3)
    total2 = int(K * L3 + L * L3 / 3)
    return total1 * total2

n_q = 0
n_c = 0
for i in range(len(s)):
    if s[i] == '?':
        n_q += 1
    elif s[i] == 'C':
        n_c += 1

n_q_right = [0]
n_a_right = [0]
n_c_right = [0]
for i in range(len(s) - 1):
    if s[i] == '?':
        n_q_right.append(n_q_right[-1] + 1)
        n_a_right.append(n_a_right[-1])
        n_c_right.append(n_c_right[-1])
    elif s[i] == 'A':
        n_q_right.append(n_q_right[-1])
        n_a_right.append(n_a_right[-1] + 1)
        n_c_right.append(n_c_right[-1])
    elif s[i] == 'C':
        n_q_right.append(n_q_right[-1])
        n_a_right.append(n_a_right[-1])
        n_c_right.append(n_c_right[-1] + 1)
    elif s[i] == 'B':
        n_q_right.append(n_q_right[-1])
        n_a_right.append(n_a_right[-1])
        n_c_right.append(n_c_right[-1])

total = 0
P3_right = 1
P3_left = 3 ** n_q
for i in range(len(s)):
    if s[i] == '?':
        P3_left /= 3
        _n_q_left = n_q - n_q_right[i] - 1
        _n_c_left = n_c - n_c_right[i]
        total += get_sum(n_q_right[i], P3_right, n_a_right[i], _n_q_left, P3_left, _n_c_left) %  (10 ** 9 + 7)
        P3_right = P3_right * 3  % (10 ** 9 + 7)
    elif s[i] == 'B':
        _n_q_left = n_q - n_q_right[i]
        _n_c_left = n_c - n_c_right[i]
        total += get_sum(n_q_right[i], P3_right, n_a_right[i], _n_q_left, P3_left, _n_c_left) %  (10 ** 9 + 7)



print(total % (10 ** 9 + 7))

Buggy submission (verdict: Runtime Error):
s = input()

def get_sum(J, J3, I, L, L3, K):
    total1 = int(I * J3 + J * J3 / 3)
    total2 = int(K * L3 + L * L3 / 3)
    return total1 * total2

n_q = 0
n_c = 0
for i in range(len(s)):
    if s[i] == '?':
        n_q += 1
    elif s[i] == 'C':
        n_c += 1

n_q_right = [0]
n_a_right = [0]
n_c_right = [0]
for i in range(len(s) - 1):
    if s[i] == '?':
        n_q_right.append(n_q_right[-1] + 1)
        n_a_right.append(n_a_right[-1])
        n_c_right.append(n_c_right[-1])
    elif s[i] == 'A':
        n_q_right.append(n_q_right[-1])
        n_a_right.append(n_a_right[-1] + 1)
        n_c_right.append(n_c_right[-1])
    elif s[i] == 'C':
        n_q_right.append(n_q_right[-1])
        n_a_right.append(n_a_right[-1])
        n_c_right.append(n_c_right[-1] + 1)
    elif s[i] == 'B':
        n_q_right.append(n_q_right[-1])
        n_a_right.append(n_a_right[-1])
        n_c_right.append(n_c_right[-1])

total = 0
P3_right = 1
P3_left = 3 ** n_q
for i in range(len(s)):
    if s[i] == '?':
        P3_left /= 3
        _n_q_left = n_q - n_q_right[i] - 1
        _n_c_left = n_c - n_c_right[i]
        total += get_sum(n_q_right[i], P3_right, n_a_right[i], _n_q_left, P3_left, _n_c_left) %  (10 ** 9 + 7)
        P3_right *= 3
    elif s[i] == 'B':
        _n_q_left = n_q - n_q_right[i]
        _n_c_left = n_c - n_c_right[i]
        total += get_sum(n_q_right[i], P3_right, n_a_right[i], _n_q_left, P3_left, _n_c_left) %  (10 ** 9 + 7)



print(total % (10 ** 9 + 7))

Buggy submission (verdict: Runtime Error):
s = input()

def get_sum(J, J3, I, L, L3, K):
    total1 = int(I * J3 + J * J3 / 3) % (10 ** 9 + 7)
    total2 = int(K * L3 + L * L3 / 3) % (10 ** 9 + 7)
    return total1 * total2

n_q = 0
n_c = 0
for i in range(len(s)):
    if s[i] == '?':
        n_q += 1
    elif s[i] == 'C':
        n_c += 1

n_q_right = [0]
n_a_right = [0]
n_c_right = [0]
for i in range(len(s) - 1):
    if s[i] == '?':
        n_q_right.append(n_q_right[-1] + 1)
        n_a_right.append(n_a_right[-1])
        n_c_right.append(n_c_right[-1])
    elif s[i] == 'A':
        n_q_right.append(n_q_right[-1])
        n_a_right.append(n_a_right[-1] + 1)
        n_c_right.append(n_c_right[-1])
    elif s[i] == 'C':
        n_q_right.append(n_q_right[-1])
        n_a_right.append(n_a_right[-1])
        n_c_right.append(n_c_right[-1] + 1)
    elif s[i] == 'B':
        n_q_right.append(n_q_right[-1])
        n_a_right.append(n_a_right[-1])
        n_c_right.append(n_c_right[-1])

total = 0
_n_q_right_3 = 1
_n_q_left_3 = 3 ** n_q
for i in range(len(s)):
    if s[i] == '?':
        _n_q_left_3 /= 3
        total += get_sum(n_q_right[i], _n_q_right_3, n_a_right[i], n_q - n_q_right[i] - 1, _n_q_left_3, n_c - n_c_right[i])
        _n_q_right_3 *= 3
    elif s[i] == 'B':
        total += get_sum(n_q_right[i], _n_q_right_3, n_a_right[i], n_q - n_q_right[i], _n_q_left_3, n_c - n_c_right[i])

print(total % (10 ** 9 + 7))

Buggy submission (verdict: Runtime Error):
s = input()

n_q = 0
n_c = 0
for i in range(len(s)):
    if s[i] == '?':
        n_q += 1
    elif s[i] == 'C':
        n_c += 1

n_q_right = [0]
n_a_right = [0]
n_c_right = [0]
for i in range(len(s) - 1):
    if s[i] == '?':
        n_q_right.append(n_q_right[-1] + 1)
        n_a_right.append(n_a_right[-1])
        n_c_right.append(n_c_right[-1])
    elif s[i] == 'A':
        n_q_right.append(n_q_right[-1])
        n_a_right.append(n_a_right[-1] + 1)
        n_c_right.append(n_c_right[-1])
    elif s[i] == 'C':
        n_q_right.append(n_q_right[-1])
        n_a_right.append(n_a_right[-1])
        n_c_right.append(n_c_right[-1] + 1)
    elif s[i] == 'B':
        n_q_right.append(n_q_right[-1])
        n_a_right.append(n_a_right[-1])
        n_c_right.append(n_c_right[-1])

total = 0
Jp3 = 1/3.0
Lp3 = 3 ** (n_q - 1)
for i in range(len(s)):
    if s[i] == '?':
        Lp3 /= 3
        total1 = int((n_a_right[i] * 3 + n_q_right[i]) * Jp3)  %  (10 ** 9 + 7)
        total2 = int(((n_c - n_c_right[i]) * 3 + n_q - n_q_right[i] - 1) * Lp3)  %  (10 ** 9 + 7)
        total += total1 * total2
        Jp3 *= 3

    elif s[i] == 'B':
        total1 = int((n_a_right[i] * 3 + n_q_right[i]) * Jp3)  %  (10 ** 9 + 7)
        total2 = int(((n_c - n_c_right[i]) * 3 + n_q - n_q_right[i]) * Lp3)  %  (10 ** 9 + 7)
        total += total1 * total2

print(total % (10 ** 9 + 7))