Problem:


Score : 400 points

Problem Statement

We have a string S of length N consisting of R, G, and B.

Find the number of triples (i,~j,~k)~(1 \leq i < j < k \leq N) that satisfy both of the following conditions:

S_i \neq S_j, S_i \neq S_k, and S_j \neq S_k.

j - i \neq k - j.

Constraints

1 \leq N \leq 4000

S is a string of length N consisting of R, G, and B.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of triplets in question.

Sample Input 1

4
RRGB

Sample Output 1

1

Only the triplet (1,~3,~4) satisfies both conditions. The triplet (2,~3,~4) satisfies the first condition but not the second, so it does not count.

Sample Input 2

39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB

Sample Output 2

1800



Sample input:
4
RRGB


Sample output:
1

Submission status: Accepted

Solution code:
from collections import Counter
N=int(input())
S=input()
C=Counter(S)
nr=C["R"]
ng=C["G"]
nb=C["B"]
ans=nr*ng*nb
for i in range(N-2):
    for j in range(i+1,N-1):
        k=j+j-i
        if k<N:
            if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
                ans-=1

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
R=[]
G={}
B=[]
for i in range(n):
  if s[i]=='R':
    R.append(i+1)
  elif s[i]=='G':
    G[i+1]=G.get(i+1,0)+1
  else:
    B.append(i+1)
res=len(R)*len(G)*len(B)
for i in R:
  for j in B:
    if ((i+j)%2==0 and G.get((i+j)//2,0)==1):
      res-=1
    if G.get(2*max(i,j)-min(i,j),0)==1:
      res-=1
    if G.get(2*min(i,j)-max(i,j),0)==1:
      res-=1
print(res)

Submission status: Accepted

Solution code:
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 d in range(1, n):
        j = i + d
        k = j + d
        if k >= n:
            break
        if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
            ans -= 1

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = str(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(1, N):
        j = i + d
        k = j + d
        if k >= N:break
        if S[i]!=S[j] and S[i]!=S[k] and S[j]!=S[k]:
            ans -= 1

print(ans)

Submission status: Accepted

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

num_R = S.count('R')
num_G = S.count('G')
num_B = S.count('B')
S = '_' + S

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

Submission status: Accepted

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

Submission status: Accepted

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

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

for i in range(n-2):
    for k in range(i+1,n-1):
        l=k+(k-i)
        if l>=n:
            continue
        if s[i]!=s[k] and s[i]!=s[l] and s[k]!=s[l]:
            al-=1

print(al)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
r = []
g = []
b = [0]*40000
for i,j in enumerate(s):
  if j == 'R':
    r.append(i)
  if j == 'G':
    g.append(i)
  if j == 'B':
    b[i] += 1
cnt = len(r)*len(g)*sum(b)
for i in r:
  for j in g:
    if b[2*j-i] == 1 :
      cnt -= 1
    if b[2*i-j] == 1 :
      cnt -= 1
    if (i+j)%2 == 0 and b[(i+j)//2] == 1:
      cnt -= 1
print(cnt)

Submission status: Accepted

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

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

#l = ["RGB", "RBG", "GRB", "GBR", "BRG", "BGR"]

lR = [0]*n
lG = [0]*n
lB = [0]*n

for i in range(len(s)):
    if s[i] == "R":
        lR[i] += 1
    elif s[i] == "G":
        lG[i] += 1
    else:
        lB[i] += 1

#print(lR)
#print(lG)
#print(lB)

for i in range(n):
    j = 1
    if lR[i]:
        while i-j >= 0 and i+j < n:
            if lG[i+j]!=0 and lG[i+j] == lB[i-j]:
                total -= 1
            elif lG[i-j]!=0 and lG[i-j] == lB[i+j]:
                total -= 1
            j += 1

    elif lG[i]:
        while i-j >= 0 and i+j < n:
            if lR[i+j]!=0 and lR[i+j] == lB[i-j]:
                total -= 1
            elif lR[i-j]!=0 and lR[i-j] == lB[i+j]:
                total -= 1
            j += 1

    elif lB[i]:
        while i-j >= 0 and i+j < n:
            if lG[i+j]!=0 and lG[i+j] == lR[i-j]:
                total -= 1
            elif lG[i-j]!=0 and lG[i-j] == lR[i+j]:
                total -= 1
            j += 1

print(total)

Submission status: Accepted

Solution code:
def solution1():
    # https://www.youtube.com/watch?v=aRdGRrsRo7I 
    n = int(input())
    s = input()
    r, g, b = 0, 0, 0
    for i in s:
        if i == 'R': r += 1
        elif i == 'G': g += 1
        else: b += 1
    
    total = r*g*b
    # all permutations of rgb are valid, to check if s[i], s[j], s[k] is a permutation of 'rgb' we could use counter. Or neat trick: ASCII value of the sum of s[i] + s[j] + s[k] is same.

    is_rgb = ord('R') + ord('G') + ord('B')

    for i in range(n):
        for j in range(i+1, n):
            # Condition 2 fails when: i - j = k - j --> k = 2*j - i
            k = 2*j - i
            if k < n:
                if ord(s[i]) + ord(s[j]) + ord(s[2*j-i]) == is_rgb:
                    total -= 1
    print(total)

solution1()

Submission status: Accepted

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

rinds, ginds, binds = [], [], set()

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

rlen, glen, blen = len(rinds), len(ginds), len(binds)


ans = 0

for i in rinds:
    for j in ginds:
        dist = abs(i-j)
        i_ = min(i, j)
        j_ = max(i, j)
        ans += blen
        # i' < j' < k
        if j_ + dist in binds:
            ans -= 1
        if dist%2==0 and i_ + dist//2 in binds:
            ans -= 1
        if i_ - dist in binds:
            ans -= 1

        # if binsearch_same(binds, blen, dist, j_):
        #     ans -= 1
        # # i'< k < j' 
        # if dist%2==0 and binsearch_same(binds, blen, dist//2, i_):
        #     ans -= 1
        # # # k < i' < j'
        # if binsearch_same(binds, blen, -dist, i_):
        #     ans -= 1

print(ans)















# n = input()
# print('Yes' if '7' in n else 'No')

# n = int(input())
# ans = 0
# for i in range(1, n+1):
#     if i%3 == 0 or i%5==0:
#         pass
#     else:
#         ans += i
# print(ans)


# k = int(input())
# def gcd(a, b):
#     if b == 0:
#         return a
#     return gcd(b, a%b)

# ans = 0
# for a in range(1, k+1):
#     for b in range(1, k+1):
#         for c in range(1, k+1):
#             ans += gcd(gcd(a, b), c)
# print(ans)

Submission status: Accepted

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

rinds, ginds, binds = [], [], []

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


rlen, glen, blen = len(rinds), len(ginds), len(binds)

def binsearch_same(binds, blen, dist, i):
    lo, hi = 0, blen-1
    while lo<=hi:
        mid = lo + (hi-lo)//2
        d = binds[mid] - i
        if dist == d:
            return True
        elif d < dist:
            lo = mid + 1
        else:
            hi = mid - 1
    return False

ans = 0

for i in rinds:
    for j in ginds:
        dist = abs(i-j)
        i_ = min(i, j)
        j_ = max(i, j)
        ans += blen
        # i' < j' < k

        if binsearch_same(binds, blen, dist, j_):
            ans -= 1
        # i'< k < j' 
        if dist%2==0 and binsearch_same(binds, blen, dist//2, i_):
            ans -= 1
        # # k < i' < j'
        if binsearch_same(binds, blen, -dist, i_):
            ans -= 1

print(ans)

Submission status: Accepted

Solution code:
# 初期入力
N= int(input())
S =input().strip()

# RGBの各index
R =set([i for i, x in enumerate(S) if x == 'R'])
G =set([i for i, x in enumerate(S) if x == 'G'])
B =set([i for i, x in enumerate(S) if x == 'B'])


count =0
rgb =[]
for i in R:
    for j in G:
        k1 =j +(j -i)
        if k1 in B: #inはlistよりsetのほうが速い
            count +=1

for i in B:
    for j in R:
        k1 =j +(j -i)
        if k1 in G: #inはlistよりsetのほうが速い
            count +=1

for i in G:
    for j in B:
        k1 =j +(j -i)
        if k1 in R: #inはlistよりsetのほうが速い
            count +=1

all =len(R) *len(G) *len(B)
print(all -count)

Submission status: Accepted

Solution code:
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(n - 2):
    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)

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.buffer.readline
import math

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


def main():
    N = int(readline())
    S = readline().decode('utf-8')

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

    for stride in range(1, math.ceil(N / 2) + 1):
        for start in range(N):
            if start + stride * 2 >= N:
                break
            else:
                if S[start] != S[start + stride] and S[start + stride] != S[start + stride * 2] and S[start + stride * 2] != S[start]:
                    total -= 1

    print(total)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from sys import stdin
import numpy as np
from numba import njit
def main():
    #入力
    readline=stdin.readline
    n=int(readline())
    s=readline().strip()
    ans=s.count("R")*s.count("G")*s.count("B")
    s=np.array([0 if s[i]=="R" else 1 if s[i]=="G" else 2 for i in range(n)],dtype=np.int64)

    print(f(n,ans,s))

@njit("i8(i8,i8,i8[:])")
def f(n,ans,s):
    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]:
                ans-=1
    return ans

if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
from sys import stdin
import numpy as np
from numba import njit
def main():
    #入力
    readline=stdin.readline
    n=int(readline())
    s=readline().strip()
    ans=s.count("R")*s.count("G")*s.count("B")
    s=np.array([0 if s[i]=="R" else 1 if s[i]=="G" else 2 for i in range(n)],dtype=np.int64)

    print(f(n,ans,s))

@njit("i8(i8,i8,i8[:])",cache=True)
def f(n,ans,s):
    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]:
                ans-=1
    return ans

if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
from sys import stdin
import numpy as np
from numba import njit
def main():
    #入力
    readline=stdin.readline
    n=int(readline())
    s=readline().strip()
    ans=s.count("R")*s.count("G")*s.count("B")
    s=np.array([0 if s[i]=="R" else 1 if s[i]=="G" else 2 for i in range(n)],dtype=np.int64)

    print(f(n,ans,s))

@njit(cache=True)
def f(n,ans,s):
    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]:
                ans-=1
    return ans

if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
from sys import stdin
import numpy as np
from numba import njit
def main():
    #入力
    readline=stdin.readline
    n=int(readline())
    s=readline().strip()
    ans=s.count("R")*s.count("G")*s.count("B")
    s=np.array([0 if s[i]=="R" else 1 if s[i]=="G" else 2 for i in range(n)],dtype=np.int64)

    print(f(n,ans,s))

@njit
def f(n,ans,s):
    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]:
                ans-=1
    return ans

if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
import sys, os, math, bisect, itertools, collections, heapq, queue
from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal

# import fractions

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)
# lcm = lambda x, y: (x * y) // fractions.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    S = iss()
    mx = S.count('R') * S.count('G') * S.count('B')

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
ans = 0
R = []
G = []
B = []


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

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


print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = str(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
p = r*g*b
for i in range(n-2):
    for j in range(int(n/2)+1):
        if i + 2*j >n-1:
            break
        if s[i] != s[i+j] and s[i] != s[i+2*j] and s[i+2*j] != s[i+j]:
            p -= 1
print(p)

Submission status: Accepted

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

from collections import Counter

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

c = [0] * 3
for v in a:
    c[v] += 1

ans = 1
for v in c:
    ans *= v

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

ans -= cnt

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
R=S.count("R")
G=S.count("G")
B=S.count("B")
count=0
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]:
            count+=1
print(R*G*B-count)

Submission status: Accepted

Solution code:
# import sys
# import math #sqrt,gcd,pi
# import decimal
# import queue # queue
# import bisect
# import heapq # priolity-queue
# import time
# from itertools import product,permutations,\
#     combinations,combinations_with_replacement
# 重複あり順列、順列、組み合わせ、重複あり組み合わせ
# import collections # deque
# from operator import itemgetter,mul
# from fractions import Fraction
# from functools import reduce

mod = int(1e9+7)
# mod = 998244353
INF = 1<<50

def readInt():
    return list(map(int,input().split()))

def main():
    n = int(input())
    s = input()
    rgb = {"R":0,"G":0,"B":0}
    for i in s:
        rgb[i] += 1
    ans = 1
    for i in rgb.keys():
        ans *= rgb[i]
    for i in range(n-2):
        for j in range(i+1,n-1):
            k = j + (j-i)
            if k>=n:
                continue
            if s[i]!=s[j] and s[i]!=s[k] and s[j]!=s[k]:
                ans -= 1
    print(ans)
    return

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
l =int(input())
s =input()
r = s.count("R")
g = s.count("G")
b = s.count("B")
A = r * g * b
B = 0
gapmax = (l - 3) // 2
for _ in range(gapmax+1):
  for i in range(l-2-2*_):
    c1, c2, c3 = s[i], s[i+1+_],s[i+2+2*_]
    if not (c1 == c2) and not (c1 == c3) and not (c2 ==c3):
      B += 1
      
print(A-B)

Submission status: Accepted

Solution code:
import collections
import sys
readline = sys.stdin.readline


def main():
    n = int(readline().rstrip())
    s = readline().rstrip()
    cnt = s.count("R") * s.count("G") * s.count("B")

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
ans = s.count('R')*s.count('G')*s.count('B')

for i in range(n):
  if s[i] == 'R':
    s[i] = 1
  elif s[i] == 'G':
    s[i] = 2
  elif s[i] == 'B':
    s[i] = 4
  
for i in range(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)

Submission status: Accepted

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

rs = s.count('R')
gs = s.count('G')
bs = s.count('B')

t = rs * gs * bs

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

print(t)

Submission status: Accepted

Solution code:
n = int(input())
s = str(input())

r = s.count('R')
g = s.count('G')
b = 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:
            break
        if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
            ans -= 1
print(ans)

Submission status: Accepted

Solution code:
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:
            continue
        if S[i]==S[j] or S[j]==S[k] or S[i]==S[k]:
            continue
        else:
            ans -=1
print(ans)

Submission status: Accepted

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

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

for start in range(0,N-2):
    for interval in range(1,N):
        try:
            if S[start] != S[start+interval] and S[start] != S[start+2*interval] and S[start+interval] != S[start+2*interval]:
                ans -= 1
        except IndexError:
            break
print(ans)

Submission status: Accepted

Solution code:
INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))
def do():
    n=INT()
    s=STR()
    r=s.count('R')
    g=s.count('G')
    b=s.count('B')
    ans=r*g*b
    #print(ans)
    for i in range(n+1):
        for j in range(1,n//2+2):
            k=i+2*j
            if k<=n-1:
                #print(ch)
                if (s[i]!=s[i+j] and s[k]!=s[i+j] and s[i]!=s[k]):
                    ans-=1
                    
    print(ans)

                
if __name__ == '__main__':
    do()

Submission status: Accepted

Solution code:
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):
        if j * 2 - i >= n:
            continue
        if s[i] == s[j] or s[j] == s[2 * j - i] or s[i] == s[2 * j - i]:
            continue
        ans -= 1

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
 
r = S.count('R')
g = S.count('G')
b = S.count('B')
 
c = 0
for i in range(N):
    for j in range(i+1, N):
        h = 2*j-i
        if h >= N:
            break
        if S[i]!=S[h] and S[i]!=S[j] and S[j]!=S[h]:
            c += 1
 
print(r*g*b-c)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N=int(input())
S=input()
r=S.count('R')
b=S.count('B')
g=S.count('G')
ans=r*b*g
for i in range(N):
    for s in range(N):
        j=i+s
        k=j+s
        if k>=N:
            break
        if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
            ans-=1
print(ans)

Submission status: Accepted

Solution code:
import collections as cl
N = int(input())
S = input()
a,b = '',''
d = cl.Counter(S)
rgb = d['R']*d['B']*d['G']
expt = 0
for i in range(N):
  a = S[i]
  for j in range(i,(N+i-1)//2+1):
    if S[j] != a:
      b = S[j]
      k = 2*j-i
      if S[k]!=b and S[k]!=a:
        expt += 1
print(rgb-expt)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
a,b = '',''
r = S.count('R')
g = S.count('G')
b = len(S)-r-g
rgb = r*g*b
expt = 0
for i in range(N):
  a = S[i]
  for j in range(i,(N+i-1)//2+1):
    if S[j] != a:
      b = S[j]
      k = 2*j-i
      if S[k]!=b and S[k]!=a:
        expt += 1
print(rgb-expt)

Submission status: Accepted

Solution code:
n=int(input())
ss=list(input())
res= ss.count('R') * ss.count('G') * ss.count('B')
for i in range(n):
  for j in range(i+1,n):
    k = 2*j-i
    if k >= n:
      continue
    if all([ss[i] != ss[j],ss[j] != ss[k],ss[k] != ss[i]]):
      res-=1
print(res)

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
stri=list(input())
cc=Counter(stri)

count=cc['R']*cc['G']*cc['B']


for i in range(n-2):
  for j in range(i+1,n-1):
    if stri[j]==stri[i]:continue
      
    lib=['R','G','B']
    lib.remove(stri[i])
    lib.remove(stri[j])
    t=lib.pop()
    
    skip_k=2*j-i
    #print(i,j,skip_k)
    if skip_k>n-1:continue
    if stri[skip_k]==t:
      count-=1
print(count)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
r = 0
g = 0
b = 0

for c in s:
    if c == "R":
        r += 1
    if c == "G":
        g += 1
    if c == "B":
        b += 1
ans = r*g*b

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

print(ans)

Submission status: Accepted

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

r = s.count('R')
g = s.count('G')
b = n - r - g
ans = r*g*b

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

Submission status: Accepted

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

r = s.count('R')
g = s.count('G')
b = len(s) - r - g
ans = r*g*b

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

Submission status: Accepted

Solution code:
n = int(input())
s = list(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,i+((n-i-1)//2)+1):
    if s[i] != s[j] and s[i] != s[2*j - i] and s[j] != s[2*j - i]:
      ans -= 1
      
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
dic={}
cntr=s.count("R")
cntg=s.count("G")
cntb=s.count("B")
for i in range(n):
    if s[i]=="R":
        dic[i]="R"
    if s[i]=="G":
        dic[i]="G"
    if s[i]=="B":
        dic[i]="B"

cnt=0
for i in range(0,n-2):
    for j in range(i+1,n-1):
        if 2*j-i<=n-1:
            if dic[i]!= dic[j] and dic[i]!=dic[2*j-i] and dic[j]!=dic[2*j-i]:
                cnt+=1
        else:
            break
print(cntr*cntg*cntb-cnt)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools
N = int(input())
S = input()
ls = list(S)

nr = len([r for r in S if r == 'R'])
nb = len([b for b in S if b == 'B'])
ng = len([g for g in S if g == 'G'])

ALL = nr * nb * ng
count = 0

RGBs = list(itertools.permutations(['R', 'G', 'B'], 3))

for i in range(N):
  for j in range(N-i*2):
    if (S[j], S[j+i], S[j+i*2]) in RGBs:
      count += 1
      
print(ALL - count)

Submission status: Accepted

Solution code:
import sys
import math
input = sys.stdin.readline
n = int(input())
s = input().strip()
def idx(c):
	if c == 'R':
		return 0
	elif c == 'G':
		return 1
	else:
		return 2

cnt = [[0] * n, [0] * n, [0] * n]
for i in range(n):
	for c in range(3):
		if i > 0:
			cnt[c][i] = cnt[c][i - 1]
	cnt[idx(s[i])][i] += 1

ans = 0
for i in range(n):
	for j in range(i + 1, n):
		if s[i] != s[j]:
			rem = 3 - idx(s[i]) - idx(s[j])
			ans += cnt[rem][j - 1] - cnt[rem][i]
			if (i + j) % 2 == 0 and idx(s[(i + j) // 2]) == rem:
				ans -= 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin
import numpy as np
def main():
    #入力
    readline=stdin.readline
    n=int(readline())
    s=readline().strip()
    t=np.array([1 if s[i]=="R" else 2 if s[i]=="G" else 3 for i in range(n)],dtype=np.int64)
    ans=t[t==1].size*t[t==2].size*t[t==3].size

    for i in range(n):
        if (n+i)%2==0:
            j=np.arange(i+1,(n+i)//2)
            k=2*j-i
        else:
            j=np.arange(i+1,(n+i)//2+1)
            k=2*j-i

        if j.size==0:
            break
        i=np.full(j.size,i,dtype=np.int64)
        tmp=t[i]*t[j]*t[k]
        ans-=tmp[tmp==6].size

    print(ans)

if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
from sys import stdin
def main():
    #入力
    readline=stdin.readline
    n=int(readline())
    s=readline().strip()
    ans=s.count("R")*s.count("G")*s.count("B")

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

    print(ans)

if __name__=="__main__":
    main()

Submission status: Accepted

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

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

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

print(r*g*b-c)

Submission status: Accepted

Solution code:
import sys
import itertools
# import numpy as np
import time
import math

sys.setrecursionlimit(10 ** 7)

from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n = int(readline())
s = input()

r_c = 0
g_c = 0
b_c = 0
for c in s:
    if c == 'R':
        r_c += 1
    if c == 'G':
        g_c += 1
    if c == 'B':
        b_c += 1
rgb_count = r_c * g_c * b_c

MAX = min(len(s) + 1, 4005)
omit_count = 0
for i in range(1, len(s) + 1):
    for j in range(i + 1, len(s) + 1):
        k = 2 * j - i
        if k >= len(s) + 1:
            continue
        if s[i - 1] != s[j - 1] and s[j - 1] != s[k - 1] and s[k - 1] != s[i - 1]:
            omit_count += 1
print(rgb_count - omit_count)

Submission status: Accepted

Solution code:
import sys
import itertools
# import numpy as np
import time
import math

sys.setrecursionlimit(10 ** 7)

from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n = int(readline())
s = input()

counter = [(0, 0, 0)]
for c in s:
    rgb = counter[-1]
    r, g, b = 0, 0, 0
    if c == 'R':
        r = 1
    if c == 'G':
        g = 1
    if c == 'B':
        b = 1
    new_rgb = (rgb[0] + r, rgb[1] + g, rgb[2] + b)
    counter.append(new_rgb)

ans = 0
MAX = 4002
x = []
for i in range(1, min(len(s) + 1, MAX)):
    for j in range(i + 1, min(len(s) + 1, MAX)):
        if s[i - 1] == s[j - 1]:
            continue
        x.append((i, j))

y = []
for p in x:
    i = p[0]
    j = p[1]
    rgb = {'R', 'G', 'B'}
    sub = {s[i - 1], s[j - 1]}

    k_s = rgb.difference(sub).pop()
    k_idx = 0
    if k_s == 'R':
        k_idx = 0
    if k_s == 'G':
        k_idx = 1
    if k_s == 'B':
        k_idx = 2

    j_cnt = counter[j]
    k_cnt = counter[len(s)]
    omit = 0
    if (2 * j - i) <= len(s):
        omit = counter[2* j - i][k_idx] - counter[2 * j - i - 1][k_idx]
    ans += (k_cnt[k_idx] - j_cnt[k_idx] - omit)

print(ans)

Submission status: Accepted

Solution code:
# D - RGB Triplets
N = int(input())
S = input()
r,b,g = 0,0,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):
        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)