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

# 組み合わせ総数
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
        if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
            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 s in range(N-2):
    for d in range(1, (N-1-s)//2+1):
        t = s + d
        u = t + d
        if S[s] != S[t] and S[t] != S[u] and S[u] != S[s]:
            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')

cnt = 0
for i in range(n):
    j = 1
    while i + 2*j <= n-1:
        # if s[i] != s[i+j] and s[i+j] != s[i+2*j] and s[i+2*j] != s[i]:
        if len({s[i], s[i+j], s[i+2*j]}) == 3:
            cnt += 1
        j += 1

print(r*g*b-cnt)

Submission status: Accepted

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

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

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

print(r*g*b-cnt)

Submission status: Accepted

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

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

ans=R*G*B-rest

print(ans)

Submission status: Accepted

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

nr = 0
ng = 0
nb = 0

for i in S:
    if i == 'R':
        nr += 1
    elif i == 'G':
        ng += 1
    elif i == 'B':
        nb += 1
    else:
        raise RuntimeError

ans = nr * ng * nb

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

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
s = input()
counter = Counter(s)
ans = counter['R'] * counter['G'] * counter['B']
for i in range(n):
    for j in range(i, n):
        k = j + (j - i)
        if k >= n:
            break
        if s[i] != s[j] != s[k] != s[i]:
            ans -= 1

print(ans)

Submission status: Accepted

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

num = len(s)
ans = s.count('R')*s.count('G')*s.count('B')

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

Submission status: Accepted

Solution code:
n = int(input())
s = input()
R_ls = [0] * n
G_ls = [0] * n
B_ls = [0] * n
R_csum = [0] * n
G_csum = [0] * n
B_csum = [0] * n
for i in range(n):
    if s[i] == 'R':
        R_ls[i] = 1
        R_csum[i] = R_csum[i-1] + 1
        G_csum[i] = G_csum[i-1]
        B_csum[i] = B_csum[i-1]
    elif s[i] == 'G':
        G_ls[i] = 1
        G_csum[i] = G_csum[i-1] + 1
        R_csum[i] = R_csum[i-1]
        B_csum[i] = B_csum[i-1]
    else:
        B_ls[i] = 1
        B_csum[i] = B_csum[i-1] + 1
        R_csum[i] = R_csum[i-1]
        G_csum[i] = G_csum[i-1]
ans = 0
for i in range(n-2):
    for j in range(i+1,n-1):
        if (s[i] == 'R' and s[j] == 'G') or (s[i] == 'G' and s[j] == 'R'):
            now = B_csum[-1] - B_csum[j]
            if j + (j-i) < n and s[2*j-i] == 'B':
                now -= 1
        elif (s[i] == 'R' and s[j] == 'B') or (s[i] == 'B' and s[j] == 'R'):
            now = G_csum[-1] - G_csum[j]
            if j + (j-i) < n and s[2*j-i] == 'G':
                now -= 1
        elif (s[i] == 'G' and s[j] == 'B') or (s[i] == 'B' and s[j] == 'G'):
            now = R_csum[-1] - R_csum[j]
            if j + (j-i) < n and s[2*j-i] == 'R':
                now -= 1
        else:
            now = 0
        ans += now
print(ans)

Submission status: Accepted

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

count = {"R":0,"G":0,"B":0}
for c in s:
  count[c] += 1

r,g,b = count.values()
ans = r*g*b
for i in range(1,(n-1)//2+1):
  for rgb in zip(s,s[i:],s[i*2:]):
    if len(set(rgb)) == 3:
      ans -=1
print(ans)

Submission status: Accepted

Solution code:
from copy import deepcopy
n = int(input())
S = input()

accum = [[0,0,0]]
dic = {"R": 0, "G": 1, "B": 2}
accum[0][dic[S[0]]] += 1
for i in range(1, n):
    new_e = deepcopy(accum[i-1])
    new_e[dic[S[i]]] += 1
    accum.append(new_e)
ans = 0
for i in range(n):
    for j in range(i+1, n):
        if S[i] != S[j]:
            if (S[i] == "R" and S[j] == "G") or (S[i] == "G" and S[j] == "R"): 
                ans += accum[n-1][dic["B"]] - accum[j][dic["B"]]
                a = dic["B"]
            elif (S[i] == "R" and S[j] == "B") or (S[i] == "B" and S[j] == "R"):
                ans += accum[n-1][dic["G"]] - accum[j][dic["G"]]
                a = dic["G"]
            else:
                ans += accum[n-1][dic["R"]] - accum[j][dic["R"]]
                a = dic["R"]
            if j + j - i < n and j + j - i > j:
                ans -= accum[j+j-i][a] - accum[j+j-i-1][a]
print(ans)

Submission status: Accepted

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

A = [0] * 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
#print(A)

Cnt = [0] * 3
for a in A:
    Cnt[a] += 1

ans = 1
for cnt in Cnt:
    ans *= cnt

# j - i = k -j　
for j in range(n):
    for i in range(j):
        k = 2*j - i
        if k < n:
            if A[i] == A[j]: continue
            if A[i] == A[k]: continue
            if A[k] == A[j]: continue
            ans -= 1
print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import Counter
input = sys.stdin.readline
n = int(input())
s = list(input())

c  = Counter(s)  

num = c['R']*c['G']*c['B']

for j in range(n):
    for d in range(1,min(j,n-j-1)+1):
        i = j-d
        k = j+d
        if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
            num -= 1
print(num)

Submission status: Accepted

Solution code:
from collections import Counter
def solve():
  N = int(input())
  S = list(input())
  c = Counter(S)
  ans = c['R']*c['G']*c['B']
  for i in range(N):
    for j in range(i+1,N):
      k = j*2-i
      if k>N-1:
        break
      if S[i]!=S[j] and S[j]!=S[k] and S[i]!=S[k]:
        ans -= 1
  return ans
print(solve())

Submission status: Accepted

Solution code:
N=int(input())
S=input()
R=0;G=1;B=2
C=["R","G","B"]
X=[[[0 for i in range(N+1)]for x in range(2)] for c in range(3)]
for c in range(3):
    for i in range(N):
        if S[i]==C[c]:
            X[c][0][i+1]=X[c][0][i]+1
        else:
            X[c][0][i+1]=X[c][0][i]
    for i in range(N-1,-1,-1):
        if S[i]==C[c]:
            X[c][1][i]=X[c][1][i+1]+1
        else:
            X[c][1][i]=X[c][1][i+1]
ans=0
for i in range(N):
    c=-1
    for cc in range(3):
        if C[cc]==S[i]:
            c=cc
            break
    for seq in [((c+1)%3,(c+2)%3),((c+2)%3,(c+1)%3)]:
        x,y=seq
        ans+=X[x][0][i]*X[y][1][i]
for i in range(N):
    x=0
    while(i+2*x<N):
        j=i+x
        k=i+2*x
        if len({S[i],S[j],S[k]})==3:
            ans-=1
        x+=1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
ans = s.count("R")
ans *= s.count("G")
ans *= s.count("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()

sr = [] 
sg = []
sb = [0] * n
kcnt = 0
for i in range(n):
  if s[i] == 'R':
    sr.append(i)
  elif s[i] == 'G':
    sg.append(i)
  else:
    sb[i] = 1
    kcnt += 1

ans = 0
for i in sr:
  for j in sg:
    nki = j*2 - i
    nkj = i*2 - j
    nkk = (j+i)
    ans += kcnt
    if  0<= nki < n and sb[nki] == 1:
        ans -= 1
    if 0<= nkj < n and sb[nkj] == 1:
        ans -= 1
    if nkk%2 == 0 and 0<= (nkk//2) < n and sb[nkk//2] == 1:
      ans -= 1

print(ans)

Submission status: Accepted

Solution code:
##愚直に組合せを計算すると、O(N^3）になるので、1番目の条件を満たして、
##2番目を満たないものを除算する方式をとる
n = int(input())
s = list(input())
##総数を計算
ans = s.count("R")*s.count("G")*s.count("B")
#(i,j)を固定してkを算出
for i in range(n):
    for j in range(i+1,n):
        k = 2*j - i
        ##indexはn-1までなのでkがn以上になるとbreak
        if k >= n:
           break
        ##kは2番目の条件を満たしていないものであるので、ここで条件1番が満たされているかをチェック
        if s[i] != s[j] and s[j] != s[k] and s[i] != s[k]:
          ans -= 1
       ##満たされていればこれは1番目を満たし、2番目を満たしていないものとなる　　　
print(ans)

Submission status: Accepted

Solution code:
def resolve():
    '''
    code here
    '''
    import collections
    N = int(input())
    S = input()

    cnt_dict = collections.Counter([item for item in S])
    ng_cnt = 0
    for i in range(N):
        for j in range(i, N):
            k = 2*j - i
            if k < N:
                if S[i] != S[j] and S[j] != S[k] and S[i] != S[k]:
                    ng_cnt += 1
    
    print(cnt_dict['R']*cnt_dict['G']*cnt_dict['B'] - ng_cnt)


if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
import sys
import functools
import collections
import operator


def resolve(in_):
    n = int(next(in_))
    s = next(in_).strip()

    c = collections.Counter()
    c[b'R'[0]] = 0
    c[b'G'[0]] = 0
    c[b'B'[0]] = 0
    c.update(s)
    ans = functools.reduce(operator.mul, c.values())
    for i in range(1, n + 1):
        for j in range(i, n + 1):
            k = j * 2 - i
            if k - 1 >= n:
                break
            si = s[i - 1]
            sj = s[j - 1]
            sk = s[k - 1]
            if si != sj and sj != sk and sk != si:
                ans -= 1
    return ans


def main():
    answer = resolve(sys.stdin.buffer)
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
s=input()
d={'R':0,'G':0,'B':0}
for i in s:
  d[i]+=1
ans=1
for i in d:
  ans*=d[i]
for k in range(n//2+1):
  for i in range(n-2*k):
    if len(set([s[i],s[i+k],s[i+2*k]]))==3:ans-=1
print(ans)

Submission status: Accepted

Solution code:

import bisect
from sys import stdin
input = stdin.readline


def main():
  N = int(input())
  S = input()[:-1]

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

  Rset = set(R)
  Gset = set(G)
  Bset = set(B)

  sum_ = 0
  for ri in R:
    for gi in G:
      if ri >= gi:
        continue
      idx = bisect.bisect_right(B, gi)
      if idx == len(B): continue
      sum_ += (len(B) - idx)
      if (2*gi - ri) in Bset:
        sum_ -= 1

  for ri in R:
    for gi in B:
      if ri >= gi:
        continue
      idx = bisect.bisect_right(G, gi)
      if idx == len(G): continue
      sum_ += (len(G) - idx)
      if (2*gi - ri) in Gset:
        sum_ -= 1

  for ri in G:
    for gi in R:
      if ri >= gi:
        continue
      idx = bisect.bisect_right(B, gi)
      if idx == len(B): continue
      sum_ += (len(B) - idx)
      if (2*gi - ri) in Bset:
        sum_ -= 1

  for ri in G:
    for gi in B:
      if ri >= gi:
        continue
      idx = bisect.bisect_right(R, gi)
      if idx == len(R): continue
      sum_ += (len(R) - idx)
      if (2*gi - ri) in Rset:
        sum_ -= 1

  for ri in B:
    for gi in R:
      if ri >= gi:
        continue
      idx = bisect.bisect_right(G, gi)
      if idx == len(G): continue
      sum_ += (len(G) - idx)
      if (2*gi - ri) in Gset:
        sum_ -= 1

  for ri in B:
    for gi in G:
      if ri >= gi:
        continue
      idx = bisect.bisect_right(R, gi)
      if idx == len(R): continue
      sum_ += (len(R) - idx)
      if (2*gi - ri) in Rset:
        sum_ -= 1

  print(sum_)


if(__name__ == '__main__'):
  main()

Submission status: Accepted

Solution code:
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque

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

num_r = s.count('R')
num_g = s.count('G')
num_b = s.count('B')

num_ng = 0
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[j] != s[k] and s[k] != s[i]:
            num_ng += 1

print(num_r*num_g*num_b-num_ng)

Submission status: Accepted

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

def get_ans(ans_,c):
    if c < 0:
        pass
    else:
        ans_ -= B_[c]
    return ans_

# N=4000
# S=[random.choice(['R','G','B']) for _ in range(N)]

S_dict={'R':set(),'G':set(),'B':set()}
for s_idx,s in enumerate(S):
    S_dict[s].add(s_idx+1)
    
# print(S_dict)
B_ = [0]*(2*N)
for s_b in S_dict['B']:
    B_[s_b]=1
    
S_R=S_dict['R']
S_G=S_dict['G']
    
ans=0
ans__=len(S_dict['B'])
for s_r in S_R:
    for s_g in S_G:
        ans_=ans__
        c_= abs(s_r-s_g)
        c_mean = 0
        if (s_r + s_g)%2 == 0:
            c_mean = (s_r + s_g)//2
            ans_ = get_ans(ans_,c_mean)
            
#         print('=====')
#         print(s_r,s_g,c_,c_mean)
        
        if s_r > s_g:
            ans_ = get_ans(ans_,s_r + c_)
            ans_ = get_ans(ans_,s_g - c_)
            
#             print(s_r + c_,s_g - c_)
            
        else:
            ans_=get_ans(ans_,s_r - c_)
            ans_=get_ans(ans_,s_g + c_)
            
#             print(s_r - c_,s_g + c_)
        
        ans+=ans_
        
#         print(ans_,ans)
        
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):
        d = j - i
        if j + d < n and s[i] != s[j] and s[j] != s[j + d] and s[j + d] != s[i]:
            ans -= 1
print(ans)

Submission status: Accepted

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

r = set([i+1 for i in range(n) if s[i] == "R"])
g = set([i+1 for i in range(n) if s[i] == "G"])
b = set([i+1 for i in range(n) if s[i] == "B"])
ans = len(r)*len(g)*len(b)


def check(s1, s2, s3):
    t = 0
    for i in range(1, n+1):
        if not i in s1:
            continue
        for j in range(i+1, n+1):
            if not j in s2:
                continue
            if (j+(j-i)) in s3:
                t += 1
    return t


ans -= check(r, g, b)
ans -= check(r, b, g)
ans -= check(g, b, r)
ans -= check(g, r, b)
ans -= check(b, r, g)
ans -= check(b, g, r)
print(ans)

Submission status: Accepted

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

freq = {'R': 0, 'G': 0, 'B': 0}
for x in s:
    freq[x] += 1

ans = freq['R'] * freq['G'] * freq['B']
for i in range(n-2):
    for j in range(i+1, n-1):
        k = j + (j-i)
        if k < n and 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()
r=s.count("R")
g=s.count("G")
b=s.count("B")
ans=r*g*b
for i in range(n):
    a=s[i]
    for j in range(i+1,n):
        b=s[j]
        d=j-i
        if j+d>=n:
            break
        c=s[j+d]
        if a!=b and b!=c and c!=a:
            ans-=1
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
ans=0
for i in range(n-2):
    if s[i]=="R":
        a,b="G","B"
    elif s[i]=="G":
        a,b="B","R"
    else:
        a,b="R","G"
    cnt=[None]*(n-i-1)
    acnt=0
    bcnt=0
    for j in range(i+1,n):
        if s[j]==a:
            acnt+=1
            cnt[j-i-1]=a
        elif s[j]==b:
            bcnt+=1
            cnt[j-i-1]=b
    tmp=acnt*bcnt
    for j in range((n-i-1)//2):
        if cnt[j]==a and cnt[(j+1)*2-1]==b:
            tmp-=1
        elif cnt[j]==b and cnt[(j+1)*2-1]==a:
            tmp-=1
    ans+=tmp
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
d = {
    'R': {},
    'G': {},
    'B': {}
}
for i, s in enumerate(S):
    d[s][i] = True
answer = len(d['R']) * len(d['G']) * len(d['B'])
for r in d['R'].keys():
    for g in d['G'].keys():
        if 2 * r - g in d['B']:
            answer -= 1
        if 2 * g - r in d['B']:
            answer -= 1
        if ((r + g) % 2 == 0 and ((r + g) // 2) in d['B']):
            answer -= 1
            

print(answer)

Submission status: Accepted

Solution code:
from collections import Counter

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

C=Counter(S)
r=C['R']
g=C['G']
b=C['B']

ans=r*g*b
# print(r,g,b)

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

print(ans)

Submission status: Accepted

Solution code:
# -*- conding: utf-8 -*-
import math
N = int(input())
S = input()

count_R = 0
count_G = 0
count_B = 0
for s in S:
  if s == "R":
    count_R += 1
  elif s == "G":
    count_G += 1
  else:
    count_B += 1

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

print(count_R * count_G * count_B - count)

Submission status: Accepted

Solution code:

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

d = {"R": 0, "G": 0, "B": 0}
for i in range(N):
    d[S[i]] += 1

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

        if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
            cnt += 1

print(d["R"] * d["G"] * d["B"] - cnt)

Submission status: Accepted

Solution code:

from bisect import bisect_left

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

d = {"R": [], "G": [], "B": []}
for i in range(N):
    d[S[i]].append(i)


def find(x, y, z):
    total = len(z)
    res = 0
    for i in x:
        for j in y:
            if i > j:
                continue

            # Find k > j
            k = bisect_left(z, j)

            # Find k == 2 * j - i
            k0 = bisect_left(z, 2 * j - i)
            flag = int(k0 >= k and k0 < total and z[k0] == 2 * j - i)
            res += total - k - flag

    return res


ans = 0
ans += find(d["R"], d["G"], d["B"])
ans += find(d["R"], d["B"], d["G"])
ans += find(d["G"], d["R"], d["B"])
ans += find(d["G"], d["B"], d["R"])
ans += find(d["B"], d["R"], d["G"])
ans += find(d["B"], d["G"], d["R"])
print(ans)

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
r,g,b = [0]*(n+1),[0]*(n+1),[0]*(n+1)
for i in range(1,n+1):
    if s[i-1]=='R':
        r[i] =r[i-1]+1
        g[i] = g[i-1]
        b[i] = b[i-1]
    elif s[i-1] =='G':
        r[i] =r[i-1]
        g[i] = g[i-1]+1
        b[i] = b[i-1]
    else:
        r[i] =r[i-1]
        g[i] = g[i-1]
        b[i] = b[i-1]+1
ans = 0
for i in range(1,n+1):
    for j in range(i+1,n+1):
        p = ['R','G','B']
        if s[i-1] ==s[j-1]:
            continue
        else:
            p.remove(s[i-1])
            p.remove(s[j-1])
            if 2*j-i-1 <n and p[0] ==s[2*j-i-1]:
                ans -=1
            if p[0]=='R':
                ans += r[-1]-r[j]
            elif p[0]=='B':
                ans += b[-1]-b[j]
            else:
                ans += g[-1]-g[j]
print(ans)

Submission status: Accepted

Solution code:
n=int(input());s=[*input()]
count=s.count('R')*s.count('G')*s.count('B')
for i in range(n+1):
  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[i]!=s[k]:
        count-=1
print(count)

Submission status: Accepted

Solution code:
n=int(input());s=[*input()];count=0
count=s.count('R')*s.count('G')*s.count('B')
for i in range(n+1):
  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[i]!=s[k]:
        count-=1
print(count)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
S = list(input())
cnt = Counter(S)
Red, Green, Blue = (cnt['R'], cnt['G'], cnt['B'])
patn = Red*Green*Blue

cnt = 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]:
            cnt += 1
        
print(patn-cnt)

Submission status: Accepted

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

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

all = r*g*b
ho = 0
for i in range(0,n):
  for j in range(i+1,n):
    if s[i] == s[j]:
      continue
    k = 2*j-i
    if k >= n:
      continue
    if s[i] == s[k] or s[j] == s[k]:
      continue
    ho += 1
print(all-ho)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


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

    # KeyInsight
    # 条件を満たす場合を直接求めるのが難しい場合は、補集合を考える

    # R, G, Bは、それぞれ独立に扱える
    r_count = s.count('R')
    g_count = s.count('G')
    b_count = s.count('B')

    ans = r_count * g_count * b_count

    # i, jを固定して全探索
    # 常にi < jとなるように添字を振るのもポイント
    for j in range(n):
        for i in range(j):
            # kは、i, jから計算
            k = 2 * j - i

            # インデックスエラーを回避
            if k < n:
                if s[i] == s[j]:
                    continue
                elif s[i] == s[k]:
                    continue
                elif s[j] == s[k]:
                    continue

                ans -= 1

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
S = input()
r,g,b = S.count('R'),S.count('G'),S.count('B')
#rgbでr,g,bがすべて異なる組み合わせ数
total = r*g*b
# そこからj-i=k-jの組み合わせを引く
for i in range(0,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[j] != S[k] and S[k] != S[i]:
            total-=1
# flg = [[[0 for i in range(N)] for j in range(N)] for k in range(N)]

print(total)

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def s(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

n = k()
s = s()
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((n-i+1)//2):
        if s[i]!=s[i+j] and s[i+j]!=s[i+2*j] and s[i+2*j]!=s[i]:
            ans-=1

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

n=int(input())
s=list(input())

cnt = Counter(s)

rn=cnt['R']
gn=cnt['G']
bn=n-rn-gn

ans=bn*gn*rn
if n>2:
  
  for x in range(n-2):
    
    y=(n-1-x)//2
    for i in range(1,y+1):
      if  (not s[x]==s[x+i]) & (not s[x]==s[x+i+i]) & (not s[x+i]==s[x+i+i]):
        ans-=1
        
  print(ans)
    
else:
  print(0)

Submission status: Accepted

Solution code:
import sys
from collections import Counter

sys.setrecursionlimit(10**7)
input = sys.stdin.readline

n = int(input())
s = input().rstrip()

cnt = Counter(s)
R = cnt['R']
G = cnt['G']
B = cnt['B']

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

print(R*G*B-cnt)

Submission status: Accepted

Solution code:
import sys
from collections import Counter

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


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

    counter = Counter(S)
    ans = counter['R'] * counter['G'] * counter['B']

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

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
from collections import Counter

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


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

    counter = Counter(S)
    ans = counter['R'] * counter['G'] * counter['B']
    
    for w in range(1, (N - 1) // 2 + 1):
        for i in range(N - 2 * w):
            if S[i] != S[i + w] and S[i] != S[i + 2 * w] and S[i + w] != S[i + 2 * w]:
                ans -= 1

    print(ans)
    return


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

ans = r*g*b

for i in range(N):
    for d in range(N):
        j = i+d
        k = i+2*d
        if k > N-1:
            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:
#!usr/bin/env pypy3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)

def main():
    N = I()
    s = S()

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

    dup = 0
    for i in range(N):
        for k in range(i+1, N):
            if (i + k) % 2 == 0:
                j = (i + k) // 2
                if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
                    dup += 1

    print(R * G * B - dup)

main()

Submission status: Accepted

Solution code:
from collections import Counter

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

counter = Counter(list(S))

ans = counter['R'] * counter['G'] * counter['B']

for d in range(1, N // 2 + 1):
    for a, b, c in zip(S, S[d:], S[2 * d:]):
        if a != b and b != c and c != a:
            ans -= 1

print(ans)

Submission status: Accepted

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

# R/G/Bそれぞれの個数
r = S.count('R')
g = S.count('G')
b = S.count('B')

# R,G,Bとなる選び方の総数。順番はRGBとは限らない
ans = r * g * b

# j-i != k-j は扱いにくい
# j-i == k-j　の数を求めて引く（余事象）
# この時、k == 2*j - i、すなわちi,jが決まればkは一意に定まる

# i = 0,1,2,...,N-3
# j = i+1,i+2,...,N-2
# k = 2*j-1

for i in range(N - 2):
    for j in range(i + 1, N - 1):
        k = 2 * j - i
        # k>=N となったらそれ以降jについてのfor回すのは無駄、次のjへ
        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/G/Bそれぞれの個数
r = S.count('R')
g = S.count('G')
b = S.count('B')

# R,G,Bとなる選び方の総数。順番はRGBとは限らない
ans = r * g * b

# j-i != k-j は扱いにくい
# j-i == k-j　の数を求めて引く（余事象）
# この時、k == 2*j - i、すなわちi,jが決まればkは一意に定まる

# i = 0,1,2,...,N-3
# j = i+1,i+2,...,N-2
# k = 2*j-1

for i in range(N - 2):
    for j in range(i + 1, N - 1):
        k = 2 * j - i
        # kが範囲内、かつ、条件1を満たしている場合
        if k <= N - 1 and S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
            ans -= 1

print(ans)

Submission status: Accepted

Solution code:
from operator import mul
from functools import reduce

def combination(n, r):
    if not 0 <= r <= n: return 0
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom

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

R = dict()
L = dict()


R["R"] = S.count("R")
L["R"] = 0
R["G"] = S.count("G")
L["G"] = 0
R["B"] = S.count("B")
L["B"] = 0

def num(n, x):
    res = 0
    for i in range(1,min(n,N-1-n) + 1):
        if S[n-i] != x and S[n+i] != x and S[n-i] != S[n+i]:
            res += 1
    return res

R[S[0]] -= 1
L[S[0]] += 1
res = 0
for i in range(1,N-1):
    x = S[i]
    R[x] -= 1
    three = sum( R[x]*L[x]for x in ["R","G","B"])
    res += three + ((N-1-i)-R[x])*L[x] + R[x]*(i-L[x]) + num(i,x)
    L[x] += 1

print(combination(N,3) - res)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
ans=0
cnt={}
cnt["R"]=0
cnt["G"]=0
cnt["B"]=0
for i in s:
    cnt[i]+=1
ans = cnt["R"] * cnt["G"] * cnt["B"]
for i in range(n-2):
    for gap in range(1,(n-i+1)//2):
        if s[i] != s[i+gap] and s[i+2*gap] != s[i+gap] and s[i+2*gap] != s[i]:
            ans-=1
print(ans)

Submission status: Accepted

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

if N<4:
    print(0)
else:
    a = [0] * N
    R, G, B = 0, 0, 0
    for i in range(N):
        if S[i] == "R":
            a[i] = 1
            R += 1
        if S[i] == "G":
            a[i] = 2
            G += 1
        if S[i] == "B":
            a[i] = 4
            B += 1

    rgb = R * G * B
    cnt = 0
    for i in range(N-2):
        for d in range(1, (N-i-1) // 2+1):
            if a[i] + a[i+d] + a[i+2*d] == 7:
                cnt += 1

    print(rgb -cnt)

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 - 2):
    for j in range(i + 1, N - 1):
        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
print(ans)