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):


import sys
import math

sys.setrecursionlimit(10 ** 6)

input_line = input()
inputs = input_line.split(' ')
N, = [int(s) for s in inputs]

input_line = input()
s = list(input_line)
sl = len(s)

r = 0
g = 0
b = 0
for ss in s:
    if ss=='R':
        r += 1
    elif ss=='G':
        g += 1
    else:
        b += 1
ans = r*g*b

num = 0

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

ans -= num

print(ans)

Solution (Python):










"""def kiri(n,m):
  r_ = n / m
  if (r_ - (n // m)) > 0:
    return (n//m) + 1
  else:
    return (n//m)"""

""" n! mod m 階乗
mod = 1e9 + 7
N = 10000000
fac = [0] * N
def ini():
  fac[0] = 1 % mod
  for i in range(1,N):
    fac[i] = fac[i-1] * i % mod"""

"""mod = 1e9+7
N = 10000000
pw = [0] * N
def ini(c):
  pw[0] = 1 % mod
  for i in range(1,N):
    pw[i] = pw[i-1] * c % mod"""

"""
def YEILD():
  yield 'one'
  yield 'two'
  yield 'three'
generator = YEILD()
print(next(generator))
print(next(generator))
print(next(generator))
"""
"""def gcd_(a,b):
  if b == 0:
    return a
  return gcd_(a,a % b) 
"""def extgcd(a,b,x,y):
  d = a
  if b!=0:
    d = extgcd(b,a%b,y,x)
    y -= (a//b) * x
    print(x,y)
  else:
    x = 1
    y = 0
  return d"""


"""def gcd(a,b):
  if b == 0:
    return a
  return gcd(b, a % b)
def lcm(a,b):
  g = gcd(a,b)
  return (a * b) // g"""


"""import bisect 
print(A,bisect.bisect_left(A,4))
print(C,n - bisect.bisect_right(C,4))
print(sum(bisect.bisect_left(A,b) * (n-bisect.bisect_right(C,b)) for b in B))"""


"""import math
pi_ = math.pi"""


"""def Erato(n):
    prime = []
    is_prime = [True] * (n+1)
    p = 0
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2,n+1):
      if is_prime[i] == True:
        prime.append(i)
        p += 1
        for j in range(2 * i,n+1,i):
          is_prime[j] = False
    return p"""

def readInts():
  return list(map(int,input().split()))
mod = 10**9 + 7
def main():
  n = int(input())
  s = input()
  t = s.count('R')*s.count('G')*s.count('B')
  for i in range(n):
    j = 1
    while i+j*2<n:
      
      
	  
      if s[i]!= s[i+j] and s[i+j]!=s[i+j*2] and s[i] != s[i+j*2]:
        t-=1
      j+=1
  print(t)

  

if __name__ == '__main__':
  main()

Solution (Python):










"""def kiri(n,m):
  r_ = n / m
  if (r_ - (n // m)) > 0:
    return (n//m) + 1
  else:
    return (n//m)"""

""" n! mod m 階乗
mod = 1e9 + 7
N = 10000000
fac = [0] * N
def ini():
  fac[0] = 1 % mod
  for i in range(1,N):
    fac[i] = fac[i-1] * i % mod"""

"""mod = 1e9+7
N = 10000000
pw = [0] * N
def ini(c):
  pw[0] = 1 % mod
  for i in range(1,N):
    pw[i] = pw[i-1] * c % mod"""

"""
def YEILD():
  yield 'one'
  yield 'two'
  yield 'three'
generator = YEILD()
print(next(generator))
print(next(generator))
print(next(generator))
"""
"""def gcd_(a,b):
  if b == 0:
    return a
  return gcd_(a,a % b) 
"""def extgcd(a,b,x,y):
  d = a
  if b!=0:
    d = extgcd(b,a%b,y,x)
    y -= (a//b) * x
    print(x,y)
  else:
    x = 1
    y = 0
  return d"""


"""def gcd(a,b):
  if b == 0:
    return a
  return gcd(b, a % b)
def lcm(a,b):
  g = gcd(a,b)
  return (a * b) // g"""


"""import bisect 
print(A,bisect.bisect_left(A,4))
print(C,n - bisect.bisect_right(C,4))
print(sum(bisect.bisect_left(A,b) * (n-bisect.bisect_right(C,b)) for b in B))"""


"""import math
pi_ = math.pi"""


"""def Erato(n):
    prime = []
    is_prime = [True] * (n+1)
    p = 0
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2,n+1):
      if is_prime[i] == True:
        prime.append(i)
        p += 1
        for j in range(2 * i,n+1,i):
          is_prime[j] = False
    return p"""

def readInts():
  return list(map(int,input().split()))
mod = 10**9 + 7
def main():
  n = int(input())
  s = input()
  t = s.count('R')*s.count('G')*s.count('B')
  for i in range(n):
    j = 1
    while i+j*2<n:
      
      
	  
      if s[i]!= s[i+j] and s[i+j]!=s[i+j*2] and s[i] != s[i+j*2]:
        t-=1
      j+=1
  print(t)

  

if __name__ == '__main__':
  main()

Solution (Python):




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

    
    r, g, b = 0,0,0
    for s in S:
        if s=="R":
            r += 1
        elif s == "G":
            g += 1
        else:
            b += 1
    rgb = r*g*b
    
    sub = 0
    for i in range(N):
        for j in range(i+1, N):
            if S[i]==S[j]:
                continue
            k = 2*j-i
            if k >= N or S[k]==S[i] or S[k]==S[j]:
                continue
            sub += 1
    return print(rgb - sub)

if __name__ == "__main__":
    resolve()

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:
      if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
        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 d in range(1, (N-i-1)//2+1):
        if {S[i], S[i+d], S[i+2*d]} == {"R", "G", "B"}:
            ans -= 1

print(ans)

Solution (Python):





kumiawase = 0
countR=0
countG=0
countB=0
N = int(input())
S = list(input())
for i in range(N):
    if(S[i] == "R"):
        countR += 1
    elif(S[i] == "G"):
        countG += 1
    elif(S[i] == "B"):
        countB += 1
kumiawase = countR * countG * countB

count = 0


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

print(kumiawase - count)

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

r_cnt = S.count("R")
g_cnt = S.count("G")
b_cnt = S.count("B")

total = r_cnt * g_cnt * b_cnt
diff = 0

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

Solution (Python):


def abc162d(n, s):

    r_arr = []
    g_arr = []
    b_arr = []

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

    min1_arr = r_arr
    min2_arr = g_arr
    max_arr = b_arr
    if len(r_arr) < len(g_arr) and len(b_arr) < len(g_arr):
        min1_arr = r_arr
        min2_arr = b_arr
        max_arr = g_arr
    elif len(g_arr) < len(r_arr) and len(b_arr) < len(r_arr):
        min1_arr = g_arr
        min2_arr = b_arr
        max_arr = r_arr

    max_dict = dict()
    for elem in max_arr:
        max_dict[elem] = 1

    except_num = 0
    for r_i in min1_arr:
        for g_i in min2_arr:
            min_i = r_i
            max_i = g_i
            if g_i < r_i:
                min_i = g_i
                max_i = r_i
            diff = max_i - min_i
            minmin = min_i - diff
            maxmax = max_i + diff
            exists_central = (max_i + min_i) % 2

            if minmin in max_dict:
                except_num += 1
            if maxmax in max_dict:
                except_num += 1
            if exists_central == 0:
                central = (max_i + min_i) / 2
                if central in max_dict:
                    except_num += 1

    ans = len(r_arr) * len(g_arr) * len(b_arr) - except_num
    return ans



if(__name__ == '__main__'):
    
    n = int(input())
    s = input()
    
    ans = abc162d(n, s)
    
    
    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 j in range(n):
  for i in range(j):
    if (k:=2*j-i)<n:
      if s[i]!=s[k]!=s[j]!=s[i]:
        count-=1   
print(count)

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

ans=s.count("R")*s.count("B")*s.count("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[i]!=s[k]:
            ans -=1

print(ans)

Solution (Python):
import sys
import math
from functools import reduce


def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def MI1(): return map(int1, sys.stdin.readline().split())

def LI(): return list(map(int, sys.stdin.readline().split()))

def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SR(): return sys.stdin.readline().rstrip()

ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
ascii_uppercase2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ'


p2D = lambda x: print(*x, sep="\n")
p2E = lambda x: print(''.join(x))
p2S = lambda x: print(*x, sep=" ")

def gcd_list(numbers):
    return reduce(math.gcd, numbers)

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=2*j-i
    if k>=N:
      continue
    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()
t = 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 and s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
            t -= 1

print(t)

Solution (Python):
from collections import deque, Counter
from itertools import chain, combinations
import json

import bisect
import sys
import math
import bisect

sys.setrecursionlimit(10 ** 8)


M = 10 ** 9 + 7


def f(rs, gs, bs):
    res = 0
    lgs = len(gs)
    lbs = len(bs)
    for i in rs:
        j = bisect.bisect_left(gs, i)
        k = bisect.bisect_left(bs, i)
        res += (lgs - j) * (lbs - k)

        for jj in range(j, lgs):
            d = (gs[jj] - i)
            index = bisect.bisect_left(bs, i + 2 * d)
            if index != lbs:
                if bs[index] == i + 2 * d:
                    res -= 1

        for kk in range(k, lbs):
            d = (bs[kk] - i)
            index = bisect.bisect_left(gs, i + 2 * d)
            if index != lgs:
                if gs[index] == i + 2 * d:
                    res -= 1

    return res


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

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

    for i, c in enumerate(S):
        if c == 'R':
            rs.append(i)
        elif c == 'G':
            gs.append(i)
        else:
            bs.append(i)

    RGB = len(rs) * len(gs) * len(bs)

    rm = 0
    for i in range(N):
        for j in range(i + 1, N):
            d = j - i
            k = i + 2 * d
            if N <= k:
                break
            if len({S[i], S[j], S[k]}) == 3:
                rm += 1

    print(RGB - rm)


if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
s = input()
t = 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 and s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
            t -= 1

print(t)

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

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

rgb = [[], [], []]

for i, c in enumerate(s):
    rgb[(c > 'G') + (c > 'B')].append(i)

exclude = 0
for i in rgb[0]:
    for j in rgb[1]:
        ii = min(i, j)
        jj = max(i, j)
        d = jj - ii
        if ii - d >= 0 and ii - d in rgb[2]:
            exclude += 1
        if jj + d < n and jj + d in rgb[2]:
            exclude += 1
        if not d % 2 and ii + d // 2 in rgb[2]:
            exclude += 1

summ = len(rgb[0]) * len(rgb[1]) * len(rgb[2])
print(summ - exclude)

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

rList = []
gList = []
bList = []

for indx in range(N):
    if S[indx] == 'R':
        rList.append(indx)
    elif S[indx] == 'G':
        gList.append(indx)
    elif S[indx] == 'B':
        bList.append(indx)

NoSum = 0
fList = rList
sList = gList
remain = 'B'

for fItem in fList:
    for sItem in sList:
        prsMax = max(fItem, sItem)
        prsMin = min(fItem, sItem)
        if ((2 * prsMax - prsMin) <= N-1):
            if (S[(2 * prsMax - prsMin)] == remain):
                NoSum += 1
        
        if ((prsMax - prsMin) % 2 == 0):
            if (S[int((prsMax + prsMin)/2)] == remain):
                NoSum += 1
        
        if ((2 * prsMin - prsMax) >= 0):
            if  (S[(2 * prsMin - prsMax)] == remain):
                NoSum += 1

print(len(rList) * len(gList) * len(bList) - NoSum)

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

Solution (Python):




import sys
input = sys.stdin.readline



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

    R = set([])
    G = set([])
    B = set([])

    for i in range(N):
        temp = S[i]
        if temp == 'R':
            R.add(i)
        elif temp == 'G':
            G.add(i)
        else:
            B.add(i)

    all = [R,G,B]

    M = [len(R),len(G),len(B)]
    res = M[0] * M[1] * M[2]
    index = M.index(max(M))

    most = all.pop(index)
    first = all[0]
    second = all[1]

    count = 0

    for f in first:
        for s in second:
            temp1 = s * 2 - f
            temp2 = f * 2 - s
            temp3 = (f + s) // 2
            if (True) & (temp1 in most):
                count += 1
            if (True) & (temp2 in most):
                count += 1
            if (s + f) % 2 == 0:
                if temp3 in most:
                    count += 1

    res -= count
    print(res)




main()

Solution (Python):
from itertools import accumulate

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

R = [0] + list(accumulate(int(s == "R") for s in S))
G = [0] + list(accumulate(int(s == "G") for s in S))
B = [0] + list(accumulate(int(s == "B") for s in S))

memo = {"R": R, "G": G, "B": B}

ans = 0
for i in range(N - 2):
    for j in range(i + 1, N - 1):
        if S[i] == S[j]:
            continue

        color = "RGB".replace(S[i], "").replace(S[j], "")
        C = memo[color]

        d = j - i
        x = N - j
        if d < x and S[j + d] == color:
            ans += C[N] - C[j] - 1
        else:
            ans += C[N] - C[j]

print(ans)

Solution (Python):
N = int(input())
R = []
G = []
B = []
RD = {}
GD = {}
BD = {}

S = input().rstrip()

for i, s in enumerate(S):
    if s == "R":
        R.append(i)
        RD[i*2] = 1
    if s == "G":
        G. append(i)
        GD[i*2] = 1
    if s == "B":
        B.append(i)
        BD[i*2] = 1

ans = len(R) * len(G) * len(B)
for r in R:
    for b in B:
        if r + b in GD:
            ans -= 1
for b in B:
    for g in G:
        if b + g in RD:
            ans -= 1
for r in R:
    for g in G:
        if r + g in BD:
            ans -= 1
print(ans)

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

cand = ["RGB", "RBG", "GRB", "GBR", "BRG", "BGR"]

ans = S.count("R") * S.count("G") * S.count("B")
for i in range(N - 2):
    for d in range(1, (N - i + 1) // 2):
        if S[i:i + 2 * d + 1:d] in cand:
            ans -= 1

print(ans)

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

print(ans)

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

cntOne = S.count('R') * S.count('G') * S.count('B')
cntTwo = 0


for i in range(N-1):
    for j in range(i+1, N):
        k = (j - i) + j
        if k < N:
            if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
                cntTwo += 1
print(cntOne - cntTwo)

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

r=S.count('R')
g=S.count('G')
b=S.count('B')
al_num=r*g*b


sub=0
for i in range(0,N):
  for j in range(i+1,N):
    if S[i]==S[j]:
      continue

    k = 2*j -i
    
    if k>=N or S[k]==S[i] or S[k]==S[j]:
      continue
    
    sub += 1
print(al_num - sub)

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

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

res = 0
for i in range(N - 1):
    for j in range(i + 1, N):
        k = j + j - i
        if k >= N:
            break
        if S[i] != S[j] and S[j] != S[k] and S[i] != S[k]:
            res += 1

res = R * G * B - res
print(res)

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

point = 0
rList = []
gList = []
bList = []
for value in s:
    point += 1
    if value == "R":
        rList.append(point)
    elif value == "G":
        gList.append(point)
    elif value == "B":
        bList.append(point)

if len(rList) < 1 or len(gList) < 1 or len(bList) < 1:
    print(0)
else:
    count = len(rList) * len(gList) * len(bList)
    for i in range(n-1):
        for j in range(i+1,n):
            k = 2*j-i
            if k < n and s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
                count -= 1
    print(count)

Solution (Python):
N = int(input())
S = list(input())
S_l = len(S)
R = 0
G = 0
B = 0
for s in S:
  if s=='R':
    R+=1
  elif s=='G':
    G+=1
  else:
    B+=1

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

Solution (Python):

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

Solution (Python):
import numpy as np
N = int(input())
S = input()

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

ans = len(l_r)*len(l_g)*len(l_b)

for r in l_r:
  for g in l_g:
    mi = min(r, g)
    ma = max(r, g)
    l = ma - mi
    if ma+l in l_b:
      ans -= 1
    if mi-l in l_b:
      ans -= 1
    if l%2==0 and l//2 + mi in l_b:
      ans -= 1

print(ans)

Solution (Python):

from collections import deque
N = int(input())
S = input()
Sd = deque(S)
ans = 0
R, G, B = 0, 0, 0
for i in range(N):
    p = Sd.popleft()
    if p == "R":
        R += 1
    elif p == "G":
        G += 1
    else:
        B += 1
ans = R * G * B
for i in range(N):
    for d in range(N):
        j = i + d
        k = j + d
        if k >= N:
            continue
        if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
            ans -= 1
print(ans)

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

R = 0
G = 0
B = 0

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

ans = R*G*B



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

print(ans)

Solution (Python):
def main():
    from itertools import accumulate as ac
    n = int(input())
    s = input()
    R_list = [0]*n
    G_list = [0]*n
    B_list = [0]*n
    for i, j in enumerate(s):
        if j == "R":
            R_list[i] += 1
        elif j == "G":
            G_list[i] += 1
        else:
            B_list[i] += 1
    R_list = list(ac(R_list[::-1]))[::-1]
    G_list = list(ac(G_list[::-1]))[::-1]
    B_list = list(ac(B_list[::-1]))[::-1]
    RG_list = [0]*n
    RB_list = [0]*n
    GR_list = [0]*n
    GB_list = [0]*n
    BR_list = [0]*n
    BG_list = [0]*n
    for i, j in enumerate(s[:-1]):
        if j == "R":
            RG_list[i] += G_list[i+1]
            RB_list[i] += B_list[i+1]
        elif j == "G":
            GR_list[i] += R_list[i+1]
            GB_list[i] += B_list[i+1]
        else:
            BR_list[i] += R_list[i+1]
            BG_list[i] += G_list[i+1]
    RG_list = list(ac(RG_list[::-1]))[::-1]
    RB_list = list(ac(RB_list[::-1]))[::-1]
    GR_list = list(ac(GR_list[::-1]))[::-1]
    GB_list = list(ac(GB_list[::-1]))[::-1]
    BR_list = list(ac(BR_list[::-1]))[::-1]
    BG_list = list(ac(BG_list[::-1]))[::-1]
    ans = 0
    for i in range(n-1):
        if s[i] == "R":
            ans += GB_list[i+1]
            ans += BG_list[i+1]
        elif s[i] == "G":
            ans += RB_list[i+1]
            ans += BR_list[i+1]
        else:
            ans += RG_list[i+1]
            ans += GR_list[i+1]
    for i in range(n):
        for j in range(i+1, n):
            k = 2*j-i
            if k >= n:
                continue
            else:
                if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
                    ans -= 1
    print(ans)


main()

Solution (Python):
import sys
from io import StringIO
import unittest

def resolve():
    n = int(input())
    s = input()
    ans = s.count('R') * s.count('G') * s.count('B')

    for i in range(n):
        for j in range(1, (n - 1)//2 + 1, 1):
            if i + 2 * j < n:
                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)


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 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__":
    
    resolve()

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

count = 0

R_count = [0] * (N+1)
R = 0
G_count = [0] * (N+1)
G = 0
B_count = [0] * (N+1)
B = 0

for i, s in enumerate(reversed(list(S))):
    if s == 'R':
        R += 1
    if s == 'G':
        G += 1
    if s == 'B':
        B += 1

    R_count[N - i - 1] = R
    G_count[N-i-1] = G
    B_count[N-i-1] = B




for i in range(0, N-2):
    for j in range(i+1, N-1):
        if S[i] == S[j]:
            continue
        if (S[i] == 'R' and S[j] == 'G') or (S[i] == 'G' and S[j] == 'R'):
            count += B_count[j+1]
            if j + (j-i) < N and S[j + (j-i)] == 'B':
                count -= 1
        elif (S[i] == 'R' and S[j] == 'B') or (S[i] == 'B' and S[j] == 'R'):
            count += G_count[j+1]
            if j + (j-i) < N and S[j + (j-i)] == 'G':
                count -= 1
        if (S[i] == 'B' and S[j] == 'G') or (S[i] == 'G' and S[j] == 'B'):
            count += R_count[j+1]
            if j + (j-i) < N and S[j + (j-i)] == 'R':
                count -= 1
print(count)

Solution (Python):
def main():
    n=int(input())
    s=list(input())
    rgb=[[0]*3 for _ in range(n)]
    rgb[0][0],rgb[0][1],rgb[0][2]=s.count('R'),s.count('G'),s.count('B')
    ans=0

    def num(x):
        if x=='R':
            return 0
        elif x=='G':
            return 1
        else:
            return 2
    for i in range(n-1):
        rgb[i+1][0],rgb[i+1][1],rgb[i+1][2]=rgb[i][0],rgb[i][1],rgb[i][2]
        rgb[i+1][num(s[i])]-=1

    for i in range(n-2):
        for j in range(i+1,n-1):
            if s[i]==s[j]:
                continue
            else:
                ans+=rgb[j+1][3-num(s[i])-num(s[j])]
                if 2*j-i<=n-1:
                    if s[i]!=s[2*j-i] and s[j]!=s[2*j-i]:
                        ans-=1
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
import sys
from io import StringIO
import unittest

def resolve():
    n = int(input())
    s = input()
    ans = s.count('R') * s.count('G') * s.count('B')

    for i in range(n):
        for j in range(1, (n - 1)//2 + 1, 1):
            if i + 2 * j < n:
                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)


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 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__":
    
    resolve()

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

num_r, num_g = S.count('R'), S.count('G')
num_b = N - num_r - num_g
max_ans = num_r * num_g * num_b
count_not_ans = 0
S = list(S)

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

print(max_ans - count_not_ans)

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-1):
        k = 2 * j - i
        if(k < n):
            if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
                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):
    for j in range(i + 1, n):
        k = j * 2 - i
        if k >= n:
            continue
        if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
            ans -= 1
print(ans)

Solution (Python):
n = int(input())
s = input()
rgb = [s.count('R'), s.count('G'), s.count('B')]

cnt = rgb[0]*rgb[1]*rgb[2]
for i in range(n-2):
    j = 1
    while i+j*2 <= n-1:
        if not (s[i] == s[i+j] or s[i+j] == s[i+j*2] or s[i] == s[i+j*2]):
            cnt -= 1
        j += 1

print(cnt)

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

res = S.count("R") * S.count("G") * S.count("B")
for i in range(N):
  for j in range(N)[i:]:
    k = 2*j-i
    if N <= k:
      break
    if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
      res -= 1

print(res)

Solution (Python):
import collections
n = int(input())
s=input()
l=list(collections.Counter(s).values())
if len(l) == 3:
    ans=l[0]*l[1]*l[2]
    for i in range(n):
        for j in range(i+1,n):
            k = 2*j-i
            if n <= k:
                break
            if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
                ans-=1
    print(ans)
else:
    print(0)

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

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

r_cnt = s.count('R')
g_cnt = s.count('G')
b_cnt = s.count('B')

ans = r_cnt * g_cnt * b_cnt

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

print(ans)

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+1, n):
        x, y = s[i], s[j]
        if x == y:
            continue
        if n <= 2*j-i:
            continue
        z = s[2*j-i]
        if x != z and y != z:
            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

total = r * g * b
for i in range(N):
    for d in range(N):
        j = i + d
        k = j + d
        if k > N-1:
            break
        if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
            total -= 1

print(total)

Solution (Python):
N= int(input())
S=input()
r=[]
g=[]
b=[]
A=0
B=0
C=0
for i in range(N):
    if S[i]=="R":
        r.append(i)
        A+=1
    elif S[i]=="G":
        g.append(i)
        B+=1
    else:
        b.append(i)
        C+=1
k=0
for i in range(N-2):
    for j in range(i+1,N-1):
        t=2*j-i
        if t<N:
            if S[i]!=S[j] and S[i]!=S[t] and S[t]!=S[j] :
                k+=1

print(A*B*C-k)

Solution (Python):
n=int(input())
s=input()
c=s.count
r,g,b=c('R'),c('G'),c('B')
t=0
for i in range(n-2):
  for j in range(i+1,n-1):
    k=j*2-i
    if k<n:
      if s[i]+s[j]+s[k] in ('RGB','RBG','GRB','GBR','BRG','BGR'):
        t+=1
print(r*g*b-t)

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

ans = s.count('R') * s.count('G') * s.count('B')
rgb = {'R', 'G', 'B'}

for i in range(n-1):
    for j in range(i+1, n):
        if j - i + j < n:
            if {s[i], s[j], s[j-i+j]} == rgb:
                ans -= 1

print(ans)

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

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

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

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


R = []
B = []
G = []
p = 0
for i in S:
    if(i == "R"):
        R.append(p)
    elif(i == "B"):
        B.append(p)
    elif(i == "G"):
        G.append(p)
    p += 1
R = set(R)
B = set(B)
G = set(G)
sum = len(R) * len(B) * len(G)
for r in R:
    for b in B:
        up = max(r, b)
        dw = min(r, b)
        d = up-dw
        if up + d in G:
            sum -= 1
        if dw - d in G:
            sum -= 1
        if (r+b)/2 in G:
            sum -= 1

print(sum)