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:
n = int(input())
s = list(input())
r = [0]*n
g = [0]*n
b = [0]*n
for i in range(n):
    if s[i]=='R':
        r[i] = 1
    elif s[i] == 'G':
        g[i] = 1
    elif s[i] == 'B':
        b[i] = 1

r_cum = [0]*n
g_cum = [0]*n
b_cum = [0]*n

x = y = z = 0
i = n-1
while i>=0:
    if r[i]==1:
        x+=1
    if g[i]==1:
        y+=1
    if b[i]==1:
        z+=1
    r_cum[i] = x
    g_cum[i] = y
    b_cum[i] = z
    i-=1
s = 0
for i in range(n):
    for j in range(i+1,n):
        #RGB
        if r[i]==1 and g[j]==1:
            if j+1<n and 2*j-i<n and b[2*j-i] == 1:
                s = s + (b_cum[j+1]-1)
                #print(i,j,b_cum[j+1]-1)
            elif j+1<n:
                s = s + b_cum[j+1]
                #print(i,j,b_cum[j+1])
        #RBG
        if r[i]==1 and b[j]==1:
            if j+1<n and 2*j-i<n and g[2*j-i] == 1:
                s = s + (g_cum[j+1]-1)
                #print(i,j,b_cum[j+1]-1)
            elif j+1<n:
                s = s + g_cum[j+1]
                #print(i,j,b_cum[j+1])
        #GBR
        if g[i]==1 and b[j]==1:
            if j+1<n and 2*j-i<n and r[2*j-i] == 1:
                s = s + (r_cum[j+1]-1)
                #print(i,j,b_cum[j+1]-1)
            elif j+1<n:
                s = s + r_cum[j+1]
                #print(i,j,b_cum[j+1])
        #GRB
        if g[i]==1 and r[j]==1:
            if j+1<n and 2*j-i<n and b[2*j-i] == 1:
                s = s + (b_cum[j+1]-1)
                #print(i,j,b_cum[j+1]-1)
            else:
                s = s + b_cum[j+1]
                #print(i,j,b_cum[j+1])
        if b[i]==1 and r[j]==1:
            if j+1<n and 2*j-i<n and g[2*j-i] == 1:
                s = s + (g_cum[j+1]-1)
                #print(i,j,b_cum[j+1]-1)
            elif j+1<n:
                s = s + g_cum[j+1]
                #print(i,j,b_cum[j+1])
        if b[i]==1 and g[j]==1:
            if j+1<n and 2*j-i<n and r[2*j-i] == 1:
                s = s + (r_cum[j+1]-1)
                #print(i,j,b_cum[j+1]-1)
            elif j+1<n:
                s = s + r_cum[j+1]
                #print(i,j,r_cum[j+1])
print(s)

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) and 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) and 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) and b[x] == l:
				ans -= 1
print (ans)

Submission status: Runtime Error

Solution code:
count=0

n=int(input())
l=list(map(str,input()))
for i in range(n-3):
    if list[0]=='R':
        j=i+1
        while i+1<=j and j<=n-1:
            if l[j]=='R':
                j+=1
            else:
                k=j+1
                while j+1<=k and k<=n and k-j!=j-i:
                    if l[k]!=l[j]:
                        count+=1
                    else:
                        k+=1
                j+=1
            i+=1
    if list[0]=='G':
        j=i+1
        while i+1<=j and j<=n-1:
            if l[j]=='G':
                j+=1
            else:
                k+=1
                while j+1<=k and k<=n and k-j!=j-i:
                    if l[k]!=l[j]:
                        count+=1
                    else:
                        k+=1
                j+=1
            i+=1    
    if list[0]=='B':
        j+=1
        while i+1<=j and j<=n-1:
            if l[j]=='B':
                j+=1
            else:
                k+=1
                while j+1<=k and k<=n and k-j!=j-i:
                    if l[k]!=l[j]:
                        count+=1
                    else:
                        k+=1
                j+=1
            i+=1
print(count)

Submission status: Runtime Error

Solution code:
import bisect
N = int(input())
S = list(input())
R_i = []
G_i = []
B_i = []
for i in range(N):
  if S[i] == "R": R_i.append(i)
  elif S[i] == "G": G_i.append(i)
  else: B_i.append(i)

r = len(R_i)
g = len(G_i)
b = len(B_i)

ans = 0
for i in range(N):
  for j in range(1, N-i):
    if S[i] == "R":
      print(i+j in G_i , b - bisect.bisect_right(B_i,i + j) > 0)
      if i+j in G_i and b - bisect.bisect_right(B_i,i + 2 * j) > 0:
        ans += b - bisect.bisect_right(B_i,i + 2 * j)
        if i + j + j in b: ans -= 1
      elif i+ j in B_i and g - bisect.bisect_right(G_i,i + 2 * j) > 0:
        ans += g - bisect.bisect_right(G_i,i + 2 * j)
        if i + j + j in g: ans -= 1
    elif S[i] == "G":
      if i+j in R_i and b - bisect.bisect_right(B_i,i + 2 * j) > 0:
        ans += b - bisect.bisect_right(B_i,i + 2 * j)
        if i + j + j in b: ans -= 1
      elif i+j in B_i and r - bisect.bisect_right(R_i,i + 2 * j) > 0:
        ans += r - bisect.bisect_right(R_i,i + 2 * j)
        if i + j + j in r: ans -= 1
    else:
      if i+j in R_i and g - bisect.bisect_right(G_i,i + 2 * j) > 0:
        ans += g - bisect.bisect_right(G_i,i + 2 * j)
        if i + j + j in g: ans -= 1
      elif i+j in G_i and r - bisect.bisect_right(R_i,i + 2 * j) > 0:
        ans += r - bisect.bisect_right(R_i,i + 2 * j)
        if i + j + j in r: ans -= 1
print(ans)

Submission status: Runtime Error

Solution code:
import numpy
import sys
import statistics

num=int(input())
txt=input()
count=0
ri=[]
gi=[]
bi=[]
for i in range(len(txt)):
  if txt[i]=='R':
    ri.append(i)
  elif txt[i]=='G':
    gi.append(i)
  else:
    bi.append(i)
for i in ri:
  for j in gi:
    add=len(bi)
    if j>i:
      diff=j-i
      if j+diff in bi:
        add-=1
      if i-diff in bi:
        add-=1
    else:
      diff=i-j
      if i+diff in bi:
        add-=1
      if i-diff in bi:
        add-=1
    if (j+i)/2. in bi:
      add-=1
	count+=add
print(count)

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)

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 collections
import math

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

count = collections.Counter(S)
c_value = list(count.values())
con = c_value[0]*c_value[1]*c_value[2]

for i in range(N):
    for j in range(i+1, int(math.ceil((N+i)/2))):
        if S[i] != S[j]:
            if S[i] != S[j + (j - i)] and S[j] != S[j + (j - i)]:
                con -= 1

print(con)

Submission status: Runtime Error

Solution code:
n=int(input())
s=input()
r=[-1]*n
g=[-1]*n
b=[-1]*n
for i in range(n):
  if s[i]=="R":
    r[i]=i
  elif s[i]=="G":
    g[i]=i
  elif s[i]=="B":
    b[i]=i
rn=sorted(set(r))
rn.pop(0)
gn=sorted(set(g))
gn.pop(0)
bn=sorted(set(b))
bn.pop(0)
ans=0
rs=len(rn)
gs=len(gn)
bs=len(bn)
for i in range(n):
  m=s[i]
  ks=0
  if m=="R":
    ks=gs*bs
    for j in gn:
      if j+j-i<n:
        if s[j+j-i]=="B":
          ks=ks-1
      if (j-i)%2==0 and s[i+(j-i)//2]=="B":
        ks=ks-1
    ans=ans+ks
    rn.pop(0)
    rs=rs-1
  elif m=="G":
    ks=rs*bs
    for j in rn:
      if j+j-i<n:
        if s[j+j-i]=="B":
          ks=ks-1
      if (j-i)%2==0 and s[i+(j-i)//2]=="B":
        ks=ks-1
    ans=ans+ks
    gn.pop(0)
    gs=gs-1
  if m=="B":
    ks=gs*rs
    for j in rn:
      if j+j-i<n:
        if s[j+j-i]=="G":
          ks=ks-1
      if (j-i)%2==0 and s[i+(j-i)//2]=="G":
        ks=ks-1
    ans=ans+ks
    bn.pop(0)
    bs=bs-1
print(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:
  x = 0
  for i in range(n-2):
    for j in range(i+1, n-1):
      if s[i]==s[j]:
        continue
      else:
        if 2*j-i<= n-1 and len({s[i], s[j], s[2*j-i]})==3:
          x += 1
print(z-x)

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 < len(b) and b[x] == k:
				ans -= 1
		if m <= b[-1] and m >= b[0]:
			x = bisect.bisect_left(b,m)
			if x < len(b) and b[x] == m:
				ans -= 1
		l = (i+j)//2
		if abs(i-l) == abs(j-l):
			x = bisect.bisect_left(b,l)
			if x < len(b) and b[x] == l:
				ans -= 1
print (ans)

Submission status: Runtime Error

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

r_cum = [0]*n
g_cum = [0]*n
b_cum = [0]*n

x = y = z = 0
i = n-1
while i>=0:
    if r[i]==1:
        x+=1
    if g[i]==1:
        y+=1
    if b[i]==1:
        z+=1
    r_cum[i] = x
    g_cum[i] = y
    b_cum[i] = z
    i-=1
s = 0
for i in range(n):
    for j in range(i+1,n):
        #RGB
        if r[i]==1 and g[j]==1:
            if 2*j-i<n and b[2*j-i] == 1:
                s = s + (b_cum[j+1]-1)
                #print(i,j,b_cum[j+1]-1)
            elif j+1<n:
                s = s + b_cum[j+1]
                #print(i,j,b_cum[j+1])
        #RBG
        if r[i]==1 and b[j]==1:
            if 2*j-i<n and g[2*j-i] == 1:
                s = s + (g_cum[j+1]-1)
                #print(i,j,b_cum[j+1]-1)
            elif j+1<n:
                s = s + g_cum[j+1]
                #print(i,j,b_cum[j+1])
        #GBR
        if g[i]==1 and b[j]==1:
            if 2*j-i<n and r[2*j-i] == 1:
                s = s + (r_cum[j+1]-1)
                #print(i,j,b_cum[j+1]-1)
            elif j+1<n:
                s = s + r_cum[j+1]
                #print(i,j,b_cum[j+1])
        #GRB
        if g[i]==1 and r[j]==1:
            if 2*j-i<n and b[2*j-i] == 1:
                s = s + (b_cum[j+1]-1)
                #print(i,j,b_cum[j+1]-1)
            else:
                s = s + b_cum[j+1]
                #print(i,j,b_cum[j+1])
        if b[i]==1 and r[j]==1:
            if 2*j-i<n and g[2*j-i] == 1:
                s = s + (g_cum[j+1]-1)
                #print(i,j,b_cum[j+1]-1)
            elif j+1<n:
                s = s + g_cum[j+1]
                #print(i,j,b_cum[j+1])
        if b[i]==1 and g[j]==1:
            if 2*j-i<n and r[2*j-i] == 1:
                s = s + (r_cum[j+1]-1)
                #print(i,j,b_cum[j+1]-1)
            elif j+1<n:
                s = s + r_cum[j+1]
                #print(i,j,r_cum[j+1])
print(s)

Submission status: Runtime Error

Solution code:
import heapq
from collections import defaultdict, deque
from math import ceil, factorial, gcd
import sys
import bisect


sys.setrecursionlimit(10 ** 7)
INF = float("inf")
MOD = 10 ** 9 + 7

si = lambda: input().strip()
ii = lambda: int(input())
mii = lambda: map(int, input().split())
lmii = lambda: list(map(int, input().split()))
smii = lambda: sorted(map(int, input().split()))


N = ii()
S = si()

R = []
G = []
B = []

for i in range(N):
    if S[i] == 'R':
        R.append(i)
    if S[i] == 'G':
        G.append(i)
    if S[i] == 'B':
        B.append(i)

colors = [
    [R, G, B],
    [R, B, G],
    [G, R, B],
    [G, B, R],
    [B, R, G],
    [B, G, R],
]

ans = 0

for C1, C2, C3 in colors:
    for c1 in C1:
        for c2 in C2:
            if c1 >= c2:
                continue
            # for c3 in C3:
            #     if c2 >= c3:
            #         continue
            #     if c1 >= c3:
            #         continue
            #     if c2 - c1 != c3 - c2:
            #         print(c1, c2, c3)
            #         ans += 1
            c3_cand = bisect.bisect_right(C3, c2)
            if c3_cand == 0 and C3[0] < c2:
                continue
            if c3_cand == len(C3) and C3[-1] < c2:
                continue
            ans += len(C3) - c3_cand
            if c2 * 2 - c1 in C3:
                ans -= 1

print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
S = list(input())
nums = [i for i in range(N)]
combinations = itertools.combinations(nums, 3)
ans = [1 for comb in combinations if (S[comb[0]] != S[comb[1]] and S[comb[0]] != S[comb[2]] and S[comb[1]] != S[comb[2]]) and (comb[1] - comb[0] != comb[2] - comb[1])]
print(sum(ans))

Submission status: Runtime Error

Solution code:
import sys
input=sys.stdin.readline
#from collections import defaultdict
#d = defaultdict(int)
#import fractions
#import math
#import collections
#from collections import deque
from bisect import bisect_left
N = int(input())
#A = list(map(int,input().split()))
S = list(input())
#S.remove("\n")
#N,M = map(int,input().split())
#S,T = map(str,input().split())
#A = [int(input()) for _ in range(N)]
#S = [input() for _ in range(N)]
#A = [list(map(int,input().split())) for _ in range(N)]
R = []
G = []
B = []
for i in range(N):
    a = S[i]
    if a == "R":
        R.append(i)
    elif a == "G":
        G.append(i)
    else:
        B.append(i)
s = len(R) * len(G) * len(B)

for r in R:
    for g in G:
        Min = min(r,g)
        Max = max(r,g)
        if 2 * Min - Max > 0:
            c1 = 2 * Min - Max
        else:
            c1 = -1
        if (Min + Max ) % 2 == 0:
            c2 = (Min + Max) / 2
        else:
            c2 = -1
        if 2 * Max - Min < N:
            c3 = 2 * Max - Min
        else:
            c3 = -1
        if c1 != -1:
            if B[bisect_left(B,c1)] == c1:
                s -= 1
        if c2 != -1:
            if B[bisect_left(B,c2)] == c2:
                s -= 1
        if c3 != -1:
            if B[bisect_left(B,c3)] == c3:
                s -= 1
print(s)

Submission status: Runtime Error

Solution code:
#include <iostream>

int main() {
  unsigned int N;
  std::string S;
  std::cin >> N;
  std::cin >> S;

  unsigned long sum = 0;
  for (unsigned int i = 0; i < N; ++i) {
    for (unsigned int j = i + 1; j < N; ++j) {
      for (unsigned int k = j + 1; k < N; ++k) {
        const bool cond_1 = (S[i] != S[j] && S[j] != S[k] && S[k] != S[i]);
        const bool cond_2 = (j - i != k - j);
        if (cond_1 && cond_2) {
          sum += 1;
        }
      }
    }
  }

  std::cout << sum << std::endl;

  return 0;
}

Submission status: Runtime Error

Solution code:
n = int(input())
s = str(input())
r = [i + 1 for i in range(n) if s[i] == 'R']
g = [i + 1 for i in range(n) if s[i] == 'G']
b = [i + 1 for i in range(n) if s[i] == 'B']

def count(x,y,z):
    c = 0
    for i in x:
        for j in y:
            if i > j: continue 
            for k in z:
                if j > k: continue 
                if j - i != k - j:
                    c += 1
    return c

ans = 0
ans += count(r,g,b)
ans += count(r,b,g)
ans += count(b,r,g)
ans += count(b,g,r)
ans += count(g,r,b)
ans += count(g,b,r)

print(ans)n = int(input())
s = str(input())
r = [i + 1 for i in range(n) if s[i] == 'R']
g = [i + 1 for i in range(n) if s[i] == 'G']
b = [i + 1 for i in range(n) if s[i] == 'B']

def count(x,y,z):
    c = 0
    for i in x:
        for j in y:
            if i > j: continue 
            for k in z:
                if j > k: continue 
                if j - i != k - j:
                    c += 1
    return c

ans = 0
ans += count(r,g,b)
ans += count(r,b,g)
ans += count(b,r,g)
ans += count(b,g,r)
ans += count(g,r,b)
ans += count(g,b,r)

print(ans)

Submission status: Runtime Error

Solution code:
from collections import Counter

n = int(input())
s = input()
x, y, z = Counter(s).values()
print(x * y * z - sum(s[i] != s[(i + j) // 2] != s[j] != s[i]
                      for i in range(n) for j in range(i + 2, n, 2)))

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
dp = [[0] * 7 for i in range(4000)]

for i in range(n):
    if s[i] == 'R':
        dp[i + 1][0] = dp[i][0] + 1
        dp[i + 1][1] = dp[i][1]
        dp[i + 1][2] = dp[i][2]
        dp[i + 1][3] = dp[i][1] + dp[i][3]
        dp[i + 1][4] = dp[i][4]
        dp[i + 1][5] = dp[i][2] + dp[i][5]
        dp[i + 1][6] = dp[i][6] + dp[i][4]
    elif s[i] == 'G':
        dp[i + 1][0] = dp[i][0]
        dp[i + 1][1] = dp[i][1] + 1
        dp[i + 1][2] = dp[i][2]
        dp[i + 1][3] = dp[i][0] + dp[i][3]
        dp[i + 1][4] = dp[i][2] + dp[i][4]
        dp[i + 1][5] = dp[i][5]
        dp[i + 1][6] = dp[i][6] + dp[i][5]
    else:
        dp[i + 1][0] = dp[i][0]
        dp[i + 1][1] = dp[i][1]
        dp[i + 1][2] = dp[i][2] + 1
        dp[i + 1][3] = dp[i][3]
        dp[i + 1][4] = dp[i][1] + dp[i][4]
        dp[i + 1][5] = dp[i][0] + dp[i][5]
        dp[i + 1][6] = dp[i][6] + dp[i][3]
    for j in range(1, i // 2 + 1):
        if s[i] != s[i - j] and s[i] != s[i - j * 2] and s[i - j] != s[i - 2 * j]:
            dp[i + 1][6] -= 1
print(dp[n][6])

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 b[x] == k:
				ans -= 1
		if m < b[-1] and m > b[0]:
			x = bisect.bisect_left(b,m)
			if b[x] == m:
				ans -= 1
		l = (i+j)//2
		if abs(i-l) == abs(j-l):
			x = bisect.bisect_left(b,l)
			if b[x] == l:
				ans -= 1
print (ans)

Submission status: Runtime Error

Solution code:
from collections import Counter


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

# 等間隔に並んでいる
count = 0

for start in range(N):
  offset = 1
  while True: 
      middle = start + offset
      end = middle + offset

      if end >= N:
          break

      s = {S[start], S[middle], S[end]}

      if len(s) == 3:
          count += 1

      offset += 1


x, y, z = Counter(S).values()

print(x * y * z - count)

Submission status: Runtime Error

Solution code:
def rgb(n,s):

    
    a=s[0]
    if len(s)==3:
        SUM=0

    else:
        SUM=rgb(n-1,s[1:])

        for i in range(1,n-1):
            l=["R","G","B"]
            l.remove(a)
            if s[i] != a:
                b=s[i]

                l.remove(b)
                if l!=[]:
                    c=l[0]
                    p=s.count(c,i+1,2*i)
                    q=s.count(c,2*i+1,n+1)
                    SUM+=p+q
                
    return SUM

n=int(input())
s=input()
print(rgb(n,s))

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
n=int(input())
s=input()
afters={}
for i in range(n):
    afters.setdefault(s[i],0)
    afters[s[i]] += 1

befores = {'R':0, 'B':0, 'G':0}
befores[s[0]]+=1
afters[s[0]]-=1

ans=0
for i in range(1,n-1):
    head=s[0:i]
    last=s[i+1::]
    afters[s[i]]-=1
    if s[i]=='R':
        ans += befores['G'] * afters['B'] + befores['B'] * afters['G'] 
        bad =[['G','B'],['B','G']]
    elif s[i]=='B':
        ans += befores['G'] * afters['R'] + befores['R'] * afters['G'] 
        bad = [['G','R'],['R','G']]
    else:
        ans += befores['B'] * afters['R'] + befores['R'] * afters['B'] 
        bad = [['B','R'],['R','B']]
    for j in range(min(i-0, n-i-1)):
        if [s[i-j-1], s[i+j+1]] in bad:
            ans -= 1
    befores[s[i]]+=1
    

print(ans)

Submission status: Runtime Error

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

RGB = [[] for i in range(3)]
for i in range(N):
    if S[i] == "R":
        RGB[0].append(i)
    elif S[i] == "G":
        RGB[1].append(i)
    elif S[i] == "B":
        RGB[2].append(i)

ans = 0
for n in range(3):
    j_start = 0
    for i in range(len(RGB[n])):
        k_start = 0
        while RGB[n][i] > RGB[(n + 1) % 3][j_start]:
            if j_start + 1 < len(RGB[(n + 1) % 3]):
                j_start += 1
            else:
                break
        for j in range(j_start, len(RGB[(n + 1) % 3])):
            while RGB[(n + 1) % 3][j] > RGB[(n + 2) % 3][k_start]:
                if k_start + 1 < len(RGB[(n + 2) % 3]):
                    k_start += 1
                else:
                    break
            for k in range(k_start, len(RGB[(n + 2) % 3])):
                if RGB[n][i] < RGB[(n + 1) % 3][j] < RGB[(n + 2) % 3][k] and RGB[(n + 1) % 3][j] - RGB[n][i] != RGB[(n + 2) % 3][k] - RGB[(n + 1) % 3][j]:
                    # if RGB[(n + 1) % 3][j] - RGB[n][i] != RGB[(n + 2) % 3][k] - RGB[(n + 1) % 3][j]:
                    ans += 1

for n in range(3):
    j_start = 0
    for i in range(len(RGB[n])):
        k_start = 0
        while RGB[n][i] > RGB[(n + 2) % 3][j_start]:
            if j_start + 1 < len(RGB[(n + 2) % 3]):
                j_start += 1
            else:
                break
        for j in range(len(RGB[(n + 2) % 3])):
            while RGB[(n + 2) % 3][j] > RGB[(n + 1) % 3][k_start]:
                if k_start + 1 < len(RGB[(n + 1) % 3]):
                    k_start += 1
                else:
                    break
            for k in range(k_start, len(RGB[(n + 1) % 3])):
                if RGB[n][i] < RGB[(n + 2) % 3][j] < RGB[(n + 1) % 3][k] and RGB[(n + 2) % 3][j] - RGB[n][i] != RGB[(n + 1) % 3][k] - RGB[(n + 2) % 3][j]:
                    ans += 1

print(ans)

Submission status: Runtime Error

Solution code:
from itertools import combinations as c

def cond1(a,b,c):
    return a != b and b != c and c != a

def cond2(i,j,k):
    return j-i != k - j

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

comb_N = list(c(range(1, N+1), 3))
comb_S = list(c(S, 3))

C = [comb_S[i] for i in range(N) if cond1(*comb_S[i]) and cond2(*comb_N[i])]
print(len(C))

Submission status: Runtime Error

Solution code:
S = input()
ans = 0
for i in range(N):
    for j in range(i+1,N):
            for k in range(j+1,N):
                x,y,z=S[i],S[j],S[k]
                if not (x==y or x==z or y==z or 2*j-i==k):
                    ans+=1
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
s = str(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 range(len(R)):
  for j in range(len(G)):
    test += len(B)
    temp = 0
    if 2*R[i] - G[j] in B:
      temp += 1
    if 2*G[j] - R[i] in B:
      temp += 1
    if (R[i]+G[j])/2 in B:
      temp += 1
    ans += len(B)- temp

print(ans)

Submission status: Runtime Error

Solution code:
from itertools import permutations
inputs = open(0).readlines()
n = int(inputs[0])
S = inputs[1].strip()

R = [i for i, c in enumerate(S) if c == 'R']
G = [i for i, c in enumerate(S) if c == 'G']
B = [i for i, c in enumerate(S) if c == 'B']

def f(Z, i, x, y):
    for z in Z[i:]:
        if z == 2*y-x:
            return 1
        if z > 2*y-x:
            return 0
    return 0

ans = 0
for X, Y, Z in list(permutations([R, G, B])):
    for x in X:
        for y in Y:
            if x < y:
                idx = 0
                for i, z in enumerate(Z):
                    if y < z:
                        break
                if y < z:
                    ans += len(Z) - i - f(Z, i, x, y)
print(ans)

Submission status: Runtime Error

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

def rgb (tmpstr, j, i, s):
    if s[j] != s[i]:
        tmpstr = s[j + 1 :  n]
        tmpset = set(["R","G","B"])
        tmpset.remove(s[j])
        tmpset.remove(s[i])
        remain = list(tmpset)[0]
        num = tmpstr.count(remain) 
        if (j + (j - i)) < len(s):
            if  s[j + (j - i)]  == remain:
                num = num - 1
        return num
    else:
        return 0
    
    
for i in range(n-2):
    out += sum([rgb(tmpstr, j, i, s) for j in range(i+1, n-1)])
print(out)

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 b[x] == k:
				ans -= 1
		if m <= b[-1] and m >= b[0]:
			x = bisect.bisect_left(b,m)
			if b[x] == m:
				ans -= 1
		l = (i+j)//2
		if abs(i-l) == abs(j-l):
			x = bisect.bisect_left(b,l)
			if b[x] == l:
				ans -= 1
print (ans)

Submission status: Runtime Error

Solution code:
from math import gcd
from collections import Counter
from functools import reduce
from math import ceil, floor


def gcd_lst(numbers):
    return reduce(gcd, numbers)


n = int(input())
s = input()
s_cnt = Counter(s).most_common()
rgb_num = []
for key, val in s_cnt:
    rgb_num.append(val)
rgb_gcd = (rgb_num[0] * rgb_num[1] * rgb_num[2]) // gcd_lst(rgb_num)
dif_num = 0
for i in range(n-2):
    for j in range(i+1, (n + i)//2 + 1):
        if  (2 * j - i) >= n:
            break
        if s[i] != s[j] and s[2 * j - i] != s[i] and s[2 * j - i] != s[j]:
            dif_num += 1
print(rgb_gcd - dif_num)

Submission status: Runtime Error

Solution code:
N = int(input())
S = list(map(str,list(input())))
c = 0

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

print(c)

Submission status: Runtime Error

Solution code:
import sys
N = int(sys.stdin.readline().rstrip())
S = sys.stdin.readline().rstrip()

s1 = []
s2 = []
s3 = []
for n in range(N):
    if S[n] == 'R':
        s1.append(n)
    elif S[n] == 'G':
        s2.append(n)
    elif S[n] == 'B':
        s3.append(n)
    
def counts(s1, s2, s3):
    cnt= 0
    for n1 in s1:
        idx1 = len(s2)-1
        f1 = True
        while f1:
            n2 = s2[idx1]

            gap = n2 - n1

            if gap < 1:
                break
            else:
                idx2 = len(s3)-1
                f2 = True
                while f2:
                    n3 = s3[idx2]

                    gap2 = n3-n2
                    if gap2 < 1:
                        break
                    elif gap2 != gap:
                        cnt += 1
                    if idx2==0:
                        break
                    else:
                        idx2 -=1
            if idx1 ==0:
                break
            else:
                idx1 -= 1
                
    return cnt
        
cnt1 = 0
cnt1 += counts(s1,s2,s3)
cnt1 += counts(s1,s3,s2)
cnt1 += counts(s2,s1,s3)
cnt1 += counts(s2,s3,s1)
cnt1 += counts(s3,s2,s1)
cnt1 += counts(s3,s1,s2)
    
print(cnt1)

Submission status: Runtime Error

Solution code:
n=int(input())
s=list(input())
from collections import Counter
myc= Counter(s)
val=list(myc.values())
ans=val[0]*val[1]*val[2]
for i in range(n-2):
    for j in range(i+1,n-1):
        if 2*j-i < n:
            if len(set(list((s[i],s[j],s[2*j-i]))))==3:
                ans-=1
print(ans)

Submission status: Runtime Error

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

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)
mcount = 0
for i in range((n-1)/2-1):
  for j in range(i+1, n-1-1-i):
    if s[j] != s[j-i-1] and s[j] != s[j+i+1] and s[j-i-1] != s[j+i+1]:
      mcount += 1
  

for i in range(n):
  if i in r:
    for j in g:
      for k in b:
        if k < j:
          continue
        count += 1
    for j in b:
      for k in g:
        if k < j:
          continue
        count += 1      
  if i in g:
    for j in r:
      for k in b:
        if k < j:
          continue
        count += 1
    for j in b:
      for k in r:
        if k < j:
          continue
        count += 1
  if i in b:
    for j in g:
      for k in r:
        if k < j:
          continue
        count += 1
    for j in r:
      for k in g:
        if k < j:
          continue
        count += 1
  if i in r:
    r = r[1:]
  elif i in g:
    g = g[1:]
  else:
    b = b[1:]

print(count-mcount)