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

Solution (Python):
n = int(input())
s = input()
R = []
G = []
B = []
cnt = 0

for i in range(n):
    if s[i] == 'R':
        R.append(i)
    elif s[i] == 'G':
        G.append(i)
    else:
        B.append(i)

ans = len(R) * len(G) * len(B)

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

print(ans - cnt)

Solution (Python):
n = int(input())
s = input()
a = s.count("R")*s.count("G")*s.count("B")
b = 0
for i in range(0,n-1):
  for j in range(i+1,n-1):
    k = 2*j-i
    if k<=n-1:
      if s[i]!=s[j] and s[k]!=s[j] and s[i]!=s[k] :
        b += 1
print(a-b)

Solution (Python):
N=int(input())
S=input()
R=0
G=1
B=2
chk=[0]*N
lst=[[0 for e in range(3)] for f in range(N)]
if S[N-1]=="R":
 chk[N-1]=0
 lst[N-1][0]+=1
elif S[N-1]=="G":
 chk[N-1]=1
 lst[N-1][1]+=1
else:
 chk[N-1]=2
 lst[N-1][2]+=1
if N>=2:
 for i in range(1,N):
  lst[N-1-i][0]+=lst[N-i][0]
  lst[N-1-i][1]+=lst[N-i][1]
  lst[N-1-i][2]+=lst[N-i][2]
  if S[N-1-i]=="R":
   chk[N-1-i]=0
   lst[N-1-i][0]+=1
  elif S[N-1-i]=="G":
   chk[N-1-i]=1
   lst[N-1-i][1]+=1
  else:
   chk[N-1-i]=2
   lst[N-1-i][2]+=1
ans=0
if N<=2:
 print(0)
else:
 for i in range(N-2):
  for j in range(i+1,N-1):
   if chk[i]!=chk[j]:
    ans+=lst[j+1][3-chk[i]-chk[j]]
    if 2*j-i<=N-1:
     if chk[2*j-i]==3-chk[i]-chk[j]:
      ans-=1
print(ans)

Solution (Python):
n=int(input())
s=input()
r=0
g=0
b=0
for i in range(n):
  if s[i]=="R":
    r+=1
  elif s[i]=="G":
    g+=1
  else:
    b+=1
ans=r*b*g
for i in range(n):
  for j in range(i,n):
    k=2*j-i
    if k>=n: break
    if s[i]!=s[j] and s[j]!=s[k] and s[k]!=s[i]:
      ans-=1

print(ans)

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

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

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

ans=R*G*B
for i in range(N-2):
    for j in range(i+1,N-1):
        if S[i]==S[j]:continue
        n=2*j-i
        if n>=N:continue
        if S[i]!=S[n] and S[j]!=S[n]:
            ans-=1

print(ans)

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

Lis=[[] for _ in range(3)]
RGB="RGB"

for i in range(N):
    idx=RGB.index(S[i])
    Lis[idx].append(i+1)

if len(Lis[0])==0 or len(Lis[1])==0 or len(Lis[2])==0:
    print(0)
    exit()

Ng=len(Lis[2])
ans=0
for r in Lis[0]:
    for g in Lis[1]:
        n1=2*r-g
        n2=2*g-r
        n3=(r+g)//2
        Su=Ng
        if N>=n1>0:
            if S[n1-1]=="B":
                Su-=1
        if N>=n2>0:
            if S[n2-1]=="B":
                Su-=1
        if (g-r)%2==0:
            if S[n3-1]=="B":
                Su-=1
        ans+=Su
print(ans)

Solution (Python):
def main():
        N = int(input())
        S = str(input())

        S_list = list(S)

        r = 0; g = 0; b = 0

        for s in S_list:
                if s=="R": r += 1
                elif s=="G": g += 1
                else: b += 1

        sub = 0

        for i in range(N):
                for j in range(i+1,N):
                        if S_list[i]==S_list[j]:
                                continue
                        k = 2*j-i
                        if k>=N or S_list[k]==S_list[i] or S_list[k]==S_list[j]:
                                continue
                        sub += 1

        ans = r*g*b - sub

        print("{}".format(ans))


if __name__=="__main__":
        main()

Solution (Python):
from collections import Counter
N = int(input())
S = input()
Sc = Counter(S)
rgb = Sc["R"] * Sc["G"] * Sc["B"]
for j in range(1, N-1):
    for i in range(j):
        k = j + (j-i)
        assert i < j < k
        if k < N:
            if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                rgb -= 1
print(rgb)

Solution (Python):
from collections import Counter
N = int(input())
S = input()
Sc = Counter(S)
rgb = Sc["R"] * Sc["G"] * Sc["B"]
for j in range(1, N-1):
    for i in range(j):
        k = j + (j-i)
        assert i < j < k
        if k < N:
            if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                rgb -= 1
print(rgb)

Solution (Python):
def solve():
  n = int(input())
  s = input()
  ans = s.count("R")*s.count("G")*s.count("B")
  for i in range(n-2):
    for j in range(i+1,n):
      k = 2*j-i
      if k < n and s[k] != s[i] and s[k] != s[j] and s[i] != s[j]:
        ans -= 1
  print(ans)
  return 0
 
if __name__ == "__main__":
  solve()

Solution (Python):
N = int(input())
S = input()
R = []
G = []
B = [False] * N
for i, c in enumerate(S):
    if c == "R":
        R.append(i)
    elif c == "G":
        G.append(i)
    else:
        B[i] = True
answer = 0
for r in R:
    for g in G:
        hikuyatsu = 0
        u = max(r,g)
        b = min(r,g)
        if b - (u - b) >= 0 and B[b - (u - b)] == True:
            hikuyatsu += 1
        if (u + b) % 2 == 0 and B[(u+b)//2] == True:
            hikuyatsu += 1
        if u + (u - b) < N and B[u + (u - b)] == True:
            hikuyatsu += 1
        answer += (N - len(R) - len(G) - hikuyatsu)
print(answer)

Solution (Python):
N=int(input())
S=input()
ans = 0
for i in range(0,N):
  for j in range(i+1,N):
    if S[i] != S[j]:
      k = j+j-i
      if k < N:
        if S[i] != S[k] and S[j] != S[k]:
          
          ans += 1
ans = (S.count('R') * S.count('G') * S.count('B')) - ans
print(ans)

Solution (Python):
import numpy as np
n=int(input())
s=list(input())
sr=set()
sg=set()
sb=set()
for ii in range(n):
    if s[ii]=="R":
        sr.add(ii)
    if s[ii]=="G":
        sg.add(ii)
    if s[ii]=="B":
        sb.add(ii)
l=[len(sr),len(sg),len(sb)]
l.sort()

if l[0]==len(sr):
  s1=sr
  if l[1]==len(sg):
    s2=sg
    s3=sb
  else:
    s2=sb
    s3=sg
elif l[0]==len(sg):
  s1=sg
  if l[1]==len(sr):
    s2=sr
    s3=sb
  else:
    s2=sb
    s3=sr
else:
  s1=sb
  if l[1]==len(sr):
    s2=sr
    s3=sg
  else:
    s2=sg
    s3=sr
  
r=0
for ii in s1:
  for jj in s2:
    r+=len(s3)
    dt=abs(ii-jj)
    if ii+dt in s3:
      r-=1
    if ii-dt in s3:
      r-=1
    if jj+dt in s3:
      r-=1
    if jj-dt in s3:
      r-=1
    if (ii+jj)%2==0:
      if (ii+jj)//2 in s3:
        r-=1
      
print(r)

Solution (Python):
n=int(input())
s=input()
p=s.count('R')*s.count('G')*s.count('B')
for j in range(1,n-1):
    for i in range(1,min(j,n-j-1)+1):
        x,y,z=s[j-i],s[j],s[j+i]
        if x!=y and y!=z and z!=x:
            p-=1
print(p)

Solution (Python):
import sys
input = sys.stdin.readline
import numpy as np

def main():
    N = int(input())
    S = input().strip()
    rgb = [list() 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)
    
    for i in range(3):
        rgb[i] = np.array(rgb[i])

    ans = 0
    for c in range(3):
        cm = c%3
        c1 = (c+1)%3
        c2 = (c+2)%3
        for m in rgb[cm]:
            tmp = len(set(np.abs(rgb[c1]-m)) & set(np.abs(rgb[c2]-m)))
            ans += tmp
    print(len(rgb[0])*len(rgb[1])*len(rgb[2])-ans)

if __name__ == "__main__":
    main()

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

r = s.count('R')
g = s.count('G')
b = s.count('B')

ans = r*g*b

cnt = 0
for i in range(n):
    for d in range(n):
        j = i + d
        k = j + d
        if k >= n:break
        if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
            ans -= 1
ans -= cnt

print(ans)

Solution (Python):
import re
import itertools
N = int(input())
S = input()
R = S.count('R')
G = S.count('G')
B = S.count('B')



ans = 0
for i in range(N-2):
  for j in range(1, N-1):
    if i + 2*j >=N:
      break
    else:
      if not(S[i] == S[i + 2*j] or S[i + j] == S[i + 2*j] or S[i] == S[i + j]):
        ans += 1
print(R*G*B - ans)

Solution (Python):
from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    n = int(input())
    s = list(input()[:-1].decode())
    r = []
    g = []
    b = [0] * n
    len_b = 0
    for i in range(n):
        if s[i] == 'R':
            r.append(i)
        elif s[i] == 'G':
            g.append(i)
        elif s[i] == 'B':
            b[i] = 1
            len_b += 1
    ans = len(r) * len(g) * len_b

    def check(i):
        return 0 <= i < n and b[i] == 1

    for ri in r:
        for gi in g:
            if check(2 * gi - ri):
                ans -= 1
            if check(2 * ri - gi):
                ans -= 1
            if (ri + gi) % 2 == 0 and check((ri + gi) // 2):
                ans -= 1
    print(ans)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

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

R_cnt, G_cnt, B_cnt = 0, 0, 0

for i in range(len(S)):
    if S[i] == "R":
        R_cnt += 1
    elif S[i] == "G":
        G_cnt += 1
    else:
        B_cnt += 1

All = R_cnt * G_cnt * B_cnt

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

print(All)

Solution (Python):
N=int(input())
S=input()
R=0
G=0
B=0
for i in range(N):
    s=S[i]
    if s=="R":
        R+=1
    elif s=="G":
        G+=1
    else:
        B+=1
ans=R*G*B
for i in range(N-2):
    for j in range(i+1, N-1):
        if 0<=j+j-i<N:
            si=S[i]
            sj=S[j]
            sk=S[2*j-i]
            if si!=sj and sj!=sk and sk!=si:
                ans-=1
print(ans)

Solution (Python):
N = int(input())
S = str(input())


R = 0
G = 0
B = 0

for i in range(N):
    s = S[i]
    if (s == 'R'):
        R +=1
    if (s == 'G'):
        G +=1
    if (s == 'B'):
        B +=1

count = R * G * B


for i in range(N):
    for j in range(1, N//2 + 1):
        if i + j*2 >= N:
            break
        if S[i] != S[i + j] and S[i + j] != S[i + 2 * j] and S[i + 2 * j] != S[i]:
            count -= 1




print(count)

Solution (Python):
n = int(input())
s = input()
 
r, b, g = 0, 0, 0
for i in range(n):
    if s[i] == 'R':
        r += 1
    if s[i] == 'G':
        g += 1
    if s[i] == 'B':
        b += 1
ans = r * g * b
 
for i in range(n):
    for j in range(1, n // 2 + 1):
        if i + 2 * j >= n:
            break
        if s[i] != s[i+j] and s[i] != s[i+2*j] and s[i+j] != s[i+2*j]:
            ans -= 1
 
print(ans)

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

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

ans = len(R) * len(G) * len(B)

count = 0

for r in R:
    for g in G:
        a = r+g
        if a % 2 == 0:
            if S[int(a/2)-1] == 'B':
                count += 1

for r in R:
    for b in B:
        a = r+b
        if a % 2 == 0:
            if S[int(a/2)-1] == 'G':
                count += 1

for g in G:
    for b in B:
        a = g+b
        if a % 2 == 0:
            if S[int(a/2)-1] == 'R':
                count += 1

print(ans - count)

Solution (Python):
def main():
    from collections import Counter
    n, s = int(input()), input()
    c = Counter(s)
    d = list(c.values())
    if len(d) <3:
        print(0)
        exit()
    ans = d[0] * d[1] * d[2]
    rgb = {"R", "G", "B"}
    for i in range(1, n // 2 + 1):
        for j in range(i):
            x = s[j::i]
            for a, b, c in zip(x, x[1:], x[2:]):
                g = {a, b, c}
                if g == rgb:
                    ans -= 1

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    from itertools import product
    n, s = int(input()), input()

    r, g, b = 0, 0, 0
    for x in s:
        if x == "R":
            r += 1
        if x == "G":
            g += 1
        if x == "B":
            b += 1

    ans = r * g * b
    rgb = {"R", "G", "B"}
    for i in range(1, n // 2 + 1):
        for j in range(i):
            x = s[j::i]
            for a, b, c in zip(x, x[1:], x[2:]):
                g = {a, b, c}
                if g == rgb:
                    ans -= 1

    print(ans)


if __name__ == '__main__':
    main()

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

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

for i in range(n):
    for j in range(i+1, n):
        k = 2 * j - i
        if k >= n:
            break
        ans -= s[k] != s[i] != s[j] != s[k]

print(ans)

Solution (Python):
def f(a, b, c):
    from itertools import product
    return sum(i + j in a for i, j in product(b, c))


def solve(string):
    n, s = string.lower().split()
    r, g, b, r2, g2, b2 = [], [], [], set([]), set([]), set([])
    for i, _s in enumerate(s):
        eval(f"{_s}.append(i)")
        eval(f"{_s}2.add(2 * i)")
    return str(len(r) * len(g) * len(b) - f(r2, g, b) - f(g2, b, r) - f(b2, r, g))


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Solution (Python):
def f(a, b, c):
    from itertools import product
    return sum(i + j in a for i, j in product(b, c))


def solve(string):
    n, s = string.split()
    r, g, b, r2, g2, b2 = [], [], [], set([]), set([]), set([])
    for i, _s in enumerate(s):
        if _s == "R":
            r.append(i)
            r2.add(2 * i)
        if _s == "G":
            g.append(i)
            g2.add(2 * i)
        if _s == "B":
            b.append(i)
            b2.add(2 * i)
    return str(len(r) * len(g) * len(b) - f(r2, g, b) - f(g2, b, r) - f(b2, r, g))


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Solution (Python):
import collections
n = int(input())
a = input()
a = a.replace('R','1')
a = a.replace('G','2')
a = a.replace('B','3')
a = tuple(map(int,list(a)))
b = collections.Counter(a)
ans =b[3]*b[1]*b[2]
for i in range(n-2):
    for j in range(i+1,n):
        if a[i] == a[j]:
            continue
        c = j*2 -i
        if c < n:
            if a[i]+a[j]+a[c]==6:
                ans -= 1
        else:
            break
print(ans)

Solution (Python):
from itertools import product


def solve(string):
    n, s = string.split()
    r, g, b = [], [], []
    r2, g2, b2 = set([]), set([]), set([])
    for i, _s in enumerate(s):
        if _s == "R":
            r.append(i)
            r2.add(2 * i)
        if _s == "G":
            g.append(i)
            g2.add(2 * i)
        if _s == "B":
            b.append(i)
            b2.add(2 * i)
    ans = len(r) * len(g) * len(b)
    ans -= sum(i + j in r2 for i, j in product(g, b))
    ans -= sum(i + j in g2 for i, j in product(b, r))
    ans -= sum(i + j in b2 for i, j in product(r, g))
    return str(ans)


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Solution (Python):
from itertools import product


def solve(string):
    n, s = string.split()
    r, g, b = [], [], []
    r2, g2, b2 = set([]), set([]), set([])
    for i, _s in enumerate(s):
        if _s == "R":
            r.append(i)
            r2.add(2 * i)
        if _s == "G":
            g.append(i)
            g2.add(2 * i)
        if _s == "B":
            b.append(i)
            b2.add(2 * i)
    ans = len(r) * len(g) * len(b)
    for i, j in product(r, g):
        ans -= 1 if i + j in b2 else 0
    for i, j in product(g, b):
        ans -= 1 if i + j in r2 else 0
    for i, j in product(b, r):
        ans -= 1 if i + j in g2 else 0
    return str(ans)


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Solution (Python):
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

n=ni()
s = input()

rs=0
gs=0
bs=0

for i in range(n):
  if s[i]=='R':
    rs+=1
  elif s[i]=='G':
    gs+=1
  else:
    bs+=1

total = rs*gs*bs

for i in range(n):
  for j in range(i+1,n):
    k=j+j-i
    if k<n:
      if s[i]!=s[j] and s[j]!=s[k] and s[i]!=s[k]:
        total-=1

print(total)

Solution (Python):
from collections import Counter


def resolve():
    n = int(input())
    s = input()
    cnt = Counter(s)
    ans = cnt['R'] * cnt['G'] * cnt['B']

    for i in range(n - 1):
        for j in range(i + 1, n):
            ken = j * 2 - i
            if (ken >= n):
                continue
            if (s[i] != s[j] and s[j] != s[ken] and s[i] != s[ken]):
                ans -= 1

    print(ans)

resolve()

Solution (Python):
from collections import Counter
n = int(input())
s = input()
c = Counter(s)
ans = c['R'] * c['G'] * c['B']
for i in range(n):
    for j in range(i, n):
        k = j + (j - i)
        if k < n:
            if s[i] == s[j]: continue
            if s[i] == s[k]: continue
            if s[k] == s[j]: continue
            ans -= 1
print(ans)

Solution (Python):
n = int(input())
s = input()
ans = s.count('R')*s.count('G')*s.count('B')
for i in range(n-2):
  for j in range(i+1,n-1):
    if 2*j - i > n-1:
      break
    if s[i] != s[j] and s[j] !=  s[2*j-i] and s[2*j-i] != s[i]:
      ans -=1
print(ans)

Solution (Python):
def count_bigger(a, b):
    i = j = 0
    c = []
    while i < len(a) and j < len(b):
        if a[i] >= b[j]:
            j += 1
            continue
        c.append(len(b) - j)
        i += 1
    if len(c) < len(a):
        c += [0] * (len(a) - len(c))
    for i in range(len(c) - 1, 0, -1):
        c[i - 1] += c[i]
    return c


def f(a, b, c):
    bc = count_bigger(b, c)
    i = j = ans = 0
    while i < len(a) and j < len(b):
        if a[i] >= b[j]:
            j += 1
            continue
        ans += bc[j] - len(set(c) & set(2 * _b - a[i] for _b in b[j:]))
        i += 1
    return ans


def solve(string):
    n, s = string.split()
    r, g, b = [], [], []
    for i, _s in enumerate(s):
        if _s == "R":
            r.append(i)
        if _s == "G":
            g.append(i)
        if _s == "B":
            b.append(i)
    return str(f(r, g, b) + f(r, b, g) + f(b, r, g) + f(b, g, r) + f(g, r, b) + f(g, b, r))


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Solution (Python):
def count_bigger(a, b):
    i = j = 0
    c = []
    while i < len(a) and j < len(b):
        if a[i] >= b[j]:
            j += 1
            continue
        c.append(len(b) - j)
        i += 1
    if len(c) < len(a):
        c += [0] * (len(a) - len(c))
    return c


def f(a, b, c):
    bc = count_bigger(b, c)
    for i in range(len(bc) - 1, 0, -1):
        bc[i - 1] += bc[i]
    i = j = ans = 0
    while i < len(a) and j < len(b):
        if a[i] >= b[j]:
            j += 1
            continue
        ans += bc[j] - len(set(c) & set(2 * _b - a[i] for _b in b[j:]))
        i += 1
    return ans


def solve(string):
    n, s = string.split()
    r, g, b = [], [], []
    for i, _s in enumerate(s):
        if _s == "R":
            r.append(i)
        if _s == "G":
            g.append(i)
        if _s == "B":
            b.append(i)
    return str(f(r, g, b) + f(r, b, g) + f(b, r, g) + f(b, g, r) + f(g, r, b) + f(g, b, r))


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Solution (Python):
n = int(input())
s = input()
r = s.count('R')
g = s.count('G')
b = n - r - g
c = 0
for i in range(n-2):
  for j in range(1,int((n-1-i)/2)+1):
    if s[i] != s[i+j] and s[i] != s[i+j+j] and s[i+j] != s[i+j+j]:
      c += 1
print(r*g*b-c)

Solution (Python):

import sys
import time
import unittest
from io import StringIO


def resolve():
    int(input())
    S = input()
    r = S.count("R")
    g = S.count("G")
    b = S.count("B")
    sum = r * g * b
    for i in range(len(S)):
        for j in range(i + 1, len(S)):
            k = j + (j - i)
            if k < len(S) and S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                sum -= 1
    print(sum)


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def checkTLE(self, input):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin

    def debugIO(self, input):
        stdin = sys.stdin
        sys.stdin = StringIO(input)
        resolve()
        sys.stdin = stdin

    def setUp(self):
        self.startTime = time.time()

    def tearDown(self):
        t = time.time() - self.startTime
        print('%s: %.3f' % (self.id(), t), flush=True)

    def test_入力例_1(self):
        input = """4
RRGB"""
        output = """1"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB"""
        output = """1800"""
        self.assertIO(input, output)


if __name__ == "__main__":
    if "IS_LOCAL" in locals():
        suite = unittest.TestLoader().loadTestsFromTestCase(TestClass)
        unittest.TextTestRunner(verbosity=0).run(suite)
    else:
        resolve()

Solution (Python):
import sys
from collections import Counter


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(20000000)

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


def main():
    N = int(input())
    S = input()
    C = Counter(S)
    r = C.get("R",0)
    g = C.get("G",0)
    b = C.get("B",0)
    answer = r * g * b
    for left in range(N):
        for mid in range(left + 1, N):
            right = mid * 2 - left
            if right < N:
                if S[right] != S[mid] and S[mid] != S[left] and S[right] != S[left]:
                    answer -= 1
    print(answer)


if __name__ == "__main__":
    main()

Solution (Python):
import sys

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


def input():
    return sys.stdin.readline().strip()


def main():
    N = int(input())
    S = input()

    nr = 0
    ng = 0
    nb = 0
    for s in S:
        if s == "R":
            nr += 1
        elif s == "G":
            ng += 1
        else:
            nb += 1

    ans = nr * ng * nb
    for left in range(N - 2):
        for center in range(left + 1, N - 1):
            right = center + (center - left)
            if right < N:
                if (
                    S[right] != S[left]
                    and S[left] != S[center]
                    and S[center] != S[right]
                ):
                    ans -= 1

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(20000000)

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


def main():
    N = int(input())
    S = input()
    R = []
    G = []
    B = []
    for i in range(N):
        if i == 0:
            if S[0] == "R":
                R.append(1)
                B.append(0)
                G.append(0)
            elif S[0] == "B":
                R.append(0)
                B.append(1)
                G.append(0)
            else:
                R.append(0)
                B.append(0)
                G.append(1)
            continue
        if S[i] == "R":
            R.append(R[i - 1] + 1)
            B.append(B[i - 1])
            G.append(G[i - 1])
        elif S[i] == "G":
            R.append(R[i - 1])
            B.append(B[i - 1])
            G.append(G[i - 1] + 1)
        else:
            R.append(R[i - 1])
            B.append(B[i - 1] + 1)
            G.append(G[i - 1])

    answer = 0
    for i in range(N - 2):
        for j in range(i + 1, N - 1):
            if (S[i], S[j]) == ("G", "R") or (S[i], S[j]) == ("R", "G"):
                answer += B[-1] - B[j]
                if 2 * j - i < N:
                    if S[2 * j - i] == "B":
                        answer -= 1
            elif (S[i], S[j]) == ("G", "B") or (S[i], S[j]) == ("B", "G"):
                answer += R[-1] - R[j]
                if 2 * j - i < N:
                    if S[2 * j - i] == "R":
                        answer -= 1
            elif (S[i], S[j]) == ("B", "R") or (S[i], S[j]) == ("R", "B"):
                answer += G[-1] - G[j]
                if 2 * j - i < N:
                    if S[2 * j - i] == "G":
                        answer -= 1
    print(answer)


if __name__ == "__main__":
    main()

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

a = [0]*n

for i in range(n):
    if s[i] == 'R':
        a[i] = 0
    if s[i] == 'G':
        a[i] = 1
    if s[i] == 'B':
        a[i] = 2

cnt = [0]*3

for i in range(n):
    cnt[a[i]] += 1

ans = 1

for i in range(3):
    ans *= cnt[i]

for j in range(n):
    for i in range(j):
        k = j + (j-i)
        if k < n:
            
            if a[i] == a[j]:
                continue
            if a[j] == a[k]:
                continue
            if a[k] == a[i]:
                continue
            ans -= 1
print(ans)

Solution (Python):
n=int(input())
s=input()
from collections import Counter
l=Counter()
l["R"]=[]
l["G"]=[]
l["B"]=[]
for j,i in enumerate(s):
    if l[i]==0:
        l[i]=[]
    l[i].append(j)
ans=0
import bisect
import itertools
ll=Counter()
ll["R"]=len(l["R"])
ll["G"]=len(l["G"])
ll["B"]=len(l["B"])
for i,j in itertools.combinations(range(n),2):
    if s[i]!=s[j]:
        c="RGB".replace(s[i],"").replace(s[j],"")
        idx=bisect.bisect_left(l[c],j)
        ans+=ll[c]-idx
        if 2*j-i<n:
            if s[2*j-i]==c:
                ans-=1
print(ans)

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

r = s.count('R')
g = s.count('G')
b = s.count('B')

all_comb = r*g*b

reduce_cnt = 0
for i in range(n):
    for k in range(i+2,n):
        if (i+k)%2 == 1: continue
        j = (i+k)//2
        if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]: reduce_cnt += 1

print(all_comb-reduce_cnt)

Solution (Python):
import collections
def main():
    N = int(input())
    S = input()
    
    
    

    
    
    c1 = collections.Counter(S)
    meet1 = c1.get("R",0)*c1.get("G",0)*c1.get("B",0)
    
    notmeet2 = 0
    for i in range(0,N-2):
        for j in range(i+1, N-1):
            k = 2*j-i
            if k<=N-1:
                if S[i]!=S[j] and S[i]!=S[k] and S[j]!=S[k]:
                    notmeet2 += 1
    print(meet1 - notmeet2)

main()

Solution (Python):
from math import ceil

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

r = 0
g = 0
b = 0

for s in S:
    if s == 'R':
        r += 1
    elif s == 'G':
        g += 1
    elif s == 'B':
        b += 1
res = r*g*b

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

Solution (Python):


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

r = s.count("R")
g = s.count("G")
b = s.count("B")

paircount_without_regulation = r*g*b


i = 0
j = 1
k = 2
between_counter = 1
regulation_counter = 0

while k <= n-1:
    while k <= n-1:
        if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
            regulation_counter += 1
        i += 1
        j += 1
        k += 1

    i = 0
    j = 1
    k = 2
    j += between_counter
    k += between_counter*2
    between_counter += 1
    

result = paircount_without_regulation - regulation_counter

print(result)

Solution (Python):

n = int(input())
s = input()
ans = s.count("R") * s.count("G") * s.count("B")
for i in range(n - 1):
    for j in range(min(i + 1, n - i)):
        if s[i] != s[i + j] and s[i] != s[i - j] and s[i + j] != s[i - j]:
            ans -= 1

print(ans)

Solution (Python):
N=int(input())
S=str(input())
S=list(S)
ans=S.count('R')*S.count('G')*S.count('B')
for i in range(N): 
    if S[i]=='R':
        S[i]=1
    elif S[i]=='G':
        S[i]=2
    elif S[i]=='B':
        S[i]=4
for i in range(0,N-2):
    for j in range(i+1,N-1):
        k=(j-i)+j
        if k>N-1:
            continue
        if S[i]+S[j]+S[k] == 7 :
            ans -= 1
print(ans)

Solution (Python):
N=int(input())
l=list(input())
r=0
g=0
b=0
for i in range(N):
    if l[i]=="R":
        r+=1
    if l[i]=="G":
        g+=1
    if l[i]=="B":
        b+=1
m=r*g*b
n=0
for i in range(N-1):
    for k in range(i+2,N+1,2):
        if k==N:
            break
        if l[i]==l[k]:
            n+=0
        else:
             if k==N-1:
                if l[int((k+i)/2)]!=l[k] and l[int((k+i)/2)]!=l[i]:
                      n+=1
                      break
                else:
                    break
             else:
                if l[int((k+i)/2)]!=l[k] and l[int((k+i)/2)]!=l[i]:
                      n+=1
m=m-n
print(m)

Solution (Python):
n=int(input())
s=str(input())
r=0
g=0
b=0
for x in range (n):
    if s[x]=='R':
        r+=1
    elif s[x]=='G':
        g+=1
    else:
        b+=1
rgb=r*g*b

for i in range (n):
    for j in range (i,n):
        k=j*2-i
        if k<n:
            if s[i]!=s[j] and s[j]!=s[k] and s[k]!=s[i]:
                rgb=rgb-1
print(rgb)

Solution (Python):
n=int(input())
s=input()
r=s.count('R')
g=s.count('G')
b=s.count('B')
ans=r*b*g
for i in range(1,(n-1)//2+1):
    for j in range(n):
        if j+i+i>n-1:
            break
        if s[j]!=s[j+i] and s[j+i]!=s[j+i+i] and s[j]!=s[j+i+i]:
            ans-=1
print(ans)

Solution (Python):
n=int(input())
s=str(input())
r=0
g=0
b=0
for x in range (n):
    if s[x]=='R':
        r+=1
    elif s[x]=='G':
        g+=1
    else:
        b+=1
rgb=r*g*b

for i in range (n):
    for j in range (i,n):
        k=j*2-i
        if k<n:
            if s[i]!=s[j] and s[j]!=s[k] and s[k]!=s[i]:
                rgb=rgb-1
print(rgb)

Solution (Python):
n = int(input())
s = input()
ans = s.count('R') * s.count('G') * s.count('B')
for i in range(n-2):
    for j in range(i+1, n-1):
        k = j + (j - i)
        if k < n:
            if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
                ans -= 1
print(ans)

Solution (Python):
N=int(input())
S=input()
R=S.count("R")
G=S.count("G")
B=S.count("B")
 
 
count=R*G*B
for i in range(0,N-2):
    for j in range(i+1,N-1):
        k=j+(j-i)
        if k>=N:
            break
        if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
            count=count-1
 
print(count)

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

nR = [0]*n
nG = [0]*n
nB = [0]*n
if s[-1] == 'R':
    nR[-1] = 1
elif s[-1] == 'G':
    nG[-1] = 1
elif s[-1] == 'B':
    nB[-1] = 1
for i in range(n-2, -1, -1):
    if s[i] == 'R':
        nR[i] = nR[i+1] + 1
        nG[i] = nG[i+1]
        nB[i] = nB[i+1]
    elif s[i] == 'G':
        nR[i] = nR[i+1]
        nG[i] = nG[i+1] + 1
        nB[i] = nB[i+1]
    elif s[i] == 'B':
        nR[i] = nR[i+1]
        nG[i] = nG[i+1]
        nB[i] = nB[i+1] + 1

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

            if s[i] != 'R' and s[j] != 'R':
                cnt += nR[j] - f
            elif s[i] != 'G' and s[j] != 'G':
                cnt += nG[j] - f
            elif s[i] != 'B' and s[j] != 'B':
                cnt += nB[j] - f

print(cnt)