Problem:


Score : 400 points

Problem Statement

We have a string S of length N consisting of R, G, and B.

Find the number of triples (i,~j,~k)~(1 \leq i < j < k \leq N) that satisfy both of the following conditions:

S_i \neq S_j, S_i \neq S_k, and S_j \neq S_k.

j - i \neq k - j.

Constraints

1 \leq N \leq 4000

S is a string of length N consisting of R, G, and B.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of triplets in question.

Sample Input 1

4
RRGB

Sample Output 1

1

Only the triplet (1,~3,~4) satisfies both conditions. The triplet (2,~3,~4) satisfies the first condition but not the second, so it does not count.

Sample Input 2

39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB

Sample Output 2

1800



Sample input:
4
RRGB


Sample output:
1

Submission status: Runtime Error

Solution code:
import math
import sys
import collections
def main():
    N=int(input())
    Tr=tuple(input())
    R=[]
    G=[]
    B=[]
    for i in range(N):
       if Tr[i]=="R":
           R.append(i)
       if Tr[i]=="G":
           G.append(i)
       if Tr[i]=="B":
           B.append(i)
    cnt=0
    if len(R)==max(len(R),len(G),len(B))
        for i in B:
            for j in G:
                for k in R:
                    if max(i,j,k)-(i+j+k-max(i,j,k)-min(i,j,k))!=(i+j+k-max(i,j,k)-min(i,j,k))-min(i,j,k):
                        cnt+=1

        print(cnt)
    if len(G) == max(len(R), len(G), len(B))
        for i in B:
            for j in R:
                for k in G:
                    if max(i, j, k) - (i + j + k - max(i, j, k) - min(i, j, k)) != (
                            i + j + k - max(i, j, k) - min(i, j, k)) - min(i, j, k):
                        cnt += 1

        print(cnt)
    if len(B) == max(len(R), len(G), len(B))
        for i in R:
            for j in G:
                for k in B:
                    if max(i, j, k) - (i + j + k - max(i, j, k) - min(i, j, k)) != (
                            i + j + k - max(i, j, k) - min(i, j, k)) - min(i, j, k):
                        cnt += 1

        print(cnt)



if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import math
from itertools import combinations

def main():
    k = int(input())
    res = 0
    memo = [[0 for _ in range(k+1)] for _ in range(k+1)]
    for i ian range(1, k+1):
        for j in range(i, k+1):
            tmp = math.gcd(i,j)
            memo[i][j] = tmp
            memo[j][i] = tmp

    for i in range(1, k+1):
        res += i 

    for c in combinations(range(1,k+1),2):
        ab = memo[c[0], c[1]]
        res += ab * 6

    for c in combinations(range(1,k+1), 3):
        ab = memo[c[0], c[1]]
        abc = memo[ab, c[2]]
        res += abc * 6
    print(res)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import numpy as np

N = int(input())
S = np.array(list(input()))

R = np.arange(S.size)[S=='R']
G = np.arange(S.size)[S=='G']
B = np.arange(S.size)[S=='B']

i = 0      
for p in np.array(np.meshgrid(R, G, B)).T.reshape(-1,3):
    if len(set(np.diff(np.sort(p)))) == 1:
        continue
    i += 1
print(i)

Submission status: Runtime Error

Solution code:
from numba import njit
N = int(input())
S = input()
if N < 4:
    print(0)
    exit()
ans = 0
@njit
def solve(N, S):
    for i in range(N):
        si = S[i]
        for j in range(i+1, N):
            sj = S[j]
            if si == sj:
                continue
            t = j - i
            for k in range(j+1, N):
                if k == j + t:
                    continue
                sk = S[k]
                if sj != sk and si != sk:
                    ans += 1
    return ans
ans = solve(N, S)
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()

ans = S.count("R") * S.count("G") * S.count("B")

res = 0
for i in range(N-2):
    for d in range(1, (N-i)//2+1):
        if set(S[i], S[i+d], S[i+2*d]) == {"R", "G", "B"}:
            res += 1
ans -= res
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
s = list(input())
 
a = 0
 
for i in range(n):
  if s[i]=='R':
    for j in range(n):
      if s[j]=='G':
        for k in range(n):
          if s[k]=='B':
            if i<j<k:
              if (j-i)!=(k-j):
                a = a + 1
            elif j<k<i:
              if (k-j)!=(i-k):
                a = a + 1
            elif k<i<j:
              if abs(j-i)!=abs(k-i):
                a = a + 1
            elif j<i<k:
              if abs(j-i)!=abs(k-i):
                a = a + 1
            elif i<k<j:
              if abs(k-i)!=abs(k-j):
                a = a + 1
            elif k<j<i:
              if abs(j-i)!=abs(k-j):
                a = a + 1
          
print(a)
n = int(input())
s = list(input())

a = 0

for i in range(n):
  if s[i]=='R':
    for j in range(n):
      if s[j]=='G':
        for k in range(n):
          if s[k]=='B':
            if i<j<k:
              if (j-i)!=(k-j):
                a = a + 1
            elif i<k<j:
              if (k-i)!=(j-k):
                a = a + 1
            elif j<i<k:
              if (i-j)!=(k-i):
                a = a + 1
            elif j<k<i:
              if (k-j)!=(i-k):
                a = a + 1
            elif k<i<j:
              if (i-k)!=(j-i):
                a = a + 1
            elif k<j<i:
              if (j-k)!=(i-j):
                a = a + 1
          
print(a)

Submission status: Runtime Error

Solution code:
N=int(input())
S=input()

ans=0
f_R=0
f_G=0
f_B=0

flag=0
i=0
while flag==0:
    if S[i]=='R':
        f_R=i
        flag+=1
    i+=1
    
i=0
flag=0
while flag==0:
    if S[i]=='G':
        f_G=i
        flag+=1
    i+=1
i=0
flag=0
while flag==0:
    if S[i]=='B':
        f_B=i
        flag+=1
    i+=1


F=0
j=f_R+1
G_num=0
while j<N:
    if S[j]=='G':
        G_num=j
        F+=1
        break
    j+=1
k=G_num+1   
while k<N:
    if S[k]=='B' and (j-f_R)!=(k-j):
        F+=1
    k+=1
if F==2:
    ans+=1
F=0
j=f_R+1
B_num=0
while j<N:
    if S[j]=='B':
        B_num=j
        F+=1
        break
    j+=1
k=B_num+1
while k<N:
    if S[k]=='G' and (j-f_R)!=(k-j):
        F+=1
    k+=1
if F==2:
    ans+=1

F=0
j=f_G+1
R_num=0
while j<N:
    if S[j]=='R':
        R_num=j
        F+=1
        break
    j+=1
k=R_num+1   
while k<N:
    if S[k]=='B' and (j-f_G)!=(k-j):
        F+=1
    k+=1
if F==2:
    ans+=1
F=0
j=f_G+1
B_num=0
while j<N:
    if S[j]=='B':
        B_num=j
        F+=1
        break
    j+=1
k=B_num+1
while k<N:
    if S[k]=='R' and (j-f_G)!=(k-j):
        F+=1
    k+=1
if F==2:
    ans+=1


F=0
j=f_B+1
R_num=0
while j<N:
    if S[j]=='R':
        R_num=j
        F+=1
        break
    j+=1
k=R_num+1   
while k<N:
    if S[k]=='G' and (j-f_B)!=(k-j):
        F+=1
    k+=1
if F==2:
    ans+=1
F=0
j=f_G+1
G_num=0
while j<N:
    if S[j]=='G':
        G_num=j
        F+=1
        break
    j+=1
k=G_num+1
while k<N:
    if S[k]=='R' and (j-f_B)!=(k-j):
        F+=1
    k+=1    
if F==2:
    ans+=1
    
print(ans)

Submission status: Runtime Error

Solution code:
pr = 0
pg = 0
pb = 0
ret = 0
R = list()
G = list()
B = list()
for i in range(len(S)):
    if S[i] == 'R':
        R.append(i)
    elif S[i] == 'G':
        G.append(i)
    elif S[i] == 'B':
        B.append(i)
l = [R,G,B]

for r in range(len(R)):
    for g in range(len(G)):
        for b in range(len(B)):
            l = [R[r], G[g], B[b]]
            l.sort()
            if l[2] - l[1] != l[1] - l[0]:
                ret += 1
print(ret)

Submission status: Runtime Error

Solution code:

from numba import njit

n = int(input())
s = list(input())
r = []
g = []
b = []

for i in range(n):
    if s[i] == "R":
        r.append(i)
    elif s[i] == "G":
        g.append(i)
    else:
        b.append(i)


ans = 0


@jit
def aaa(n, s, r, g, b):
    ans=0
    for i in range(n):
        for j in range(i + 1, n):
            if s[i] == s[j]:
                continue
            if "R" not in [s[i], s[j]]:
                a = list(map(lambda x: x > j, r))
                ans += sum(a)
                if 2 * j - i in r:
                    ans -= 1
            elif "G" not in [s[i], s[j]]:
                a = list(map(lambda x: x > j, g))
                ans += sum(a)
                if 2 * j - i in g:
                    ans -= 1
            elif "B" not in [s[i], s[j]]:
                a = list(map(lambda x: x > j, b))
                ans += sum(a)
                if 2 * j - i in b:
                    ans -= 1
    return ans


ans = aaa(n, s, r, g, b)
print(ans)

Submission status: Runtime Error

Solution code:
#N = 39
#S = "RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB"
N = int(input())
S = input()
result = 0
while (a <= N -3):
    b = a + 1
    while (b <= N - 1):
        c = b + 1
        while (c <= N):
            if ((b - a) == (c - b)):
                c += 1
                continue
            if (S[a - 1] != S[b - 1] and S[a - 1] != S[c - 1] and S[b - 1] != S[c - 1]):
                result += 1
            c += 1
        b += 1
    a += 1
print(result)

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
a = s.count("R")
b = s.count("G")
c = s.count("B")
z = a*b*c
if z ==0:
  print(0)
else:
  R=[]
  G=[]
  B=[]
  for i in range(n):
    if s[i]=="R":
      R.append(i)
    elif s[i]=="G":
      G.append(i)
    else:
      B.append(i)
  x = 0
  for i in R:
    for j in G:
      for k in B:
        if 2*j==i+k or 2*i==j+k or 2*k==i+j:
          x+=1
print(z-x)

Submission status: Runtime Error

Solution code:
#200412D
N = int(input())
S = str(input())

Ans=0
for i in range(N-2):
    for j in range(N-i-2):
        for k in range(N-j-i-1):
            if i+j != i + i + j + k + 2:
                if S[i+1] != S[j+i+2]:
                    if S[i+1] != S[i+j+k+3]:
                        Ans += 1
print(int(Ans))

Submission status: Runtime Error

Solution code:
def get_diff_s(a, b):
    return list({'R', 'G', 'B'} - {a, b})[0]

n = int(input())
s = input()

r_cnt = 0
g_cnt = 0
b_cnt = 0

r_cnt_list = []
g_cnt_list = []
b_cnt_list = []
for si in s[::-1]:
    if si == 'R':
        r_cnt += 1
    elif si == 'G':
        g_cnt += 1
    else:
        b_cnt += 1
    r_cnt_list.append(r_cnt)
    g_cnt_list.append(g_cnt)
    b_cnt_list.append(b_cnt)

ans = 0
for i in range(n - 3):
    for j in range(i + 1, n - 1):
        if s[i] == s[j]:
            continue
        ds = get_diff_s(s[i], s[j])
        di = j - i
        if ds == 'R':
            ans += r_cnt_list[j + 1]
            if s[j + di] == 'R':
                ans -= 1
        if ds == 'G':
            ans += g_cnt_list[j + 1]
            if s[j + di] == 'G':
                ans -= 1
        if ds == 'B':
            ans += b_cnt_list[j + 1]
            if s[j + di] == 'B':
                ans -= 1

print(ans)

Submission status: Runtime Error

Solution code:
def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]

from math import comb

n=n0()
s=s0()
r=s.count("R")
g=s.count("G")
b=s.count("B")
ans=0
for i in range(1,(n-1)//2+1):
    for j in range(0,n-2*i):
        ans+=1
#         print(j,j+1,j+i*2)
        if s[j]==s[j+i]==s[j+2*i]:
            ans-=1
        elif s[j]==s[j+i] or s[j]==s[j+2*i] or s[j+i]==s[j+2*i]:
            ans-=1
            
ans+=comb(r,3)+comb(g,3)+comb(b,3)+comb(r,2)*(g+b)+comb(g,2)*(r+b)+comb(b,2)*(r+g)
print(comb(n,3)-ans)

Submission status: Runtime Error

Solution code:
N = int(input())
S = list(map(str, input()))
f=0
i_c = j_c = k_c =1
for i in range(N):
    for j in range(N):
        for k in range(N):
            if(k+1>j+1 and
               j+1>i+1 and
               j-i=k-j and
               S[i]!=S[j] and 
               S[i]!=S[k] and
               S[j]!=S[k]):
                    f+=1
 
print(f)

Submission status: Runtime Error

Solution code:

a=int(input())
moji=input()
r=[]
g=[]
b=[]
for i in range(a):
    if moji[i]=="R":
        r.append(i)
    elif moji[i]=="G":
        g.append(i)
    elif moji[i]=="B":
        b.append(i)
res=len(r)*len(g)*len(b)
if len(r)==0 or len(g)==0 or len(b)==0:
    print(0)
else:
    min_len={len(r):r,len(g):g,len(b):b}
    tmp=list(min_len.keys()).sort()
    r=min_len[tmp[0]]
    g=min_len[tmp[1]]
    b=min_len[tmp[2]]
    for i in r:
        for j in g:
            if -abs(i-j)+min(i,j) in b:
                res-=1
            if abs(i-j)+max(i,j) in b:
                res-=1
            if abs(i-j)%2==0 and  int(abs(i-j)/2)+min(i,j) in b:
                res-=1
    print(res)

Submission status: Runtime Error

Solution code:
def rest_col(s1,s2):
  if(s1=="R"):
    if(s2="R"):
      return -1
    elif(s2=="G"):
       return 2
    else:
       return 1
  if(s1=="G"):
    if(s2="G"):
      return -1
    elif(s2=="R"):
       return 2
    else:
       return 1
  if(s1=="B"):
    if(s2="B"):
      return -1
    elif(s2=="R"):
       return 1
    else:
       return 0

n=int(input())
s=input()
count=[[0,0,0] for i in range(n)]
for i in range(n):
  if(s[n-1-i]=="R"):
    count[n-1-i][0]+=1
  if(s[n-1-i]=="G"):
    count[n-1-i][1]+=1
  if(s[n-1-i]=="B"):
    count[n-1-i][2]+=1
ans=0
for a in range(n):
  for b in range(a,n):
    r=rest_color(s[a],s[b])
    if(r==-1):
      continue
    else:
      ans+=count[b+1][r]
print(ans)

Submission status: Runtime Error

Solution code:
def get_diff_s(a, b):
    return list({'R', 'G', 'B'} - {a, b})[0]

n = int(input())
s = input()

r_cnt = 0
g_cnt = 0
b_cnt = 0

r_cnt_list = []
g_cnt_list = []
b_cnt_list = []
for si in s[::-1]:
    if si == 'R':
        r_cnt += 1
    elif si == 'G':
        g_cnt += 1
    else:
        b_cnt += 1
    r_cnt_list.append(r_cnt)
    g_cnt_list.append(g_cnt)
    b_cnt_list.append(b_cnt)

ans = 0
for i in range(n - 3):
    for j in range(i + 1, n - 1):
        if s[i] == s[j]:
            continue
        ds = get_diff_s(s[i], s[j])
        di = j - i
        if ds == 'R':
            ans += r_cnt[j + 1]
            if s[j + di] == 'R':
                ans -= 1
        if ds == 'G':
            ans += g_cnt[j + 1]
            if s[j + di] == 'G':
                ans -= 1
        if ds == 'B':
            ans += b_cnt[j + 1]
            if s[j + di] == 'B':
                ans -= 1

print(ans)

Submission status: Runtime Error

Solution code:
a=int(input())
moji=input()
r=[]
g=[]
b=[]
for i in range(a):
    if moji[i]=="R":
        r.append(i)
    elif moji[i]=="G":
        g.append(i)
    elif moji[i]=="B":
        b.append(i)
res=len(r)*len(g)*len(b)
if len(r)==0 or len(g)==0 or len(b)==0:
    print(0)
else:
    min_len={len(r):r,len(g):g,len(b):b}
    tmp=list(min_len.keys()).sort()
    r=min_len[tmp[0]]
    g=min_len[tmp[1]]
    b=min_len[tmp[2]]
    for i in r:
        for j in g:
            if -abs(i-j)+min(i,j) in b:
                res-=1
            if abs(i-j)+max(i,j) in b:
                res-=1
            if abs(i-j)%2==0 and  int(abs(i-j)/2)+min(i,j) in b:
                res-=1
    print(res)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
X = int(input())
S = str(input())
a = 0

for i in range(X-1):
   for j in range(i+1,X):
      Sj = S[j]
      Si = S[i]
      if Sj != Si:
         z == j - i
         
         if X < i + j and S[j+i] == S[j]
           a = a + 1
           S2 = S[j:]
           S3 = S2.replace(Si,"").replace(Sj,"")
           a = a + S3.len()
print(a)

Submission status: Runtime Error

Solution code:
n = int(input())
s = list(input())

r = [i for i, x in enumerate(l_dup) if x == 'R']
g = [i for i, x in enumerate(l_dup) if x == 'G']
b = [i for i, x in enumerate(l_dup) if x == 'B']

c = 0

for k in r:
    for j in g:
        for i in b:
            a = list(k, j, i)
            a.sort
            if a[0] - a[1] != a[1] - a[2]:
                c += 1

print(c)

Submission status: Runtime Error

Solution code:
import bisect

def get_unique_list(seq):
    seen = []
    return [x for x in seq if x not in seen and not seen.append(x)]

n = int(input())
S = input()

R = []
G = []
B = []
count = 0

if n < 4:
  print(0)
  exit(0)

for i, s in enumerate(S):
  if s == "R":
    R.append(i)
  elif s == "G":
    G.append(i)
  elif s == "B":
    B.append(i)

if R == [] or G == [] or B == []:
  print(0)
  exit(0)

for r in R:
  for g in G:
    if r > g:
      i, j = g, r
    else:
      i, j = r, g

    diff = j - i
    small_B = bisect.bisect_left(B, i)
    mid_B = bisect.bisect_left(B, j) - bisect.bisect_right(B, i)
    large_B = len(B) - bisect.bisect_right(B, j)

    if B[bisect.bisect_left(B, i - diff)] == i - diff:
      count -= 1
    if bisect.bisect_left(B, j + diff) < len(B):
      if B[bisect.bisect_left(B, j + diff)] == j + diff:
        count -= 1
    if B[bisect.bisect_left(B, i + j / 2)] < len(B):
        if B[bisect.bisect_left(B, i + j / 2)] == i + j / 2:
          count -= 1

    count += small_B + mid_B + large_B

print(count)

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
l = len(S)
count=0
A=[]
B=[]
C=[]
#print(S[])
for i in range(l):
    if S[i] == "R":
        A.append(i+1)
    elif S[i] == "G":
        B.append(i+1)
    elif S[i] == "B":
        C.append(i+1)
for i in A:
    for j in B:
        for k in C:
            if abs(i-j) != abs(j-k) or abs(k-i) !=abs(j-i) or abs(i-k)!=abs(j-k)
            count+=1
print(count)

Submission status: Runtime Error

Solution code:
#200412D
N = int(input())
S = str(input())

Ans=0
for i in range(N-2):
    for j in range(N-i-2):
        for k in range(N-j-i):
            if i+j != i + i + j + k + 2:
                if S[i+1] != S[j+i+2]:
                    if S[i+1] != S[i+j+k+3]:
                        Ans += 1
print(int(Ans))

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
a = s.count("R")
b = s.count("G")
c = s.count("B")
z = a*b*c
if z ==0:
  print(0)
else:
  R=[]
  G=[]
  B=[]
  for i in range(n):
    if s[i]=="R":
      R.append(i)
    elif s[i]=="G":
      G.append(i)
    else:
      B.append(i)
  x = 0
  for i in R:
    for j in G:
      if (0<= 2*j-i <=n-1 and s[2*j-i] in B) or (0<= 2*i-j <=n-1 and s[2*i-j] in B):
          x += 1
print(z-x)

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()

pr = 0
pg = 0
pb = 0
ret = 0
R = list()
G = list()
B = list()
for i in range(len(S)):
    if S[i] == 'R':
        R.append(i)
    elif S[i] == 'G':
        G.append(i)
    elif S[i] == 'B':
        B.append(i)
l = [R,G,B]

for r in range(R):
    for g in range(G):
        for b in range(B):
            if abs(R[r] - G[g]) != abs(B[b] - G[g]) and abs(R[r] - B[b]) != abs(B[b] - G[g]):
                ret += 1
print(ret)

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
cnt = 0
for i in range(n-2):
    for j in range(i+1,n-1):
        if s[i] != s[j]:
            if (s[i]+s[j]).count('R') == 0:
                cnt += s[j+1:n].count('R')
                if s[j+i] == 'R' and j-i > n-j:
                    cnt -= 1
            elif (s[i]+s[j]).count('G') == 0:
                cnt += s[j+1:n].count('G')
                if s[j+i] == 'G' and j-i > n-j:
                    cnt -= 1
            else:
                cnt += s[j+1:n].count('B')
                if s[j+i] == 'B' and j-i > n-j:
                    cnt -= 1
print(cnt)

Submission status: Runtime Error

Solution code:
from numba import njit

n = int(input())
s = list(input())
r = []
g = []
b = []

for i in range(n):
    if s[i] == "R":
        r.append(i)
    elif s[i] == "G":
        g.append(i)
    else:
        b.append(i)


ans = 0


@njit
def aaa(n, s, r, g, b):
    ans=0
    for i in range(n):
        for j in range(i + 1, n):
            if s[i] == s[j]:
                continue
            if "R" not in [s[i], s[j]]:
                a = list(map(lambda x: x > j, r))
                ans += sum(a)
                if 2 * j - i in r:
                    ans -= 1
            elif "G" not in [s[i], s[j]]:
                a = list(map(lambda x: x > j, g))
                ans += sum(a)
                if 2 * j - i in g:
                    ans -= 1
            elif "B" not in [s[i], s[j]]:
                a = list(map(lambda x: x > j, b))
                ans += sum(a)
                if 2 * j - i in b:
                    ans -= 1
    return ans


ans = aaa(n, s, r, g, b)
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
s = str(input())
i, j, k = map(int, range(input()).split())
1 <= i < j < k <= n
j != (i+k)/2
if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
  print((i, j, k))

Submission status: Runtime Error

Solution code:
import sys
import numpy as np
input = sys.stdin.readline
 
N = int(input())
S = input()
ans = 0
 
def dfs(i):
    cnt = 0
    ls = []
    for j in range(i+1,N):
        if S[j] != S[i]:
            ls.append(j)
    for k in ls:
        for l in range(k+1,N):
            if S[k] != S[l] and S[l] != S[i] and(l-k != k-i):
                cnt += 1
    return cnt
 
ans = np.sum([dfs(m) for m in range(N)])
        
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
s = list(input())
r = [i for i, x in enumerate(s) if x == 'R']
g = [i for i, x in enumerate(s) if x == 'G']
b = [i for i, x in enumerate(s) if x == 'B']
ans = len(r)*len(g)*len(b)
num = 0
for i in r:
    for j in g:
        a = (i+j)/2
        b = 2*i-j
        c = 2*j-i
        d =  [a, b, c]
        li = set(d) & set(k)
        num += len(list(li))
            
print(ans-num)

Submission status: Runtime Error

Solution code:
def calc(first, second, last_li, last):
    ans = 0
    for i in range(len(S)):
        if S[i] == first:
            for j in range(i+1, len(S)):
                if S[j] == second:
                    ans += (last_li+[0])[j+1]
                    if j+j-i < len(Bs) and S[j+j-i] == last:
                        ans -= 1
    return ans

# print('input >>')
N = int(input())
S = input()

Rs_add = [0] * len(S)
for i in reversed(range(len(Rs_add)-1)):
    if S[i] == 'R':
        Rs_add[i] = Rs_add[i+1] + 1
    else:
        Rs_add[i] = Rs_add[i+1]
Gs_add = [0] * len(S)
for i in reversed(range(len(Gs_add)-1)):
    if S[i] == 'G':
        Gs_add[i] = Gs_add[i+1] + 1
    else:
        Rs_add[i] = Rs_add[i+1]
Bs_add = [0] * len(S)
for i in reversed(range(len(Bs_add)-1)):
    if S[i] == 'B':
        Bs_add[i] = Bs_add[i+1] + 1
    else:
        Bs_add[i] = Bs_add[i+1]

"""
Rs = [1 if i == 'R' else 0 for i in S]
Gs = [1 if i == 'G' else 0 for i in S]
Bs = [1 if i == 'B' else 0 for i in S]

Rs_add = [0] * len(S)
Rs_add[0] = sum(Rs)
Gs_add = [0] * len(S)
Gs_add[0] = sum(Gs)
Bs_add = [0] * len(S)
Bs_add[0] = sum(Bs)

for i in range(len(Rs)-1):
    if Rs[i] == 1:
        Rs_add[i+1] = Rs_add[i]-1
    else:
        Rs_add[i+1] = Rs_add[i]
for i in range(len(Gs)-1):
    if Gs[i] == 1:
        Gs_add[i+1] = Gs_add[i]-1
    else:
        Gs_add[i+1] = Gs_add[i]
for i in range(len(Bs)-1):
    if Bs[i] == 1:
        Bs_add[i+1] = Bs_add[i]-1
    else:
        Bs_add[i+1] = Bs_add[i]
"""


ans = 0

ans += calc('R', 'G', Bs_add, 'B')
ans += calc('G', 'R', Bs_add, 'B')
ans += calc('R', 'B', Gs_add, 'G')
ans += calc('B', 'R', Gs_add, 'G')
ans += calc('G', 'B', Rs_add, 'R')
ans += calc('B', 'G', Rs_add, 'R')


# print('-----output-----')
print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
s=input()
k=[i for i in range(1,n+1)]
from itertools import combinations
c=list(combinations(k,3))
ans=0
for i in range(len(c)):
  p=int(c[i][0])
  q=int(c[i][1])
  r=int(c[i][2])
  if s[p]!=s[q] and s[q]!=s[r] and s[p]!=s[r] and q-p!=r-q:
    ans=ans+1
print(ans)

Submission status: Runtime Error

Solution code:
def get_diff_s(a, b):
    return list({'R', 'G', 'B'} - {a, b})[0]

n = int(input())
s = input()

r_cnt = 0
g_cnt = 0
b_cnt = 0

r_cnt_list = []
g_cnt_list = []
b_cnt_list = []
for si in s[::-1]:
    if si == 'R':
        r_cnt += 1
    elif si == 'G':
        g_cnt += 1
    else:
        b_cnt += 1
    r_cnt_list.append(r_cnt)
    g_cnt_list.append(g_cnt)
    b_cnt_list.append(b_cnt)

ans = 0
for i in range(n - 3):
    for j in range(i + 1, n - 1):
        if s[i] == s[j]:
            continue
        ds = get_diff_s(s[i], s[j])
        di = j - i
        if ds == 'R':
            ans += r_cnt[j + 1]
            if s[j + di] == 'R':
                ans -= 1
        if ds == 'G':
            ans += g_cnt[j + 1]
            if s[j + di] == 'G':
                ans -= 1
        if ds == 'B':
            ans += b_cnt[j + 1]
            if s[j + di] == 'B':
                ans -= 1

print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
s=input()
ans=0
r=g=b=0
R=[0 for _ in range(n)]
G=[0 for _ in range(n)]
B=[0 for _ in range(n)]
for i in range(i):
    if s[i]='R':r+=1
    elif s[i]='G':g+=1
    else:b+=1
    R[i]=r
    G[i]=g
    B[i]=b
for i in range(n):
    if s[i]='R':ans+=G[i]*(B[-1]-B[i])+B[i]*(G[-1]-G[i])
    elif s[i]='G':ans+=R[i]*(B[-1]-B[i])+B[i]*(R[-1]-R[i])
    else:ans+=R[i]*(G[-1]-G[i])+G[i]*(R[-1]-R[i])
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
S = list(input())
import itertools as itr

A = {'R':[] ,'G':[] ,'B':[]}
sum = 0

for i in range(N):
    A[S[i]].append(i+1)



for i,j,k itr.product(A['R'],A['B'],A['G']):
            if not i-j == k-j:
                l = [i,j,k]
                l.sort()
                if not l[1] - l[0] == l[2] - l[1]:
                    sum += 1

print(sum)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 12 22:06:39 2020

@author: H_hoshigi
"""
N = 4#int(input())
S = "RRGB"#input()

R_sum_list = [0]
G_sum_list = [0]
B_sum_list = [0]
R_sum = 0
G_sum = 0
B_sum = 0
for i in range(N):
    if S[-(i+1)] == "R":
        R_sum += 1
    elif S[-(i+1)] == "G":
        G_sum += 1
    else:
        B_sum += 1
    R_sum_list.append(R_sum)
    G_sum_list.append(G_sum)
    B_sum_list.append(B_sum)
R_sum_list = R_sum_list[::-1]
G_sum_list = G_sum_list[::-1]
B_sum_list = B_sum_list[::-1]


alphabet_list_list = [["R", "G", "B"],
                      ["R", "B", "G"],
                      ["G", "R", "B"],
                      ["G", "B", "R"],
                      ["B", "R", "G"],
                      ["B", "G", "R"],
                      ]
answer = 0
for a in range(6):
    for i in range(N):
        if S[i] == alphabet_list_list[a][0]:
            for j in range(i+1, N):
                if S[j] == alphabet_list_list[a][1]:
                    kosuu = 0
                    if alphabet_list_list[a][2] == "R":
                        kosuu = R_sum_list[N-j-1]
                        if S[2*j - i] == "R":
                            kosuu -= 1
                    if alphabet_list_list[a][2] == "G":
                        kosuu = G_sum_list[N-j-1]
                        if S[2*j - i] == "G":
                            kosuu -= 1
                    if alphabet_list_list[a][2] == "B":
                        kosuu = B_sum_list[N-j-1]
                        if S[2*j - i] == "B":
                            kosuu -= 1
                    answer += kosuu
print(answer)

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()

cnt = 0
for i in range(1, n-2):
    for i in range(i+1, n-1):
        for k in range(j+1,n)
        if k != j * 2 - i:
            if s[i-1] != s[j-1] and s[i-1] != s[k-1] and s[j-1] != s[k-1]:
                cnt += 1

print(cnt)

Submission status: Runtime Error

Solution code:
count = 0
N = int(input())
S = list(input())
# for i in range(N):
#     for j in range(i+1, N):
#         for k in range(j+1, N):
#             if(S[i] != S[j] and S[i] != S[k] and S[j] != S[k] and (j - i) != (k - j)):
#                 count+=1
# print(count)

countSecond = 1
countThird = 1
for i in range(N):
    while(S[i] == S[i+countSecond]):
        if(countSecond < N):
            countSecond+=1
        else:
            break
            
    while(S[i+countSecond] == S[i + countThird]):
        if(countThird < N):
            countThird += 1
        else:
            break
    if((countSecond - i) != (countThird - i)):
        count+=1
print(count)

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
def main():
    ans = 0
    color_list=["R","G","B"]
    for i in range(n):
      color_list.remove(s[i])
      	for j in range(i+1, n):
            if s[j] in color_list:
              color_list.remove(s[j])
                for k in range(j+1, n):
                    if s[k] in color_list:
                        ans +=1
                        color_list=["R","G","B"]
            else:
              color_list=["R","G","B"]
              
                    
               

    return ans

if __name__ == '__main__':
  print(main())

Submission status: Runtime Error

Solution code:
N=int(input())
S=str(input())
S=list(S)
a=0
if 1<=N<=4000:
    if N<=3:
        print(0)
    else:
        i=1
        j=2
        k=3
        for k in range(3,N+1):
            for j in range(2,k):
                for i in range(1,j):
                    if (S[i]==S[j])and(S[j]==S[k]):
                        if j-i==k-j:
                            continue
                        else:
                            a+=1
                    i+=1
                j+=1
            k+=1
        print(a)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(2147483647)
def main():
    N = int(input())
    S = input().rstrip("\r\n")
    R = []
    G = []
    B = []
    for i, s in enumerate(S):
        if s == "R":
            R.append(i)
        elif s == "G":
            G.append(i)
        else:
            B.append(i)
    ans = len(R) * len(G) * len(B)
    for i in range(len(R)):
        for j in range(len(G)):
            diff = G[j] - R[i]
            left = 0
            right = len(B)
            exists_same = False
            while left < right:
                m = (left + right) // 2
                if B[m] - G[j] == diff:
                    exists_same = True
                    break
                elif B[m] - G[j] > diff:
                    right = m
                elif B[m] - G[j] < diff:
                    left = m + 1
            if exists_same:
                ans -= 1
    for i in range(len(R)):
        for j in range(len(B)):
            diff = B[j] - R[i]
            left = 0
            right = len(G)
            exists_same = False
            while left < right:
                m = (left + right) // 2
                if G[m] - B[j] == diff:
                    exists_same = True
                    break
                elif G[m] - B[j] > diff:
                    right = m
                elif G[m] - B[j] < diff:
                    left = m + 1
            if exists_same:
                ans -= 1
    for i in range(len(B)):
        for j in range(len(G)):
            diff = B[j] - R[i]
            left = 0
            right = len(R)
            exists_same = False
            while left < right:
                m = (left + right) // 2
                if R[m] - B[j] == diff:
                    exists_same = True
                    break
                elif R[m] - B[j] > diff:
                    right = m
                elif R[m] - B[j] < diff:
                    left = m + 1
            if exists_same:
                ans -= 1
    print(ans)
if __name__ == "__main__":
    main()