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:
n = int(input())
s = input()
t = s.count("R")*s.count("G")*s.count("B")
for i in range(n):
    for j in range(i+1, n):
        k = 2*j - i
        if k < n and s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
            t -= 1

print(t)

Submission status: Accepted

Solution code:
from collections import deque, Counter
from itertools import chain, combinations
import json
# import numpy as np
import bisect
import sys
import math
import bisect

sys.setrecursionlimit(10 ** 8)
# mem = {}

M = 10 ** 9 + 7


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

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

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

    return res


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

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

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

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

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

    print(RGB - rm)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

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

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

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

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

Submission status: Accepted

Solution code:
# import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(10000)


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

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

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

    all = [R,G,B]

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

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

    count = 0

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

    res -= count
    print(res)




main()

Submission status: Accepted

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

S = input().rstrip()

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

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

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

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


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

Submission status: Accepted

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

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

#全体からj-i=k-jの場合を引く
sub=0
for i in range(0,N):
  for j in range(i+1,N):
    if S[i]==S[j]:
      continue

    k = 2*j -i
    #i,j,kは等差数列なのでkは決まる, k>=Nは不適なのでスキップ
    if k>=N or S[k]==S[i] or S[k]==S[j]:
      continue
    
    sub += 1
print(al_num - sub)
# O(N^2)

Submission status: Accepted

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

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

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

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

print(ans)

Submission status: Accepted

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

R = 0
G = 0
B = 0

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

ans = R*G*B

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

print(ans)

Submission status: Accepted

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


main()

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest

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

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

    print(ans)


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """4
RRGB"""
        output = """1"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB"""
        output = """1800"""
        self.assertIO(input, output)

if __name__ == "__main__":
    #unittest.main()
    resolve()

Submission status: Accepted

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

count = 0

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

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

    R_count[N - i - 1] = R
    G_count[N-i-1] = G
    B_count[N-i-1] = B
# print(R_count)
# print(G_count)
# print(B_count)

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

Submission status: Accepted

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

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

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest

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

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

    print(ans)


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """4
RRGB"""
        output = """1"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB"""
        output = """1800"""
        self.assertIO(input, output)

if __name__ == "__main__":
    #unittest.main()
    resolve()

Submission status: Accepted

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

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

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

print(max_ans - count_not_ans)

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-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:
#!/usr/bin/env python3
n = int(input())
s = input()
ans = s.count("R") * s.count("G") * s.count("B")
for i in range(n):
    for j in range(i + 1, n):
        k = j * 2 - i
        if k >= n:
            continue
        if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
            ans -= 1
print(ans)

Submission status: Accepted

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

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

print(cnt)

Submission status: Accepted

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

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

print(res)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

ans = r_cnt * g_cnt * b_cnt

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

print(ans)

Submission status: Accepted

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

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

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

print(total)

Submission status: Accepted

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

print(A*B*C-k)

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

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


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

print(sum)

Submission status: Accepted

Solution code:
#
# Written by NoKnowledgeGG @YlePhan
# ('ω')
#
#import math
#mod = 10**9+7
#import itertools
#import fractions
#import numpy as np
#mod = 10**4 + 7
"""def kiri(n,m):
  r_ = n / m
  if (r_ - (n // m)) > 0:
    return (n//m) + 1
  else:
    return (n//m)"""

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

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

"""
def YEILD():
  yield 'one'
  yield 'two'
  yield 'three'
generator = YEILD()
print(next(generator))
print(next(generator))
print(next(generator))
"""
"""def gcd_(a,b):
  if b == 0:#結局はc,0の最大公約数はcなのに
    return a
  return gcd_(a,a % b) # a = p * b + q"""
"""def extgcd(a,b,x,y):
  d = a
  if b!=0:
    d = extgcd(b,a%b,y,x)
    y -= (a//b) * x
    print(x,y)
  else:
    x = 1
    y = 0
  return d"""

# 最大公約数 と 最小公倍数
"""def gcd(a,b):
  if b == 0:
    return a
  return gcd(b, a % b)
def lcm(a,b):
  g = gcd(a,b)
  return (a * b) // g"""

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

# 円周率 pi
"""import math
pi_ = math.pi"""

# エラトステネスの篩
"""def Erato(n):
    prime = []
    is_prime = [True] * (n+1)
    p = 0
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2,n+1):
      if is_prime[i] == True:
        prime.append(i)
        p += 1
        for j in range(2 * i,n+1,i):
          is_prime[j] = False
    return p"""

def readInts():
  return list(map(int,input().split()))
mod = 10**9 + 7
def main():
  n = int(input())
  S = input()
  # あらかじめ iとjとkを保存しとけばよい

  R_ = [i for i in range(n) if S[i] == 'R']
  G_ = [j for j in range(n) if S[j] == 'G']
  B_ = [k for k in range(n) if S[k] == 'B']
  #print(R_,G_,B_,sep='\n')
  #二つが決まればもう1つが決まる?
  ans = len(R_) * len(G_) * len(B_)
  #now = 0
  for i in range(n):
    for d in range(n):
      j = i+d
      k = j+d
      if k >= n:
        break
      if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
        ans -=1
        
  print(ans)

  

if __name__ == '__main__':
  main()

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    s = input()

    r, g, b = 0, 0, 0
    for i, c in enumerate(s, 1):
        if c == 'R':
            r += 1
        elif c == 'G':
            g += 1
        else:
            b += 1

    ans = r * g * b
    sub = 0
    for j in range(N):
        for i in range(j):
            if s[i] == s[j]:
                continue
            k = (j - i) + j
            if k >= N:
                continue
            if (s[i] == s[k]) or (s[j] == s[k]):
                continue
            sub += 1  # 相異なる3文字で(j-i)==(k-j)を満たしている数

    ans -= sub

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
s = input()
 
r_cnt = s.count('R')
g_cnt = s.count('G')
b_cnt = s.count('B')
 
ans = r_cnt * g_cnt * b_cnt
 
for i in range(n):
    for d in range(n):
        j = i + d
        k = j + d
        if k >= n:break
        if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
            ans -= 1
 
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
s = "0" + s
ans = s.count("R") * s.count("G") * s.count("B")

for i in range(1,n):
    for j in range(i + 1, n + 1):
        if 2 * j - i <= n:
            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:
import sys
input = sys.stdin.readline

def main():
    n = int(input())
    li = list(input().rstrip())

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

    for i in range(n):
        if li[i] == 'R':
            r.append(i)
        elif li[i] == 'G':
            g.append(i)
        elif li[i] == 'B':
            b.append(i)

    if (len(r) < 1 or len(g) < 1) or len(b) < 1:
        print(0)
    else:
        all_c = len(r) * len(g) * len(b)

        for i in range(n):
            for j in range(i+1,n):
                k = 2*j-i
                if k >= n:
                    continue

                if (li[i] != li[j] and li[j] != li[k]) and li[i] != li[k]:
                    all_c -= 1

        print(all_c)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
S = input()

R = 0
G = 0
B = 0

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

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

print(R*G*B-count)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys


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


def resolve():
    n = int(input())
    s = input()

    count_dic = {}

    for i in range(n):
        count_dic[str(i)+"R"] = s.count("R", i, n)
        count_dic[str(i)+"G"] = s.count("G", i, n)
        count_dic[str(i)+"B"] = s.count("B", i, n)

    all_count = n* (n-1) * (n-2)
    count = 0
    for i in range(n):
        for j in range(i+1, n):
            if s[i] == s[j]:
                continue

            if s[i] == "R" and s[j] == "G":
                target_str = "B"
            elif s[i] == "R" and s[j] == "B":
                target_str = "G"
            elif s[i] == "G" and s[j] == "R":
                target_str = "B"
            elif s[i] == "G" and s[j] == "B":
                target_str = "R"
            elif s[i] == "B" and s[j] == "R":
                target_str = "G"
            elif s[i] == "B" and s[j] == "G":
                target_str = "R"

            count += count_dic[str(j)+target_str]

            if 2*j-i < n:
                if target_str == s[2*j-i]:
                    count -= 1
            """
            for k in range(j+1, n):
                if j - i == k -j:
                    continue
                
                if s[i] == s[k] or s[j] == s[k]:
                    continue

                count += 1
            """

    print(count)


if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:

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

R = S.count("R")
G = S.count("G")
B = S.count("B")
total = 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]:
            total -= 1

print(total)

Submission status: Accepted

Solution code:
from bisect import bisect_right
N = int(input())
S = input().strip()
R = []
G = []
B = []
Ron = [0 for _ in range(len(S))]
Gon = [0 for _ in range(len(S))]
Bon = [0 for _ in range(len(S))]
for i in range(N):
    if S[i]=="R":
        R.append(i)
        Ron[i]=1
    elif S[i]=="G":
        G.append(i)
        Gon[i]=1
    else:
        B.append(i)
        Bon[i]=1
cnt = 0
for i in range(N):
    if Ron[i]==1:
        #RGB
        indr = i
        ind0 = bisect_right(G,indr)
        for j in range(ind0,len(G)):
            indg = G[j]
            if len(B)>0:
                ind1 = bisect_right(B,indg)
                if 2*indg-indr>B[-1] or Bon[2*indg-indr]==0 or Bon[2*indg-indr]==1 and 2*indg-indr<B[ind1]:
                    cnt += len(B)-ind1
                else:
                    cnt += len(B)-ind1-1
        #RBG
        ind0 = bisect_right(B,indr)
        for j in range(ind0,len(B)):
            indb = B[j]
            if len(G)>0:
                ind1 = bisect_right(G,indb)
                if 2*indb-indr>G[-1] or Gon[2*indb-indr]==0 or Gon[2*indb-indr]==1 and 2*indb-indr<G[ind1]:
                    cnt += len(G)-ind1
                else:
                    cnt += len(G)-ind1-1
    elif Gon[i]==1:
        #GBR
        indg = i
        ind0 = bisect_right(B,indg)
        for j in range(ind0,len(B)):
            indb = B[j]
            if len(R)>0:
                ind1 = bisect_right(R,indb)
                if 2*indb-indg>R[-1] or Ron[2*indb-indg]==0 or Ron[2*indb-indg]==1 and 2*indb-indg<R[ind1]:
                    cnt += len(R)-ind1
                else:
                    cnt += len(R)-ind1-1
        #GRB
        ind0 = bisect_right(R,indg)
        for j in range(ind0,len(R)):
            indr = R[j]
            if len(B)>0:
                ind1 = bisect_right(B,indr)
                if 2*indr-indg>B[-1] or Bon[2*indr-indg]==0 or Bon[2*indr-indg]==1 and 2*indr-indg<B[ind1]:
                    cnt += len(B)-ind1
                else:
                    cnt += len(B)-ind1-1
    elif Bon[i]==1:
        #BRG
        indb = i
        ind0 = bisect_right(R,indb)
        for j in range(ind0,len(R)):
            indr = R[j]
            if len(G)>0:
                ind1 = bisect_right(G,indr)
                if 2*indr-indb>G[-1] or Gon[2*indr-indb]==0 or Gon[2*indr-indb]==1 and 2*indr-indb<G[ind1]:
                    cnt += len(G)-ind1
                else:
                    cnt += len(G)-ind1-1
        #BGR
        ind0 = bisect_right(G,indb)
        for j in range(ind0,len(G)):
            indg = G[j]
            if len(R)>0:
                ind1 = bisect_right(R,indg)
                if 2*indg-indb>R[-1] or Ron[2*indg-indb]==0 or Ron[2*indg-indb]==1 and 2*indg-indb<R[ind1]:
                    cnt += len(R)-ind1
                else:
                    cnt += len(R)-ind1-1
print(cnt)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
S = input()

R = 0
G = 0
B = 0

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

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

print(R*G*B-count)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

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

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

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

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
s = input()
R = 0
G = 0
B = 0

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

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

print(ans)