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 heapq
import re
from itertools import permutations
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from math import factorial, sqrt, ceil, gcd
from functools import lru_cache, reduce
INF = 1 << 60
MOD = 1000000007
sys.setrecursionlimit(10 ** 7)


class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


def dijkstra_heap(s, edge, n):
    
    d = [10**20] * n
    used = [True] * n 
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d


def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr


def lcm(x, y):
    return (x * y) // gcd(x, y)


def lcm_list(numbers):
    return reduce(lcm, numbers, 1)


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


def is_prime(n):
    if n <= 1:
        return False
    p = 2
    while True:
        if p ** 2 > n:
            break
        if n % p == 0:
            return False
        p += 1
    return True



def eratosthenes(limit):
    A = [i for i in range(2, limit+1)]
    P = []

    while True:
        prime = min(A)
        
        if prime > sqrt(limit):
            break
            
        P.append(prime)
            
        i = 0
        while i < len(A):
            if A[i] % prime == 0:
                A.pop(i)
                continue
            i += 1
            
    for a in A:
        P.append(a)
            
    return P


def permutation_with_duplicates(L):

    if L == []:
        return [[]]

    else:
        ret = []

        
        S = sorted(set(L))

        for i in S:

            data = L[:]
            data.remove(i)

            for j in permutation_with_duplicates(data):
                ret.append([i] + j)

        return ret



n = int(input())
s = input()
c = []
cntr = 0
cntg = 0
cntb = 0
for i in s:
    if i == "R":
        cntr += 1
        if cntg > 0:
            c.append(["G", cntg])
            cntg = 0
        elif cntb > 0:
            c.append(["B", cntb])
            cntb = 0
    elif i == "G":
        cntg += 1
        if cntr > 0:
            c.append(["R", cntr])
            cntr = 0
        elif cntb > 0:
            c.append(["B", cntb])
            cntb = 0
    else:
        cntb += 1
        if cntr > 0:
            c.append(["R", cntr])
            cntr = 0
        elif cntg > 0:
            c.append(["G", cntg])
            cntg = 0
    
    
if cntr > 0:
    c.append(["R", cntr])
elif cntg > 0:
    c.append(["G", cntg])
elif cntb > 0:
    c.append(["B", cntb])
m = len(c)
ans = 0

for i in range(m - 2):
    cnt1 = 0
    cnt2 = 0
    if c[i][0] == "R":
        for j in range(i + 1, m):
            if c[j][0] == "G":
                cnt1 += c[j][1]
            elif c[j][0] == "B":
                cnt2 += c[j][1]
    elif c[i][0] == "G":
        for j in range(i + 1, m):
            if c[j][0] == "R":
                cnt1 += c[j][1]
            elif c[j][0] == "B":
                cnt2 += c[j][1]
    else:
        for j in range(i + 1, m):
            if c[j][0] == "R":
                cnt1 += c[j][1]
            elif c[j][0] == "G":
                cnt2 += c[j][1]
    ans += c[i][1] * cnt1 * cnt2


for i in range(n - 2):
    for j in range(i + 1, n):
        k = j + (j - i)
        if k >= n:
            break
        if sorted([s[i], s[j], s[k]]) == ["B", "G", "R"]:
            ans -= 1
print(ans)

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

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

for i in range(0,n):
    if s[i]=='R':
        r.append(i)

    elif s[i]=='G':
        g.append(i)

    elif s[i]=='B':
        b.append(i)

s3=0
rl=len(r)
gl=len(g)
bl=len(b)

for i in range(0,n-2):
    n2=int((n+i)/2)+1
    for j in range(i,n2):
        k=2*j-i
        if k<=n-1:
            
            if s[i]!=s[j] and s[i]!=s[k] and s[j]!=s[k]:
                s3+=1
s2=rl*gl*bl-s3
print(s2)

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

Solution (Python):
def main():
    n = int(input())
    s = list(input())
    count = {"G": 0, "R":0, "B":0}
    for i in s:
        count[i] += 1
    ans = count["G"]*count["R"]*count["B"]
    for j in range(n):
        for i in range(1, n//2+1):
            if j+2*i > n-1:
                continue
            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)
    
    

if __name__ == "__main__":
    main()

Solution (Python):


n=int(input())
s=list(input())
ans=0
t=[]
ss=[]
for i in range(n):
    t.append(s[i:])
    ss.append(t[i].count('R'))
    ss.append(t[i].count('G'))
    ss.append(t[i].count('B'))

for i in range(n-2):
    for j in range(i+1,n-1):
        if s[i]==s[j]:
            continue
        else:
            if s[i]=='R' and s[j]=='G'or s[i]=='G'and s[j]=='R':
                ans+=ss[3*(j+1)+2]
                if 2*j-i<n:
                    if s[2*j-i]=='B':
                        ans-=1
            if s[i]=='B' and s[j]=='R'or s[i]=='R'and s[j]=='B':
                ans+=ss[3*(1+j)+1]
                if 2*j-i<n:
                    if s[2*j-i]=='G':
                        ans-=1
            if s[i]=='B' and s[j]=='G'or s[i]=='G'and s[j]=='B':
                ans+=ss[3*j+3]
                if 2*j-i<n:
                    if s[2*j-i]=='R':
                        ans-=1
        
print(ans)

Solution (Python):
n = int(input())
s_list = list(str(input()))
r = 0
g = 0
b = 0

for i in range(n):
    if s_list[i] == 'R':
        r += 1
    elif s_list[i] == 'G':
        g += 1
    else:
        b += 1

ans = r * g * b

for d in range(1, n):
    for i in range(n):
        if i + 2 * d >= n:
            break
        if s_list[i] != s_list[i + d] and \
            s_list[i] != s_list[i + 2 * d] and \
            s_list[i + d] != s_list[i + 2 * d]:
            ans -= 1

print(ans)

Solution (Python):
import collections
n = int(input())
s = input()
c = collections.Counter(s)
raw = c['R'] * c['G'] * c['B']

for i in range(n):
    for j in range(i+1, n):
        if s[i] != s[j]:
            step = j - i
            k = j + step
            try:
                m = s[k]
            except:
                break
            if s[i] != m and s[j] != m:
                raw -= 1
print(raw)

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

Solution (Python):
import itertools

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

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

rcnt = 0
gcnt = 0
bcnt = 0

for c in S:
    if c == 'R':
        rcnt += 1
    elif c == 'G':
        gcnt += 1
    elif c == 'B':
        bcnt += 1

ans = rcnt * gcnt * bcnt
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] and S[j] != S[k] and S[i] != S[k]:
            ans -= 1
print(ans)

Solution (Python):
n = int(input())
s = str(input())
rn = s.count("R")
gn = s.count("G")
bn = s.count("B")
 
ct=0
for i in range(n):
  for j in range(i+1,n):
    k = 2*j - i
    if k >= n:
      break
    elif s[i]!=s[j] and s[j]!=s[k]and s[i]!=s[k]:
      ct += 1
 
print(rn*gn*bn-ct)

Solution (Python):
n = int(input())
s = str(input())
rn = s.count("R")
gn = s.count("G")
bn = s.count("B")

ct=0
for i in range(n):
  for j in range(i+1,n):
    if 2*j - i >= n:
      break
    elif s[i]!=s[j] and s[j]!=s[2*j-i] and s[i]!=s[2*j-i]:
      ct += 1
 
print(rn*gn*bn-ct)

Solution (Python):
import math

n = int(input())
s = input()
ex = 0

for i in range(n-2):
  for x in range(1, int(math.ceil((n-i)/2))):
    if s[i] != s[i+x] and s[i] != s[i+2*x] and s[i+x] != s[i+2*x]:
      ex += 1

print(s.count("R") * s.count("G") * s.count("B")-ex)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 6)

int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

INF = float('inf')

def solve():
    n = II()
    S = list(input())
    
    n = len(S)

    R = []
    G = []
    B = []
    rcnt = 0
    gcnt = 0
    bcnt = 0
    for idx, s in enumerate(S):
        if s == 'R':
            R.append(idx+1)
            rcnt += 1
        elif s == 'G':
            G.append(idx+1)
            gcnt += 1
        else:
            B.append(idx+1)
            bcnt += 1
    
    
    
    ans = rcnt * gcnt * bcnt
    

    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] and S[j] != S[k] and S[k] != S[i]:
                ans -= 1
    print(ans)





if __name__ == '__main__':
    solve()

Solution (Python):
from collections import Counter
import sys
input = sys.stdin.readline

def Solve(N:int,S:str) -> int:
    total = Counter(S)
    counter1 = {"R":0,"G":0,"B":0}
    ans = 0

    for i in range(N-2):
        RGB = {"R","G","B"} - {S[i]}
        counter1[S[i]]+=1
        counter2 = {"R":0,"G":0,"B":0}
        for j in range(i+1,N-1):
            counter2[S[j]]+=1
            tmp = list(RGB - {S[j]})
            if len(tmp)==1:
                target = tmp[0]
                ans+=total[target]-counter1[target]-counter2[target]
                if((j - i)+ j <= N -1 and S[(j - i)+ j] == target):
                    ans-=1
    return ans

def main():
    N = int(input())
    S = input()
    print(Solve(N,S))

if __name__ == "__main__":
    main()

Solution (Python):
from collections import Counter
N = int(input())
S = list(input())
dic = Counter(S)
dic2 = {"R":0,"G":0,"B":0}
ans = 0
for i in range(N-2):
    RGB = {"R","G","B"} - {S[i]}
    dic2[S[i]]+=1
    dic3 = {"R":0,"G":0,"B":0}
    for j in range(i+1,N-1):
        dic3[S[j]]+=1
        if len(RGB - {S[j]})!=1:
            continue
        else:
            target = list(RGB - {S[j]})[0]
            ans+=dic[target]-dic2[target]-dic3[target]
            if((j - i)+ j <= N -1):
                if(S[(j - i)+ j] == target):
                    ans-=1
print(ans)

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

for n in range(N):
    if S[n]=="R":
        R.append(n)
    elif S[n]=="G":
        G.append(n)
    else:
        B.append(n)

ANS=len(R)*len(G)*len(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[j]!=S[k] and S[k]!=S[i]:
            ANS-=1


print(ANS)

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

pls = S.count("R") * S.count("G") * S.count("B")
minus_count = 0
for i in range(len(S)):
    for j in range(i + 1, len(S)):
        if S[i] == S[j]:
            continue
        dis = j - i
        k = j + dis
        if k > len(S) - 1:
            break
        if S[i] == S[k]:
            continue
        elif S[j] == S[k]:
            continue
        minus_count += 1
print(pls - minus_count)

Solution (Python):
import math
from itertools import product
from collections import Counter


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def sums(a, N):
    ans = ((N//a)*((N//a)+1)//2)*a
    return ans


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

def complement(A, B):
    for color in colors:
        if A != color and B != color:
            return color
    

def solve():
    N = read_int()
    S = input().strip()
    right = Counter(S)
    left = Counter()
    total = 0
    for i in range(N):
        take = S[i]
        right[take] -= 1
        
        for j in range(i):
            if S[j] != S[i]:
                color = complement(S[j], S[i])
                k = 2*i-j
                total += right[color]
                if k < N and S[k] == color:
                    total -= 1
        
        left[take] += 1
    return total


if __name__ == '__main__':
    print(solve())

Solution (Python):
N=input()
S=input()
Sn=[]
R=0
G=0
B=0
for i in range(len(S)):
    if S[i]=='R':
        R+=1
        Sn.append(1)
    elif S[i]=='G':
        G+=1
        Sn.append(2)
    else:
        B+=1
        Sn.append(3)

count=0
for start in range(len(S)):
        for diff in range(1,int((len(S)-start+1)/2)):
            
            if (Sn[start]-Sn[start+diff])*(Sn[start+diff]-Sn[start+(2*diff)])*(Sn[start]-Sn[start+(2*diff)])!=0: 
                count+=1
                
print(str((R*G*B)-count))

Solution (Python):
N=input()
S=input()
Sn=[]
R=0
G=0
B=0
for i in range(len(S)):
    if S[i]=='R':
        R+=1
        Sn.append(1)
    elif S[i]=='G':
        G+=1
        Sn.append(2)
    else:
        B+=1
        Sn.append(3)

count=0
for start in range(len(S)):
        for diff in range(1,int((len(S)-start+1)/2)):
            
            if (Sn[start]-Sn[start+diff])*(Sn[start+diff]-Sn[start+(2*diff)])*(Sn[start]-Sn[start+(2*diff)])!=0: 
                count+=1
                
print(str((R*G*B)-count))

Solution (Python):
import sys

input = sys.stdin.readline


N = int(input())

S = input()

color =list("RGB")


for x in color:
    if x not in S:
        print(0)
        exit()

r = 0
g = 0
b = 0
for i in range(len(S)):
    if S[i] == color[0]:
        r+=1
    elif S[i] == color[1]:
        g+=1
    elif S[i] == color[2]:
        b+=1

all = r*g*b

sub = 0
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] or S[j] == S[k] or S[k] == S[i] :
            continue
        sub+=1

print(all - sub)

Solution (Python):
def main():
    n = int(input())
    s = input()
    s_num = []
    r = 0
    g = 0
    b = 0
    for t in range(n):
        if s[t] == "R":
            r += 1
            s_num.append(0)
        elif s[t] == "G":
            g += 1
            s_num.append(1)
        else:
            b += 1
            s_num.append(2)
    res = r * g * b
    for i in range(n - 2):
        m = (n - i - 1)//2
        for dif in range(1, m + 1):
            x = s_num[i]
            y = s_num[i + dif]
            z = s_num[i + 2 * dif]
            if (x - y) * (y - z) * (z - x) != 0:
                res -= 1
    print(res)

if __name__ == "__main__":
    main()

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

s = list(s)

k = r = b = g = count = 0

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

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

print(rbg-count)

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

res = 0
for i in range(N-2):
    n = 1
    while True:
        j = i+n
        k = j+n
        if k > N-1: break
        res += (S[i] != S[j] and S[j] != S[k] and S[k] != S[i])
        n += 1
    
    
rgb = S.count("R") * S.count("G") * S.count("B")
print(rgb - res)

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

res = 0
for i in range(N-2):
    n = 1
    while True:
        j = i+n
        k = j+n
        if k > N-1: break
        res += (S[i] != S[j] and S[j] != S[k] and S[k] != S[i])
        n += 1
    
    
rgb = S.count("R") * S.count("G") * S.count("B")
print(rgb - res)

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

res = 0
for i in range(N-2):
    n = 1
    while True:
        j = i+n
        k = j+n
        if k > N-1: break
        res += (S[i] != S[j] and S[j] != S[k] and S[k] != S[i])
        n += 1
    
    
rgb = S.count("R") * S.count("G") * S.count("B")
print(rgb - res)

Solution (Python):
import numpy as np
in_list = []
for i in range(2):
    in_list.append(input())
N,S = in_list
N = int(N)

res = 0
for i in range(N-2):
    n = 1
    while True:
        j = i+n
        k = j+n
        if k > N-1: break
        res += (S[i] != S[j] and S[j] != S[k] and S[k] != S[i])
        n += 1
    
    
rgb = S.count("R") * S.count("G") * S.count("B")
print(rgb - res)

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

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

a = 0
for i in range(n - 2):
    d = 1
    while True:
        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]:
            a += 1

        d += 1

print(r*g*b - a)

Solution (Python):
from collections import Counter

n = int(input())
s = str(input())
s_count = Counter(list(s))

ans = 1
count = 0

for i, j in s_count.items():
    count += 1
    ans *= j

if count < 3:
    ans = 0
    

for i in range(n):
    k = 1

    while i + 2 * k < n:
        l = [s[i], s[i+k], s[i+2*k]]
        if l.count('R') == 1 and l.count('G') == 1 and l.count('B') == 1:
            ans -= 1
        
        k += 1
    
print(ans)

Solution (Python):

from collections import Counter

n = int(input())
s = str(input())
s_count = Counter(list(s))

ans = 1
count = 0

for i, j in s_count.items():
    count += 1
    ans *= j

if count < 3:
    ans = 0
    

for i in range(n):
    k = 1

    while i + 2 * k < n:
        if ans == 0:
            break
        l = []
        l.append(s[i])
        l.append(s[i+k])
        l.append(s[i+2*k])
        if l.count('R') == 1 and l.count('G') == 1 and l.count('B') == 1:
            ans -= 1
        
        k += 1
    
print(ans)

Solution (Python):
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop

import collections

n = int(input())
s = input()
r,g,b = 0,0,0
for i in range(n):
    if s[i] == "R":
        r+=1
    elif s[i] == "G":
        g+=1
    else:
        b+=1
rgb = r*g*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]:
                rgb-=1
print(rgb)

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[i]!=S[k] and S[k]!=S[j]:
      ans-=1
      
print(ans)

Solution (Python):

import sys
from collections import defaultdict

def solve(N: int, S: str):
    sub = 0
    rgb = defaultdict(int)
    for i in range(N):
        rgb[S[i]] += 1
        for j in range(i + 1, N):
            if 2 * j - i >= N:
                break
            if S[i] != S[j] and S[j] != S[2 * j - i] and S[i] != S[2 * j - i]:
                sub += 1

    print(rgb["R"] * rgb["G"] * rgb["B"] - sub)
    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):

import sys


def solve(N: int, S: str):
    r = 0
    g = 0
    b = 0
    sub = 0
    for i in range(N):

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

    print(r * g * b - sub)
    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):
import math
N = int(input())
S = input()

R = len([s for s in S if s == "R"])
G = len([s for s in S if s == "G"])
B = len([s for s in S if s == "B"])

out = R * G * B
for i in range(N):
    intvl = math.ceil((N - i)/2)
    for j in range(1,intvl):
        if S[i] != S[i+j] and S[i+j] != S[i+2*j] and S[i] != S[i+2*j]:
            out -= 1
print(out)

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

c = 0
for i in range(1,n//2 +1):
  for j in range(n-2*i):
    if a[j] != a[j+i] and a[j+i] != a[j+2*i] and a[j] != a[j+2*i]:
      c+=1

ans = a.count("R") * a.count("G") * a.count("B") - c
print(ans)

Solution (Python):





import sys,os
from io import BytesIO, IOBase
import collections,itertools,bisect,heapq,math,string


BUFSIZE = 8192

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

def main():
    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-1:
                if s[i]!=s[j] and s[i]!=s[k] and s[j]!=s[k]:
                    ans-=1
    print(ans)

if __name__ == "__main__":
    main()

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

rc=s.count('R')
gc=s.count('G')
bc=s.count('B')

ans=rc*gc*bc
for i in range(n):
    for j in range(i+1,n-1):
        k=2*j -i

        if k >= n or s[i] == s[j] or s[j] == s[k] or s[i] == s[k]:
            continue

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

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


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

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

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


for i in range(N-2):
    for j in range(i+1, N-1):
        k = j + (j-i)
        if k > N-1:
            continue
        if len(set([S[i], S[j], S[k]])) == 3:
            sum_rgb -= 1
print(sum_rgb)

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:
            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 , g, b = S.count("R"), S.count("G"), S.count("B")
ans = r * g * b

for i in range(N-2):
    for d in range(1,N//2+1):
        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):
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-2):
    for d in range(N//2+1):
        j = i + d
        k = j + d
        if k < N:
            if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
                ans -= 1
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 9)
input = sys.stdin.readline
from itertools import permutations,  combinations, accumulate
from functools import *
from collections import deque, defaultdict, Counter
from heapq import heapify, heappop, heappush, heappushpop

INF = float('inf')
NIL = - 1


N = int(input())
S = input().rstrip()

ret = 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]:
            ret += 1

print(S.count('R') * S.count('G') * S.count('B') - ret)

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-2):
    for j in range(i+1, N-1):
        k = 2*(j+1) - (i+1) 
        if k <= N:
            if S[i] != S[j] and S[j] != S[k-1] and S[k-1] != 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')

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:
            """
            if s[i] == s[j]:
                continue
            if s[j] == s[k]:
                continue
            if s[k] == s[i]:
                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()

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:
            if s[i] == s[j]:
                continue
            if s[j] == s[k]:
                continue
            if s[k] == s[i]:
                continue
            ans -= 1
print(ans)

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

"""
for i in range(n-2):
    for j in range(i+1, n-1):
        for k in range(j+1, n):
"""

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

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

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

"""
for i in range(n-2):
    for j in range(i+1, n-1):
        for k in range(j+1, n):
"""

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

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

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

"""
for i in range(n-2):
    for j in range(i+1, n-1):
        for k in range(j+1, n):
"""

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