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 numpy

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):
    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 = input()

if S.count("R") <= S.count("G") and S.count("R") <= S.count("B"):
  mins = "R"
  if S.count("G") <= S.count("B"):
    spl = "G"
  else:
    spl = "B"
if S.count("G") <= S.count("R") and S.count("G") <= S.count("B"):
  mins = "G"
  if S.count("R") <= S.count("B"):
    spl = "R"
  else:
    spl = "B"
if S.count("B") <= S.count("R") and S.count("B") <= S.count("G"):
  mins = "B"
  if S.count("R") <= S.count("G"):
    spl = "R"
  else:
    spl = "G"
    
ss = max(set(S.replace(mins," ").split(spl)))

start = S.index(ss)
end = start + len(ss) - 1

i,j,k = 1,2,3
if i == start+1:
  j = end+2
  k = end+3
if j == start+1:
  k = end+2

res = 0

while True:
  if S[i-1] != S[j-1] and S[i-1] != S[k-1] and S[j-1] != S[k-1] and j-i != k-j:
  	res += 1
  if not k == len(S):
    k += 1
  elif not j == len(S)-1:
    j += 1
    if not j == start+1:
      k = j+1
    else:
      k = end+2
  elif not i == len(S)-2:
    i += 1
    if not i == start+1:
   	  j = i+1
      k = j+1
    else:
      j = end+2
      k = end+3
  else:
    break

print(res)

Submission status: Runtime Error

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

total = n * (n-1) * (n-2)

cnt = 0

for i in range(1, n+1):
    for j in range(i+1, n+1):
        for k in range(j+1, n+1):
        if s[i-1] == s[j-1]:
            break
        if j-i != k-j: 
            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:
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 = list(input())

a = S[0]
res= 0

for i in range(1, N-2):
    if a == S[i]:
        b = S[i]
        for j in range(j, N-1):
            if i == j:
                continue
            c = S[j]
            if a!=b and b!= c and a!=c:
                res +=1

print(res)

Submission status: Runtime Error

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

for i in range(N-2):
    for j in range(i+1,N-1):
        if S[i]!=S[j]:
            for k in range(1,2*j-i):
                if S[j]!=S[k] and S[k]!=S[i]:
                    ans+=1
            for k in range(2*j-i+1,N):
                if S[j]!=S[k] and S[k]!=S[i]:
                    ans+=1
         
                
print(ans)

Submission status: Runtime Error

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

def f(N, S):
    d = {}
    for c in S:
        if c not in d:
            d[c] = 1
        else:
            d[c] += 1

    if d["R"] == 0 or d["G"] == 0 or d["B"] == 0:
        return 0

    total = d["R"] * d["G"] * d["B"]


    dame = 0
    for i in range(1, N - 1):
        for j in range(i + 1, N):
            k = 2 * j - i
            if j < k < N+1 and S[i-1] != S[j-1] and S[j-1] != S[k-1] and S[i-1] != S[k-1]:
                    dame += 1

    return (total - dame)

val = f(N, S)
print(val)

Submission status: Runtime Error

Solution code:
N = int(input())
lis = str(input())
a = []
b = []
for pair in itertools.combinations(lis, 3):
    #if(pair[0]!=pair[1] and pair[1]!=pair[2] and pair[2]!=pair[0]):
    a.append(list(pair))
    print(list(pair))

lis_num = range(len(lis))
for i in itertools.combinations(lis_num, 3):
    b.append(list(i))
    print(i)

count = 0
for i in range(len(a)):
    if (a[i][0]!=a[i][1] and a[i][1]!=a[i][2] and a[i][2] != a[i][0] and b[i][0]!=b[i][1] and b[i][1] != b[i][2]):
        count += 1
print(count)

Submission status: Runtime Error

Solution code:

n = int(input())
s = input()
comb = combinations(list(range(0, n)), 3)
ans = 0
for i, j, k in list(comb):
    if s[i] != s[j] and s[i] != s[k] and s[j] != s[k] and j - i != k - j:
        ans += 1

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=c[i][0]
  q=c[i][1]
  r=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:
from bisect import bisect_right,bisect_left
N = int(input())
S = input().strip()
R = []
G = []
B = []
Ron = [0 for _ in range(len(S))]
Gon = [0 for _ in range(len(S))]
Bon = [0 for _ in range(len(S))]
for i in range(N):
    if S[i]=="R":
        R.append(i)
        Ron[i]=1
    elif S[i]=="G":
        G.append(i)
        Gon[i]=1
    else:
        B.append(i)
        Bon[i]=1
cnt = 0
for i in range(N):
    if i in R:
        #RGB
        indr = i
        ind0 = bisect_right(G,indr)
        for j in range(ind0,len(G)):
            indg = G[j]
            ind1 = bisect_right(B,indg)
            if Bon[2*indg-indr]==0:
                cnt += len(B)-ind1
            else:
                cnt += len(B)-ind1-1
        #RBG
        ind0 = bisect_right(B,indr)
        for j in range(ind0,len(B)):
            indb = B[j]
            ind1 = bisect_right(G,indb)
            if Gon[2*indb-indr]==0:
                cnt += len(G)-ind1
            else:
                cnt += len(G)-ind1-1
    elif i in G:
        #GBR
        indg = i
        ind0 = bisect_right(B,indg)
        for j in range(ind0,len(B)):
            indb = B[j]
            ind1 = bisect_right(R,indb)
            if Ron[2*indb-indg]==0:
                cnt += len(R)-ind1
            else:
                cnt += len(R)-ind1-1
        #GRB
        ind0 = bisect_right(R,indg)
        for j in range(ind0,len(R)):
            indr = R[j]
            ind1 = bisect_right(B,indr)
            if Bon[2*indr-indg]==0:
                cnt += len(B)-ind1
            else:
                cnt += len(B)-ind1-1
    elif i in B:
        #BRG
        indb = i
        ind0 = bisect_right(R,indb)
        for j in range(ind0,len(R)):
            indr = R[j]
            ind1 = bisect_right(G,indr)
            if Gon[2*indr-indb]==0:
                cnt += len(G)-ind1
            else:
                cnt += len(G)-ind1-1
        #BGR
        ind0 = bisect_right(G,indb)
        for j in range(ind0,len(G)):
            indg = G[j]
            ind1 = bisect_right(R,indg)
            if Ron[2*indg-indb]==0:
                cnt += len(R)-ind1
            else:
                cnt += len(R)-ind1-1
print(cnt)

Submission status: Runtime Error

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

cnt = 0
for n1 in range(N):
  s1 = S[n1]
  for n2 in range(n1+1, N):
    s2 = S[n2]
    if s2 != s1
    	gap = n2-n1
      	for n3 in range(n2+1, N):
          gap2 = n3-n2
          s3 = S[n3]
          if s1 != s3 and s2 != s3:
            if gap2 != gap:
              cnt += 1
              
print(cnt)

Submission status: Runtime Error

Solution code:
import numpy as np
N = int(input())
S = input()
ans = 0
for i in range(0, N):
    for j in range(i, N):
        if i ==j or S[i] == S[j]:
            continue
        s = ['R', 'G', 'B']
        s.remove(S[i])
        s.remove(S[j])
        k_s = np.where(np.array(list(S[j:])) == s[0])[0] + j
        for k in k_s:
            if j == k or S[j] == S[k]:
                continue
            elif j-i != k-j and S[i] != S[k]:
                ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
lis = str(input())
a = []
b = []
for pair in itertools.combinations(lis, 3):
    a.append(list(pair))
    print(list(pair))

lis_num = range(len(lis))
for i in itertools.combinations(lis_num, 3):
    b.append(list(i))
    print(i)
    
count = 0
for i in range(len(a)):
    if (a[i][0]!=a[i][1] and a[i][1]!=a[i][2] and a[i][2] != a[i][0] and b[i][0]!=b[i][1] and b[i][1] != b[i][2]):
        count += 1
print(count)

Submission status: Runtime Error

Solution code:
N = int(input())
S = list(input())
ans = 0


def main():
    for i in range(N):
        first_s = S[i]
        for j in range(i+1, N):
            if S[j] != first_s:
                second_s = S[j]
                for k in range(j+1, N):
                    if (S[k] != first_s) and (S[k] != second_s):
                        if k == 2*j-i:
                            continue
                        ans += 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
ans = 0
for i in range(0, N):
    for j in range(i, N):
        if i ==j or S[i] == S[j]:
            continue
        s = ['R', 'G', 'B']
        s.remove(S[i])
        s.remove(S[j])
        k_s = np.where(np.array(list(S[j:])) == s[0])[0] + j
        for k in k_s:
            if j == k or S[j] == S[k]:
                continue
            elif j-i != k-j and S[i] != S[k]:
                ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque

N = int(input())
S = input()

R, G, B = deque(), deque(), deque()
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
while len(R) != 0:
    r = R.popleft()
    for i in range(len(G)):
        g = G[i]
        for j in range(len(B)):
            L = sorted([r, g, B[j]]
            if L[2] - L[1] != L[1] - L[0]:
                ans += 1
            if L[1] == B[j]:
                ans += len(B) - j
                break

print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n=int(input())
s=input()
r=[]
g=[]
b=[]
lr=0
lg=0
lb=0
for i in range(n):
    if(s[i]=='R'):
        r.append(i+1)
        lr+=1
    elif(s[i]=='G'):
        g.append(i+1)
        lg+=1
    else:
        b.append(i+1)
        lb+=1
ans=0
# print(r,g,b)
# print(lr,lg,lb)
for i in range(lr):
    for j in range(lg):
        ind1=bisect(b,(r[i]+g[j])/2)
        ind2=bisect(b,2*g[j]-r[i])
        ind3=bisect(b,2*r[i]-g[j])
        if(ind1!=lb and (b[ind1]==(r[i]+g[j])/2)) or (ind1!=0 and (b[ind1-1]==(r[i]+g[j])/2)) :
            ans+=1
        if(ind2!=lb and (b[ind2]==(2*g[j]-r[i]))) or (ind2!=0 and b[ind2-1]==(2*g[j]-r[i])):
            ans+=1
        if(ind3!=lb and (b[ind3]==(2*r[i]-g[j]))) or(ind3!=0 and b[ind3-1]==(2*r[i]-g[j])):
            ans+=1
        # print(ind1,ind2,ind3)
# print(ans)
print(lr*lb*lg-ans)

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):
    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=input()
k=[i for i in range(1,n+1)]
from itertools import combinations
c=list(combinations(k,2))
ans=0
for i in range(len(c)):
  p=c[i][0]
  q=c[i][1]
  r=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:
n = int(input())
s = input()

rs = {}
gs = {}
bs = {}

for i, e in enumerate(s):
  if e == 'R':
    rs[i] = ''
  if e == 'G':
    gs[i] = ''
  if e == 'B':
    bs[i] = ''

a = max(bs.keys())
b = min(bs.keys())
      
saidai = len(rs) * len(gs) * len(bs)
if saidai == 0:
  print('0')
else:
  for i in rs.keys():
    for j in gs.keys():
      mi = i if i < j else j
      ma = j if i < j else i
      p = [mi - (ma - mi), mi + (ma - mi)/2, ma + (ma - mi)]
      for e in p:
        if e%1 != 0 or e < b or e > a:
          continue
        if e in bs:
          saidai -= 1
  print(int(saidai))

Submission status: Runtime Error

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

d = {}
for c in S:
    if c not in d:
        d[c] = 1
    else:
        d[c] += 1
total = d["R"] * d["G"] * d["B"]


dame = 0
for i in range(1, N - 1):
    for j in range(i + 1, N):
        k = 2 * j - i
        if j < k < N+1 and S[i-1] != S[j-1] and S[j-1] != S[k-1] and S[i-1] != S[k-1]:
                dame += 1



if d["R"] == 0 or d["G"] == 0 or d["B"] == 0:
    print(0)
else:
    print(total - dame)

Submission status: Runtime Error

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

for a in range(N-2):
  sa = S[a]
  for b in range(a+1,N-1):  
    sb = S[b]
    if sa != sb:
      for c in range(b+1,N):
        if a-b != b-c:
          sc = s[c]
          if sb != sc and sc != sa:
            count += 1
print(count)

Submission status: Runtime Error

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

R, G, B = [], [], []
for i, c in enumerate(s):
  if c == 'R': R.append(i)
  elif c == 'G': G.append(i)
  else: B.append(i)

ans = 0
for r in R:
  for g in G:
    if r < g: x, y = r, g
    else: x, y = g, r

    ans += len(X3)
    if x*2 - y >= 0:
      if s[x*2 - y] == 'B': ans -= 1
    if (x + y) % 2 == 0:
      if s[(x+y)//2] == 'B': ans -= 1
    if y*2 - x < len(s):
      if s[y*2 - x] == 'B': ans -= 1

print(ans)

Submission status: Runtime Error

Solution code:
l = [input() for _ in range(2)]
S = l[1]
N = int(l[0])
count = 0

indexR = []
indexG = []
indexB = []

i = 0
j = 0
k = 0

for a in range(N):
    character = S[a:a+1]

    if character == 'R':
        indexR.append(a+1)
    elif character == 'G':
        indexG.append(a+1)
    elif character == 'B':
        indexB.append(a+1)

for r in indexR:
    for g in indexG:
        for b in indexB:
            mean = (max[r, g, b] + min[r, g, b])/2
            if mean != r and mean != g and mean != b:
                count = count + 1
print(count)

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
R = [] 
G = []
B = []
i = 1
while i < N+1:
  a = S[i-1]
  if a == 'R':
    R.append(i)
  elif a == 'G':
    G.append(i)
  else:
    B.append(i)
  i += 1
  

c = 0
d = len(R)*len(G)*len(B)


if d == 0:
    print(0)
else:
    for t in range(len(R)):
        for u in range(len(G)):
            k = (R[t] + G[u])/2
            if 2*R[t]- G[u] > 0:
              l = 2*R[t]-G[u]
            if 2*G[u] - R[t] > 0:
            m = 2*G[u]-R[t]
            if k in B:
                c += 1
            if l in B:
                c += 1
            if m in B:
                c += 1

Submission status: Runtime Error

Solution code:
/usr/local/bin/python3 /Users/koki/Documents/ABC/162/D.py
(base) koki@Koki-MBP ~ % /usr/local/bin/python3 /Users/koki/Documents/ABC/162/D.py
39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB
1800
(base) koki@Koki-MBP ~ % /usr/local/bin/python3 /Users/koki/Documents/ABC/162/D.py
39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB
1800
(base) koki@Koki-MBP ~ % /usr/local/bin/python3 /Users/koki/Documents/ABC/162/D.py
2
3
0
(base) koki@Koki-MBP ~ % /usr/local/bin/python3 /Users/koki/Documents/ABC/162/D.py
4

0
(base) koki@Koki-MBP ~ % /usr/local/bin/python3 /Users/koki/Documents/ABC/162/D.py
4
RRGB
1
(base) koki@Koki-MBP ~ % /usr/local/bin/python3 /Users/koki/Documents/ABC/162/D.py
39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB
1800
(base) koki@Koki-MBP ~ %

Submission status: Runtime Error

Solution code:
l = [input() for _ in range(2)]
S = l[1]
N = int(l[0])
count = 0

indexR = []
indexG = []
indexB = []

i = 0
j = 0
k = 0

for a in range(N):
    character = S[a:a+1]

    if character == 'R':
        indexR.append(a+1)
    elif character == 'G':
        indexG.append(a+1)
    elif character == 'B':
        indexB.append(a+1)

for r in indexR:
    for g in indexG:
        for b in indexB:
            mean = (max[r, g, b] + min[r, g, b]/2)
            if mean != r and mean != g and mean != b:
                count = count + 1
print(count)

Submission status: Runtime Error

Solution code:
n=int(input())
s=input()
#n=39
#s="RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB"

r=[]
g=[]
b=[]

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

lenr=len(r)
leng=len(g)
lenb=len(b)

isum=0
for ir in range(len(r)):
    for ig in range(len(g)):
        imin=max(r[ir],g[ig])
        bi=bisect(b,imin)
        if abs(r[ir]-g[ig])+imin in b: 
            isum=isum+lenb-bi-1
        else:
            isum=isum+lenb-bi
for ir in range(len(r)):
    for ib in range(len(b)):
        imin=max(r[ir],b[ib])
        gi=bisect(g,imin)
        if abs(r[ir]-b[ib])+imin in g: 
            isum=isum+leng-gi-1
        else:
            isum=isum+leng-gi
for ig in range(len(g)):
    for ib in range(len(b)):
        imin=max(g[ig],b[ib])
        ri=bisect(r,imin)
        if abs(g[ig]-b[ib])+imin in r: 
            isum=isum+lenr-ri-1
        else:
            isum=isum+lenr-ri
                
print(isum)

Submission status: Runtime Error

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

for a in range(N-2):
  sa = S[a]
  for b in range(a+1,N-1):  
    sb = S[b]
    if sa != sb:
        for c in range(b+1,N):
          if a-b != b-c:
            sc = s[c]
            if sb != sc and sc != sa:
                count += 1
print(count)

Submission status: Runtime Error

Solution code:
import bisect
n = int(input())
s = input()
r = []
g = []
b = []
for i in range(n):
	if s[i] == "R":
		r.append(i+1)
	elif s[i] == "G":
		g.append(i+1)
	else:
		b.append(i+1)
ans = 0
for i in r:
	for j in g:
		ans += len(b)
		if i > j:
			k = j-abs(i-j)
			m = i+abs(i-j)
		else:
			k = j+abs(i-j)
			m = i-abs(i-j)
		if k <= b[-1] and k >= b[0]:
			x = bisect.bisect_left(b,k)
			if x >= 0 and x < len(b):
				if b[x] == k:
					ans -= 1
		if m <= b[-1] and m >= b[0]:
			x = bisect.bisect_left(b,m)
			if x >= 0 and x < len(b):
				if b[x] == m:
					ans -= 1
		l = (i+j)//2
		if abs(i-l) == abs(j-l):
			x = bisect.bisect_left(b,l)
			if x >= 0 and x < len(b):
				if b[x] == l:
					ans -= 1
print (ans)

Submission status: Runtime Error

Solution code:
import numpy as np
from numba import njit
N = input("")
S = input("")
N = int(N)
X = 0

for i in range(N-3):
    j = i+1

    for jj in range(N):
        if not(S[j] == S[i]):
            k = j+1

            for kk in range(N):
                if not(S[k] == S[j] or S[k] == S[i]):
                    if j-i != k-j :
                        X += 1
                k += 1
                if k == N:
                    break            
        j += 1
        if j == N-1:
            break
print(X)

Submission status: Runtime Error

Solution code:

"""
K = int(input())

def gcd(a, b): # 最大公約数
    a = abs(a)
    b = abs(b)
    while b:
        a, b = b, a % b
    return a # int
ans = 0
for i in range(1,K+1):
    for j in range(i,K+1):
        for k in range(j,K+1):
            d = gcd(gcd(i,j),k)
            if i==j==k:
                ans += d
            elif i==j or j== k or k==i:
                ans += 3*d
            else:
                ans += 6*d
print(ans)
"""
N = int(input())
S = input()
R = []
G = []
B = []
import bisect
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
for rr in R:
    for gg in G:
        
        r = min(rr,gg)
        g = max(rr,gg)
        if 2*r-g>=0:
            if 2*r-g == B[bisect.bisect_left(B,2*r-g)]:
                ans -=1
        
        if 2*g-r < N:
            #print(rr,gg,bisect.bisect_left(B,2*g-r))
            if 2*g-r == B[bisect.bisect_left(B,2*g-r)]:
                ans -=1
        
        if (g+r)%2==0 and (g+r)//2 == B[bisect.bisect_left(B,(g+r)//2)]:
            ans -= 1
        ans += len(B)
        
print(ans)

Submission status: Runtime Error

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

rs = []
gs = []
bs = []

for i in range(n):
  if s[i] == "R":
    rs.append(i)
  elif s[i] == "G":
    gs.append(i)
  elif s[i] == "B":
    bs.append(i)

def binary(lst, d):
  low = 0
  high = len(lst) - 1
  while low <= high:
    mid = (low + high) // 2
    if lst[mid] == d:
      return True
    elif lst[mid] < d:
      low = mid + 1
    else:
      high = mid - 1
  return False

ans = len(rs)*len(gs)*len(bs)
for i in range(len(rs)):
  for j in range(len(gs)):
    dis = abs(rs[i] - gs[j])
    if (rs[i] + gs[j])%2 == 0:
      can1 = abs(rs[i] + gs[j])//2
      if binary(bs, can1):
      	ans -= 1
    if max(rs[i], gs[j]) + dis <= bs[-1]:
      can2 = max(rs[i], gs[j]) + dis
      if binary(bs, can2):
      	ans -= 1
    if min(rs[i], gs[j]) - dis >= 0:
      can3 = min(rs[i], gs[j]) - dis
      if binary(bs, can3):
      	ans -= 1
        
print(ans)

Submission status: Runtime Error

Solution code:
from collections import defaultdict
from bisect import bisect_left, bisect_right
n = int(input()) # 入力が1つ
s = input()
# map(int, input().split()) # 入力が複数
# [int(i) for i in input().split()] # 配列で数字
# n = 4
# s = 'RRGB'
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
for i in range(len(r)):
    j = bisect_left(g, r[i])
    k = bisect_left(b, g[j])
    ans += (len(g) - j) * (len(b) - k)

def bs(nums, target):
    l = 0
    r = len(nums) - 1
    while l <= r:
        mid = (l + r) // 2
        if nums[mid] == target:
            return True
        elif nums[mid] < target:
            l = mid + 1
        else:
            r = mid - 1
    return False

def solution(t1, t2, t3):
    ans = 0
    if len(t1) < 1 or len(t2) < 1 or len(t3) < 1:
        return ans
    for i in range(len(t2)):
        num = t2[i]
        left = bisect_left(t1, num)
        right = bisect_right(t3, num)
        ans += left * (len(t3) - right)

    for i in t1:
        for j in t3:
            if i < j:
                if (i + j) % 2 == 0 and bs(t2, (i + j) // 2):
                    ans -= 1
    return ans

ans = 0
ans += solution(r, g, b)
ans += solution(r, b, g)
ans += solution(g, r, b)
ans += solution(g, b, r)
ans += solution(b, r, g)
ans += solution(b, g, r)
print(ans)

Submission status: Runtime Error

Solution code:
N=int(input())
S=list(input())
ans=0
if N==3:
    print("0")
else:
    for i in range(N-3):
        div=S[i+1:]
        A=S[i]
        for j in range((N-i-1)//2):
            if div[j] == div[2j-1]:
                ans-=1
        for j in range(N-i-1):
            DIV=div[j+1:]
            if not div[j] == A:
                ans+=len(DIV.replace(DIV[0],A).replace("",""))
    print(ans)

Submission status: Runtime Error

Solution code:
import sys
import collections

N = int(input())
S = [ch for ch in input()]
counter = collections.Counter(S)
if len(counter.keys()) != 3:
    print(0)
    eys.exit()
total = 1
for k, v in counter.items():
    total *= v

for i in range(len(S)):
    l, r = i - 1, i + 1
    while 0 <= l and r < len(S):
        if len(set([S[i], S[l], S[r]])) == 3:
            total -= 1
        l -= 1
        r += 1
print(total)

Submission status: Runtime Error

Solution code:
from bisect import bisect_left

N = int(input())
S = input()
R = []
G = []
B = []

for idx, s in enumerate(S):
    if s == 'R':
        R.append(idx+1)
    if s == 'G':
        G.append(idx+1)
    if s == 'B':
        B.append(idx+1)
# print(R, G, B)

R0, G0, B0 = R, G, B

sum = 0

for i in range(6):
    if i == 0:
        R, G, B, temp = R0, G0, B0, 'B'
    elif i == 1:
        R, G, B, temp = R0, B0, G0, 'G'
    elif i == 2:
        R, G, B, temp = B0, R0, G0, 'G'
    elif i == 3:
        R, G, B, temp = B0, G0, R0, 'R'
    elif i == 4:
        R, G, B, temp = G0, R0, B0, 'B'
    elif i == 5:
        R, G, B, temp = G0, B0, R0, 'R'

    for s in R:
        idx1 = bisect_left(G, s)
        if idx1 > G[-1]:
            break
        for g in G[idx1:]:
            idx2 = bisect_left(B, g)

            if idx2 > B[-1]:
                break
            sum += len(B[idx2:])
            if 2 * g - s <= N:
                if S[2 * g - s - 1] == temp:
                    sum -= 1

print(sum)

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
r = s.count("R")
g = s.count("G")
b = s.count("B")
ans = 0
ans = r*g*b
for i in range(1,n-1):
    m = min(i,n-i-1)
    for j in range(1,m+1):
        A,B,C = s[i],s[i-j],s[i+j]
        if A != B and A != C and B != C:
            ans -= 1
if r == 0 or g == 0 or b == 0:
    ans = 0
elif r == 1 or g == 1 or b == 1:
    ans = 1
print(ans)
n = int(input())
s = input()
r = s.count("R")
g = s.count("G")
b = s.count("B")
ans = 0
ans = r*g*b
for i in range(1,n-1):
    m = min(i,n-i-1)
    for j in range(1,m+1):
        A,B,C = s[i],s[i-j],s[i+j]
        if A != B and A != C and B != C:
            ans -= 1
if r == 0 or g == 0 or b == 0:
    ans = 0
elif r == 1 or g == 1 or b == 1:
    ans = 1
print(ans)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-


import numpy as np

N=int(input())
S=input()

R,G,B=[],[],[]
RG,GB,BR=[],[],[]

dp=np.zeros([3,N])

NR,NG,NB=0,0,0
for i in range(N):
    if S[i]=="R": 
        R.append(i)
        NR+=1
    if S[i]=="G": 
        G.append(i)
        NG+=1
    if S[i]=="B": 
        B.append(i)
        NB+=1

    dp[0,i]=NR
    dp[1,i]=NG
    dp[2,i]=NB

    if S[i]=="R" or S[i]=="G": RG.append(i)
    if S[i]=="G" or S[i]=="B": GB.append(i)
    if S[i]=="B" or S[i]=="R": BR.append(i)

LR=len(R)
LG=len(G)
LB=len(B)

if LR!=0 and LG!=0 and LB!=0:

    MR=R[-1]
    MG=G[-1]
    MB=B[-1]
    ans=0

    for i in range(N-2):
        si=S[i]

        if si=="R":
            for j in range(LG+LB):
                if GB[j]>i:
                    sj=S[GB[j]]
                    if sj=="G":
                        if GB[j] < MB:
                            ans+=LB-dp[2,GB[j]]
                            p=(GB[j]-i)*2+i
                            if p < N:
                                if S[p]=="B": 
                                    ans-=1
        
                    if sj=="B":
                        if GB[j] < MG:
                            ans+=LG-dp[1,GB[j]]
                            p=(GB[j]-i)*2+i
                            if p < N:
                                if S[p]=="G": 
                                    ans-=1
        
        if si=="G":
            for j in range(LR+LB):
                if BR[j]>i:
                    sj=S[BR[j]]
                    if sj=="R":
                        if BR[j] < MB:
                            ans+=LB-dp[2,BR[j]]
                            p=(BR[j]-i)*2+i
                            if p < N:
                                if S[p]=="B": 
                                    ans-=1

                    if sj=="B":
                        if BR[j] < MR:
                            ans+=LR-dp[0,BR[j]]
                            p=(BR[j]-i)*2+i
                            if p < N:
                                if S[p]=="R": 
                                    ans-=1
        
        if si=="B":
            for j in range(LG+LR):
                if RG[j]>i:
                    sj=S[RG[j]]
                    if sj=="R":
                        if RG[j] < MG:
                            ans+=LG-dp[1,RG[j]]
                            p=(RG[j]-i)*2+i
                            if p < N:
                                if S[p]=="G": 
                                    ans-=1

                    if sj=="G":
                        if RG[j] < MR:
                            ans+=LR-dp[0,RG[j]]
                            p=(RG[j]-i)*2+i
                            if p < N:
                                if S[p]=="R": 
                                    ans-=1
        


print(int(ans))

Submission status: Runtime Error

Solution code:
import numpy as np

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

i=0
for c in combinations(range(N),3):
    if len(set(np.diff(sorted(c)))) == 1:
        continue
    if len(set([S[c[0]],S[c[1]],S[c[2]]])) != 3:
        continue
    i+=1
print(i)