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):
def calc(i, j, B1, len_b):
    extra = 0
    d = j - i

    
    k1 = d + j
    if k1 < len_b:
        extra += B1[k1]

    
    k2 = i - d
    if 0 <= k2 and k2 < len_b:
        extra += B1[k2]

    
    s = i + j
    if s % 2 == 0:
        k3 = s // 2
        if k3 < len_b:
            extra += B1[k3]
    return extra

def main():
    N = int(input())
    S = input()
    R, G, B = [], [], []
    RGB = {'R': R, 'G': G, 'B': B}
    for i in range(N):
        RGB[S[i]].append(i)
    num_r, num_g, num_b = len(R), len(G), len(B)
    ans = num_r * num_g * num_b
    if ans == 0:
        print(0)
        return

    len_b = (B[-1] + 1)
    B1 = [0]*len_b
    for b in B:
        B1[b] = 1
    
    for i in R:
        for j in G:
            if i < j:
                ans -= calc(i, j, B1, len_b)
            else:
                ans -= calc(j, i, B1, len_b)
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
def calc(i, j, B1, len_b):
    extra = 0
    d = j - i

    
    k1 = d + j
    if k1 < len_b:
        extra += B1[k1]

    
    k2 = i - d
    if 0 <= k2 and k2 < len_b:
        extra += B1[k2]

    
    s = i + j
    if s % 2 == 0:
        k3 = s // 2
        if k3 < len_b:
            extra += B1[k3]
    return extra

def main():
    N = int(input())
    S = input()
    R, G, B = [], [], []
    num_r, num_g, num_b = 0, 0, 0
    for i in range(N):
        s = S[i]
        if s == 'R':
            R.append(i)
            num_r += 1
        elif s == 'G':
            G.append(i)
            num_g += 1
        elif s == 'B':
            B.append(i)
            num_b += 1
    if len(R) == 0 or len(G) == 0 or len(B) == 0:
        print(0)
        return
    len_b = (B[-1] + 1)
    B1 = [0]*len_b
    for b in B:
        B1[b] = 1
    
    ans = 0
    for i in R:
        for j in G:
            ans += num_b
            if i < j:
                ans -= calc(i, j, B1, len_b)
            else:
                ans -= calc(j, i, B1, len_b)
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
def calc(i, j, B1, len_b):
    extra = 0
    d = j - i

    
    k1 = d + j
    if k1 < len_b:
        extra += B1[k1]

    
    k2 = i - d
    if 0 <= k2 and k2 < len_b:
        extra += B1[k2]

    
    s = i + j
    if s % 2 == 0:
        k3 = s // 2
        if k3 < len_b:
            extra += B1[k3]
    return extra

def main():
    N = int(input())
    S = input()
    R, G, B = [], [], []
    RGB = {'R': R, 'G': G, 'B': B}
    for i in range(N):
        RGB[S[i]].append(i)
    num_r, num_g, num_b = len(R), len(G), len(B)
    if num_r == 0 or num_g == 0 or num_b == 0:
        print(0)
        return

    len_b = (B[-1] + 1)
    B1 = [0]*len_b
    for b in B:
        B1[b] = 1
    
    ans = num_r * num_g * num_b
    for i in R:
        for j in G:
            ans -= calc(min(i, j), max(i, j), B1, len_b)
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
def calc(i, j, B1, len_b):
    extra = 0
    d = j - i

    
    k1 = d + j
    if k1 < len_b:
        extra += B1[k1]

    
    k2 = i - d
    if 0 <= k2 and k2 < len_b:
        extra += B1[k2]

    
    s = i + j
    if s % 2 == 0:
        k3 = s // 2
        if k3 < len_b:
            extra += B1[k3]
    return extra

def main():
    N = int(input())
    S = input()
    RGB = {'R': [], 'G': [], 'B': []}
    for i in range(N):
        RGB[S[i]].append(i)
    num_r = len(RGB['R'])
    num_g = len(RGB['G'])
    num_b = len(RGB['B'])
    if num_r == 0 or num_g == 0 or num_b == 0:
        print(0)
        return

    len_b = (RGB['B'][-1] + 1)
    B1 = [0]*len_b
    for b in RGB['B']:
        B1[b] = 1
    
    ans = num_r * num_g * num_b
    for i in RGB['R']:
        for j in RGB['G']:
            ans -= calc(min(i, j), max(i, j), B1, len_b)
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
def calc(i, j, B1, len_b):
    extra = 0
    d = j - i

    
    k1 = d + j
    if k1 < len_b:
        extra += B1[k1]

    
    k2 = i - d
    if 0 <= k2 and k2 < len_b:
        extra += B1[k2]

    
    s = i + j
    if s % 2 == 0:
        k3 = s // 2
        if k3 < len_b:
            extra += B1[k3]
    return extra

def main():
    N = int(input())
    S = input()
    RGB = {'R': [], 'G': [], 'B': []}
    for i in range(N):
        RGB[S[i]].append(i)
    num_r = len(RGB['R'])
    num_g = len(RGB['G'])
    num_b = len(RGB['B'])
    if num_r == 0 or num_g == 0 or num_b == 0:
        print(0)
        return

    len_b = (RGB['B'][-1] + 1)
    B1 = [0]*len_b
    for b in RGB['B']:
        B1[b] = 1
    
    ans = num_r * num_g * num_b
    for i in RGB['R']:
        for j in RGB['G']:
            ans -= calc(min(i, j), max(i, j), B1, len_b)
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
def calc(i, j, B1, len_b):
    extra = 0
    d = j - i

    
    k1 = d + j
    if k1 < len_b:
        extra += B1[k1]

    
    k2 = i - d
    if 0 <= k2 and k2 < len_b:
        extra += B1[k2]

    
    s = i + j
    if s % 2 == 0:
        k3 = s // 2
        if k3 < len_b:
            extra += B1[k3]
    return extra

def main():
    N = int(input())
    S = input()
    R, G, B = [], [], []
    num_r, num_g, num_b = 0, 0, 0
    for i in range(N):
        s = S[i]
        if s == 'R':
            R.append(i)
            num_r += 1
        elif s == 'G':
            G.append(i)
            num_g += 1
        elif s == 'B':
            B.append(i)
            num_b += 1
    if len(R) == 0 or len(G) == 0 or len(B) == 0:
        print(0)
        return
    len_b = (B[-1] + 1)
    B1 = [0]*len_b
    for b in B:
        B1[b] = 1
    
    ans = 0
    for i in R:
        for j in G:
            ans += num_b
            if i < j:
                ans -= calc(i, j, B1, len_b)
            else:
                ans -= calc(j, i, B1, len_b)
    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-2):
    for j in range(i+1,n-1):
        if 2*j-i <= n-1:
            if s[2*j-i] != s[i] and s[i] != s[j] and s[j] != s[2*j-i]:
                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(0, N-2):
    for j in range(i+1, N-1):
        k = j+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()

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

ans = len(r) * len(g) * len(b)

for i in range(1, int((n-1)/2)+1):
    for j in range(n-i-i, 0, -1):
        if s[j-1] != s[j+i-1] and s[j+i-1] != s[j+i+i-1] and s[j+i+i-1] != s[j-1]:
            ans -= 1

print(ans)

Solution (Python):
n = int(input())
s = input()
r = s.count("R")
g = s.count("G")
b = s.count("B")
ans = r * g * b
for i in range(n-2):
  for j in range(i+1, n-1):
    k = 2*j - i
    if k < n and 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()
cnt=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]!=S[k]!=S[i]:
            cnt-=1
print(cnt)

Solution (Python):
import numpy as np
n = int(input())
s = input()

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

counts = 0
for i in range(n):
  for j in range(i+1, n):
    k = 2*j-i
    if(k >= n):
      break
    if(s[i] != s[j] and s[j]!=s[k] and s[k]!=s[i]):
      counts += 1
print(r*g*b - counts)

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

r = 0
g = 0
b = 0

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

rgb_tot = r*g*b


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

print(rgb_tot)

Solution (Python):





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

def solve(N, S):
    R = [1] if S[0] == "R" else [0]
    G = [1] if S[0] == "G" else [0]
    B = [1] if S[0] == "B" else [0]
    
    for i in range(1, N):
        char = S[i]

        if char == "R":
            R.append(R[-1] + 1)
            G.append(G[-1])
            B.append(B[-1])
        elif char == "G":
            R.append(R[-1])
            G.append(G[-1] + 1)
            B.append(B[-1])
        else:
            R.append(R[-1])
            G.append(G[-1])
            B.append(B[-1] + 1)
    
    count = 0

    for i in range(N-2):
        for j in range(i+1, N-1):
            if S[i] == S[j]:
                continue
            
            if S[i] != "R" and S[j] != "R":
                prefix = R
                char = "R"
            elif S[i] != "G" and S[j] != "G":
                prefix = G
                char = "G"
            else:
                prefix = B
                char = "B"

            k = 2*j-i

            count += prefix[-1] - prefix[j]
            if k < N and S[k] == char:
                count -= 1     
                
    return count

print(solve(N, S))

Solution (Python):
n=int(input())
s=input()
p=s.count('R')*s.count('G')*s.count('B')

for i in range(0,n-2):
    for j in range(1,int((n-i)/2)+1):
        if i+j*2>=n:
            break
        if s[i]!=s[i+j] and s[i]!=s[i+j*2] and s[i+j*2]!=s[i+j]:
            p-=1
print(p)

Solution (Python):
import collections

N = int(input())

S = list(input())
ScountList = collections.Counter(S)
result = ScountList['R'] * ScountList['G'] * ScountList['B']

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

Solution (Python):
N = int(input())
S = list(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*g*b

for i in range(1,N-1):
  for j in range(1,min(i,N-i-1)+1):
    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=input()

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

ans=r*g*b
for i in range(N-2):
  for j in range(i+1,N-1):
    if 2*j-i>=N:
      break
    elif S[i]!=S[j] and S[j]!=S[2*j-i] and S[2*j-i]!=S[i]:
      ans-=1
print(ans)

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

r = sum([1 for x in s if x == 'R'])
g = sum([1 for x in s if x == 'G'])
b = sum([1 for x in s if x == 'B'])
ans = r * g * b

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

s_r = s.count("R")
s_g = s.count("G")
s_b = s.count("B")

def noteq(a, b, c):
  if a != b and b != c and c != a:
    return True
  else:
    return False

ans = s_r * s_g * s_b

for i in range(n):
  for d in range(n):
    j = i + d
    k = j + d
    if k >= n:
      break;
    else:
      a = s[i]
      b = s[j]
      c = s[k]
      if noteq(a, b, c):
        ans -= 1
    
print(ans)

Solution (Python):
import collections

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

aaa = collections.Counter(s)
s_r = aaa["R"]
s_g = aaa["G"]
s_b = aaa["B"]

def noteq(a, b, c):
  if a != b and b != c and c != a:
    return True
  else:
    return False

ans = s_r * s_g * s_b

for i in range(n):
  for d in range(n):
    j = i + d
    k = j + d
    if k >= n:
      break;
    else:
      a = s[i]
      b = s[j]
      c = s[k]
      if noteq(a, b, c):
        ans -= 1
    
print(ans)

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

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

ans= G*R*B

if n<=2:
	print(0)
else:
	for i in range(n-2):
		for j in range(min(i+1,n-2-i)):
			if s[i+1]!=s[i-j] and s[i+1]!=s[i+2+j] and s[i-j]!=s[i+2+j]:
				ans-=1
	print(ans)

Solution (Python):
def contains(n, s):
	r, g, b = 0, 0, 0
	for i in s:
		if i == 'R':
			r += 1
		if i == 'G':
			g += 1
		if i == 'B':
			b += 1
	ans = r * g * b
	for i in range(len(s)):
		for j in range(i + 1, len(s)):
			k = j - i + j
			if k < len(s):
				if (s[i] == 'R' or s[j] == 'R' or s[k] == 'R') and (s[i] == 'G' or s[j] == 'G' or s[k] == 'G') and (s[i] == 'B' or s[j] == 'B' or s[k] == 'B'):
					
					ans -= 1
	return ans


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

Solution (Python):

import os
import sys
from io import BytesIO, IOBase

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

from collections import defaultdict
from itertools import permutations
from bisect import bisect_left

input()
s = input()

d = defaultdict(list)

for idx, c in enumerate(s):
    d[c].append(idx)

ans = 0

for first, second, third in permutations("RGB"):
    for v1 in d[first]:
        for v2 in d[second]:

            if v2 < v1:
                continue

            ans += len(d[third]) - bisect_left(d[third], v2)
            dist = v2 - v1

            dist_idx_check = bisect_left(d[third], v2 + dist)

            if dist_idx_check < len(d[third]) and d[third][dist_idx_check] == v2 + dist:
                ans -= 1

print(ans)

Solution (Python):
N = int(input())
S = [i for i in input()]
r = 0
g = 0
b = 0

for i in S:
    if i == "R":
        r += 1
    elif i == "G":
        g += 1
    else:
        b += 1

ans = r * g * b

for i in range(N):
    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[k]!=S[i]:
            ans -= 1
 
print(ans)

Solution (Python):
n = int(input())
s = input()
bc = n*[0]
gc = n*[0]
rc = n*[0]
for i in range(1, n+1):
    mi = n - i
    if s[n - i] == 'B':
        if i == 1:
            bc[mi] = 1
        else:
            bc[mi] = bc[mi+1] + 1
            rc[mi] = rc[mi+1]
            gc[mi] = gc[mi+1]
    elif s[mi] == 'R':
        if i == 1:
            rc[mi] = 1
        else:
            rc[mi] = rc[mi+1] + 1
            bc[mi] = bc[mi+1]
            gc[mi] = gc[mi+1]
    elif s[mi] == 'G':
        if i ==1:
            gc[mi] = 1
        else:
            gc[mi] = gc[mi+1] + 1
            rc[mi] = rc[mi+1]
            bc[mi] = bc[mi+1]
si = 0
for i in range(0, n - 1):
    
    for j in range(i+1, n-1):
        if s[j] != s[i]:
            d = j-i 
            if j+d < n:
                nc = s[j+d]
                if nc in [s[i], s[j]]:
                    l = list(set(['R', 'G', 'B']) - set([s[i], s[j]]))
                    if l[0] == 'R':
                        si+= rc[j+1]
                    elif l[0] == 'G':
                        si += gc[j+1] 
                    else:
                        si += bc[j+1]
                else:
                    l = list(set(['R', 'G', 'B']) - set([s[i], s[j]]))
                    if l[0] == 'R':
                        si+= rc[j+1]-1
                    elif l[0] == 'G':
                        si += gc[j+1] -1
                    else:
                        si += bc[j+1] - 1
            else:
                l = list(set(['R', 'G', 'B']) - set([s[i], s[j]]))
                if l[0] == 'R':
                    si+= rc[j+1]
                elif l[0] == 'G':
                    si += gc[j+1] 
                else:
                    si += bc[j+1]
print(si)

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

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

Solution (Python):

import sys
import collections
from functools import reduce

def solve(N: int, S: str):
    c = collections.Counter(S)
    if len(c.keys()) < 3:
        print(0)
        return
    rgb = reduce(lambda x, y: x*y, [int(i) for i in c.values()])
    cnt = 0
    for i in range(N):
        for j in range(i + 1, N):
            k = 2 * j - i
            if k > N - 1:
                continue
            if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                cnt += 1
    print(rgb - cnt)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    S = next(tokens)  
    solve(N, S)

if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
s = list(input())
r_pos = []
g_pos = []
b_pos = []

for i in range(n):
    st = s[i]
    if st == "R":
        r_pos.append(i)

    if st == "G":
        g_pos.append(i)

    if st == "B":
        b_pos.append(i)

colors = [r_pos, g_pos, b_pos]
if len(colors[0]) > len(colors[1]):
    colors[0], colors[1] = colors[1], colors[0]
if len(colors[0]) > len(colors[2]):
    colors[0], colors[2] = colors[2], colors[0]
if len(colors[1]) > len(colors[2]):
    colors[1], colors[2] = colors[2], colors[1]

x_num, y_num, z_num = len(colors[0]), len(colors[1]), len(colors[2])

ans = x_num * y_num * z_num
c = set(colors[2])
for a in colors[0]:
    for b in colors[1]:
        x, y = min(a, b), max(a, b)

        dif = y - x
        left_pos = x - dif
        right_pos = y + dif
        mid_pos = 4000
        if dif % 2 == 0:
            mid_pos = x + dif // 2

        left = 0
        right = 0
        mid = 0
        if left_pos in c:
            left = 1
        if right_pos in c:
            right = 1
        if mid_pos in c:
            mid = 1

        ans -= left + right + mid

print(ans)

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

R = [None] * S.count("R")
G = [None] * S.count("G")
B = [None] * S.count("B")

ri = 0
gi = 0
bi = 0
for i in range(N):
    if S[i] == "R":
        R[ri] = i+1
        ri+=1
    elif S[i] == "G":
        G[gi] = i+1
        gi += 1
    else:
        B[bi] = i+1
        bi += 1
R = set(R)
G = set(G)
B = set(B)

c = 0
for r in R:
    for g in G:
        for d in [(r + g) / 2, 2 * r - g, 2 * g - r]:
            if d in B:
                c += 1

ans = len(R) * len(G) * len(B) - c
print(ans)

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


R=[0]*N
G=[0]*N
B=[0]*N

dic={}
for c in ("R","G","B"):
  dic[c] = [0] * N

rp=0
gp=0
bp=0
for i in range(len(S)):
  if S[i]=="R":
    rp+=1
  elif S[i]=="G":
    gp+=1
  elif S[i]=="B":
    bp+=1
  dic["B"][i]=bp
  dic["R"][i]=rp
  dic["G"][i]=gp

def getRest(s1,s2):
  for c in ["R","G","B"]:
    if c not in (s1,s2):
      return c

ans = 0
for i in range(N-2):
  for j in range(i+1,N-1):
    
    if S[i] == S[j]:
      continue
    
    rest = getRest(S[i],S[j])
    ans += dic[rest][-1] - dic[rest][j]
    
    if j + (j-i) < N and S[j + (j-i)] == rest:
      ans -= 1
    
print(ans)

Solution (Python):


import numpy as np
from numba import njit

def main():
    n=int(input())
    s=np.zeros(n,dtype=np.int64)
    a=input()
    for i in range(n):
        s[i]=ord(a[i])%3
    ans=0
    d=np.zeros((3,n),dtype=np.int64)
    m=np.zeros((3,n),dtype=np.int64)
    l=np.array([[0,2,1],[2,1,0],[1,0,2]])
    

    for i in range(n):
        d[s[i],i]=1
        m[s[i],i]=1

    for j in range(3):
        for i in range(n-1,0,-1):
            m[j,i-1]+=m[j,i]

    print(f(n,s,m,d,l))

@njit
def f(n,s,m,d,l):
    ans=0
    for i in range(n-2):
        for j in range(i+1,n-1):
            if s[i]==s[j]:
                continue
            ans+=m[l[s[i]][s[j]],j+1]
            if (j*2-i)<n:
                ans-=d[l[s[i]][s[j]],j*2-i]
    return ans
        
main()

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

Solution (Python):
import sys

n = int(sys.stdin.readline().rstrip())
s = sys.stdin.readline().rstrip()

def main():
    cnt = [0] * 3
    for c in s:
      if c == 'R': cnt[0] += 1
      elif c == 'G': cnt[1] += 1
      else: cnt[2] += 1
    res = cnt[0] * cnt[1] * cnt[2]
    for i in range(n - 2):
      for j in range(i + 1, n - 1):
        k = 2 * j - i
        if k >= n: break
        res -= s[i] != s[j] and s[j] != s[k] and s[k] != s[i]
    print(res)

if __name__ == '__main__':
  main()

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


R=[0]*N
G=[0]*N
B=[0]*N

dic={}
for c in ("R","G","B"):
  dic[c] = [0] * N

rp=0
gp=0
bp=0
for i in range(len(S)):
  if S[i]=="R":
    rp+=1
    dic["R"][i]=rp
  elif S[i]=="G":
    gp+=1
    dic["G"][i]=gp
  elif S[i]=="B":
    bp+=1
    dic["B"][i]=bp

def getRest(s1,s2):
  for c in ["R","G","B"]:
    if c not in (s1,s2):
      return c

ans = 0
for i in range(N-2):
  for j in range(i+1,N-1):
    
    if S[i] == S[j]:
      continue
    
    rest = getRest(S[i],S[j])
    ans += dic[rest][-1] - dic[rest][j]
    
    if j + (j-i) < N and S[j + (j-i)] == rest:
      ans -= 1
    
print(ans)

Solution (Python):
D = {"R": 1, "G": 2, "B": 4}
E = {"R": 0, "G": 1, "B": 2}
N = int(input())
T = [E[a] for a in input()]
S = [1 << t for t in T]
s = 0
for i in range(N):
    for j in range(1, min(i+1, N-i)):
        if S[i] + S[i-j] + S[i+j] == 7: s += 1

X = [[0] * N for _ in range(3)]

for i, t in enumerate(T):
    X[t][i] += 1

for i in range(3):
    for j in range(1, N):
        X[i][j] += X[i][j-1]

ans = -s
for i in range(N):
    if T[i] == 0:
        ans += X[1][i] * (X[2][-1] - X[2][i]) + X[2][i] * (X[1][-1] - X[1][i])
    if T[i] == 1:
        ans += X[2][i] * (X[0][-1] - X[0][i]) + X[0][i] * (X[2][-1] - X[2][i])
    if T[i] == 2:
        ans += X[0][i] * (X[1][-1] - X[1][i]) + X[1][i] * (X[0][-1] - X[0][i])
print(ans)

Solution (Python):
N = int(input())
S = input()
if N <= 3:
    print('0')
else:
    minuscount = 0
    Rcount = 0
    Gcount = 0
    Bcount = 0 
    for i in range(N):
        if S[i] == 'R':
            Rcount += 1
        elif S[i] == 'G':
            Gcount += 1
        else:
            Bcount += 1
        for j in range(1,N):
            if i+2*j>=N:
                break
            if S[i] != S[i+j] and S[i+j] != S[i+2*j] and S[i+2*j] != S[i]:
                minuscount += 1

    print(Rcount*Gcount*Bcount-minuscount)

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

d ={'R':set(), 'G':set(),'B':set()}


for i,x in enumerate(s):
    d[x].add(i+1)
    


ans = 1
for k in d:
    ans*=len(d[k])
    
for i in d['R']:
    for j in d['G']:
        if (i+j)%2 == 0:
            v = (i+j)//2
            if v in d['B']:ans-=1
                
for i in d['R']:
    for j in d['B']:
        if (i+j)%2 == 0:
            v = (i+j)//2
            if v in d['G']:ans-=1
                
for i in d['B']:
    for j in d['G']:
        if (i+j)%2 == 0:
            v = (i+j)//2
            if v in d['R']:ans-=1
                
    

    
    
    
    
print(ans)

Solution (Python):
def calc():
  n=int(input())
  s=list(input())
  cnt1=0
  cnt2=0
  cnt3=0
  for i in range(n):
      if s[i]=='R':
          cnt1+=1
          s[i]=1
      elif s[i]=='G':
          cnt2+=1
          s[i]=2
      elif s[i]=='B':
          cnt3+=1
          s[i]=4
  ans=cnt1*cnt2*cnt3
  for i in range(n):
      for j in range(i+1,n):
        k=2*j-i
        if k>=n:
          continue
        if s[i]+s[j]+s[k]==7:
            ans-=1
  print(ans)
calc()

Solution (Python):
n = int(input())
s = str(input())
numr = s.count("R")
numg = s.count("G")
numb = s.count("B")
ans = numr*numb*numg
for i in range(len(s)):
    if s[i] == "R":
        for j in range(len(s)):
            if s[j] == "G":
                if i<j:
                    if i+2*(j-i) < n and  s[i+2*(j-i)] == "B":
                        ans -= 1
                    if i-(j-i) >= 0 and  s[i-(j-i)] ==  "B":
                        ans -= 1
                if i>j:
                    if i-2*(i-j) >= 0 and  s[i-2*(i-j)] == "B":
                        ans -= 1
                    if i+(i-j) < n and s[i+(i-j)] ==  "B":
                        ans -= 1
                if (i+j)%2 == 0 and s[(i+j)//2] == "B":
                    ans -= 1
 
print(ans)

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

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

Solution (Python):
N = int(input())
S = input()
result = 0
'''
for i in range(N):
    print(result)
    for j in range(i+1,N):
        if S[i] != S[j]:
            for k in range(j ,N):
                if k- j == j - i:
                    continue
                if S[j] != S[k] and S[i] != S[k]:
                    result +=1
print(result)
'''
r = S.count('R')
g = S.count('G')
b = S.count('B')
target = set('RGB')
result = r * g * b
for i in range(N):
    for j in range(1, min(i, (N - i - 1)) + 1):
        if target == set(S[i] + S[i-j] + S[j + i]):
            result -= 1
print(result)

Solution (Python):

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

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

ans = R * G * B
count = 0

for i in range(N - 2):
    for j in range(i + 1, N):
        k = j * 2 - i
        if k >= N:
            break
        if {S[i], S[j], S[k]} == {"R", "G", "B"}:
            count += 1
ans -= count
print(ans)

Solution (Python):
import sys
import collections

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


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

    c = collections.Counter(S)
    ans = c["R"] * c["G"] * c["B"]

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


if __name__ == '__main__':
    main()

Solution (Python):


def main():
    n=int(input())
    s=input()
    ans=0
    d=[[0]*n for _ in range(3)]
    m=[[0]*n for _ in range(3)]
    l=[[0,2,1],[2,1,0],[1,0,2]]

    for i in range(n):
        d[ord(s[i])%3][i]=1
        m[ord(s[i])%3][i]=1

    for j in range(3):
        for i in range(n-1,0,-1):
            m[j][i-1]+=m[j][i]

    for i in range(n-2):
        for j in range(i+1,n-1):
            if s[i]==s[j]:
                continue
            ans+=m[l[ord(s[i])%3][ord(s[j])%3]][j+1]
            if j*2-i<n:
                ans-=d[l[ord(s[i])%3][ord(s[j])%3]][j*2-i]

    print(ans)
        
main()

Solution (Python):
N=int(input())
S=input()
s=0
s+=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[j]!=S[k] and S[k]!=S[i]:
      s-=1
print(s)

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

C = [0]*3
for i in range(N):
    c = "RGB".index(S[i])
    C[c]+=1

K = 0
for i in range(N-2):
    for j in range(i,N):
        k = 2*j-i
        if k>=N:break
        if len(set([S[i],S[j],S[k]]))==3:
            K+=1
print(C[0]*C[1]*C[2]-K)

Solution (Python):
K = int(input())
S = list(input())
total = 0
R,G,B = 0,0,0

for a in range(0,K):
  if S[a]=="R":
    R += 1
  elif S[a]=="G":
    G += 1
  else :
    B += 1
    
total = R*G*B

for i in range(0,K):
  for j in range(i,K):
    k = 2*j-i
    if k<K:
      if S[i] != S[k] and S[i] != S[j] and S[j] != S[k]:
        total -= 1
print(total)

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

all = r * g * b
sub = 0
for i in range(n - 2):
    for j in range(i + 1, n - 1):
        k = 2 * j - i
        if k < n and s[i] != s[j] and s[j] != s[k]and s[k] != s[i]:
            sub += 1
print(all - sub)

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

print(len(list_B)*len(list_G)*len(list_R)-count)

Solution (Python):

def main():
    n=int(input())
    s=input()
    ans=0
    d=[[0]*n for _ in range(3)]
    m=[[0]*n for _ in range(3)]
    l=[[0,2,1],[2,1,0],[1,0,2]]

    for i in range(n):
        d[ord(s[i])%3][i]=1
        m[ord(s[i])%3][i]=1

    for j in range(3):
        for i in range(n-1,0,-1):
            m[j][i-1]+=m[j][i]

    for i in range(n-2):
        for j in range(i+1,n-1):
            if s[i]==s[j]:
                continue
            ans+=m[l[ord(s[i])%3][ord(s[j])%3]][j+1]
            if j*2-i<n:
                ans-=d[l[ord(s[i])%3][ord(s[j])%3]][j*2-i]

    print(ans)
        
main()