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()
rgb={"R":0,"G":0,"B":0}
for s in S:
  rgb[s]+=1
ans=rgb["R"]*rgb["G"]*rgb["B"]
for dist in range(1,N//2+1):
  left=0
  while left+dist*2<N:
    if len(set([S[left],S[left+dist],S[left+dist*2]]))==3:
      ans-=1
    left+=1
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
n = int(input())
s = list(input())
c = defaultdict(int)

for i in s:
    if i=="R":
        c["R"]+=1
    elif i=="G":
        c["G"]+=1
    else:
        c["B"]+=1
ans = c["R"]*c["G"]*c["B"]
nans = 0
for i in range(n-2):
    j = 1
    while i+2*j<=n-1:
        
        if s[i] != s[i+j] and s[i] != s[i+j*2] and s[i+j] != s[i+j*2]:
            nans+=1
        j+=1
print(ans-nans)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
r=b=g=0
for s in S:
    if s=='R':
        r+=1
    elif s=='B':
        b+=1
    else:
        g+=1
out=0
for i in range(1,N):
    for j in range(min(i+1,N-i)):
        if S[i]!=S[i+j] and S[i]!=S[i-j] and S[i+j]!=S[i-j]:
            out+=1
print(r*b*g-out)

Submission status: Accepted

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

c = collections.Counter(S)
R = c['R']
G = c['G']
B = c['B']
ans = R*G*B
delans = 0
for i in range(1,N-1):
    for l in range(min(i,N-i-1)):
        if S[i]!=S[i-l-1] and S[i]!=S[i+l+1] and S[i-l-1]!=S[i+l+1]:
            delans += 1
ans -= delans
print(ans)

Submission status: Accepted

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

from collections import Counter
rgb = dict(Counter(s))
if len(rgb) < 3:
    print('0')
else:
    p = rgb['R'] * rgb['G'] * rgb['B']
    for i in range(1, (len(s)-1)//2 + 1):
        for j in range(len(s) - 2*i):
            if {s[j], s[j + i], s[j + 2*i]} == {'R', 'G', 'B'}:
                p -= 1
    print(p)

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(i+1,i+1+(n-i-1)//2):
    if s[i]==s[j]:
      continue
    elif s[i]!=s[2*j-i] and s[j]!=s[2*j-i]:
      ans-=1
      
print(str(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[j] and S[i]!=S[h] and S[j]!=S[h]:
      c+=1
ans=r*g*b-c
print(ans)

Submission status: Accepted

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

# RGBとなる組み合わせの数を求める
sum_collection = S.count('R') * S.count('G') * S.count('B')

# RGBとなる組み合わせの数から、 j - i == k - jとなる組の数を減らす
for i in range(N):
    for j in range(i, N):
        # j - i == k - j　となる組を見つける
        k = j + (j - i)
        if k >= N:
            break
        # すべて違ったら　(R G Bの組み合わせであったら)
        if S[i] != S[j] != S[k] != S[i]:
            sum_collection -= 1

print(sum_collection)

Submission status: Accepted

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

listr = []
listg = []
listb = []
listc = [0]*L
ans = 0

for i in range(L):
    if(S[i] == "R"):
        listr.append(i)
        listc[i] = 0
    elif(S[i] == "G"):
        listg.append(i)
        listc[i] = 1
    else:
        listb.append(i)
        listc[i] = 2
if(listr == [] or listg == [] or listb == []):
    print(0)
else:
    for i in listr:
        for j in listg:
            div = abs(j - i)
            ans += len(listb)
            mi = min(i,j) - div
            ma = max(i,j) + div
            if(div%2 == 0):
                k = min(i,j) + div//2
                if(listc[k] == 2):
                    ans -= 1
                if(mi >= 0):
                    if(listc[mi] == 2):
                        ans -= 1
                if(ma < L):
                    if(listc[ma] == 2):
                        ans -= 1
            else:
                if(mi >= 0):
                    if(listc[mi] == 2):
                        ans -= 1
                if(ma < L):
                    if(listc[ma] == 2):
                        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 - 2):
    for j in range(n):
        if i + j*2 < n:
            if s[i] != s[i + j]:
                if s[i] != s[i + j*2]:
                    if s[i + j] != s[i + j*2]:
                        ans -= 1
        else:
            break

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
s_=input()
s=Counter(s_)
rgb = s['R'] * s['G'] * s['B']
cnt=0
for i in range(len(s_)):
  for j in range(i+1,len(s_)):
    h=j-i+j
    if h<n:
      if s_[i]!=s_[j] and s_[h]!=s_[j] and s_[i]!=s_[h]:
        cnt+=1

print(rgb-cnt)

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
cnt=0
for i in range(len(s_)):
  for j in range(i+1,len(s_)):
    h=j-i+j
    if h<n:
      if s_[i]!=s_[j] and s_[h]!=s_[j] and s_[i]!=s_[h]:
        cnt+=1

print(rgb-cnt)

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
from collections import Counter

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
S = sr()
counter = Counter(S)
r = counter['R']; g = counter['G']; b = counter['B']
answer = r * g * b
for i in range(N):
    for j in range(i+1, N):
        if S[i] == S[j]:
            continue
        diff = j - i
        if j + diff >= N:
            break
        x = S[i]; y = S[j]
        z = S[j+diff]
        if x != z and y != z:
            answer -= 1

print(answer)

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)

cnt=len(R)*len(G)*len(B)
for i in R:
    for j in G:
        koho=set()
        if (j+i)%2==0:
            koho.add((j+i)//2)
        if 0<=2*i-j<N:
            koho.add(2*i-j)
        if 0<=2*j-i<N:
            koho.add(2*j-i)
        for k in koho:
            if k in B:
                cnt-=1
            
                
print(cnt)

Submission status: Accepted

Solution code:
import collections

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

CountRGB = collections.Counter(S)

ans = CountRGB["R"] * CountRGB["G"] * CountRGB["B"]

tmp = 0
for i in range(N-2):
    for j in range(i+1, N-1):

        if(2 * j - i > N - 1):
            break

        checker = []
        checker.append(S[i])
        checker.append(S[j])
        checker.append(S[2 * j - i])

        c = collections.Counter(checker)

        if(c["R"] == 1 and c["G"] == 1 and c["B"] == 1):
            tmp +=1

print(ans - tmp)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
r, g, b = s.count("R"), s.count("G"), s.count("B")
ans = r * g * b
for i in range(n):
    for j in range(i + 1, 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()
CC = Counter(S)
#print(CC)
if len(CC) < 3:
  print(0)
  exit(0)

ans = CC['R'] * CC['G'] * CC['B']
for i in range(N):
  for x in range(1,N):
    if i+2*x >= N:
      break
    if S[i] != S[i+x] and S[i+x] != S[i+2*x] and S[i+2*x] != S[i]:
      ans -= 1
      
print(ans)

Submission status: Accepted

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

cnt = [[0, 0, 0] for _ in range(n)]

for i in range(n):
    cnt[i][0 if s[i] == 'R' else 1 if s[i] == 'G' else 2] += 1
    if i:
        for j in range(3):
            cnt[i][j] += cnt[i - 1][j]

res = 0
for i in range(1, n):
    for j in range(i + 1, n):
        if s[i] == s[j]: continue
        if s[i] != 'R' and s[j] != 'R':
            res += cnt[i - 1][0]
            if i + i - j >= 0: res -= s[i + i - j] == 'R'
        if s[i] != 'G' and s[j] != 'G':
            res += cnt[i - 1][1]
            if i + i - j >= 0: res -= s[i + i - j] == 'G'
        if s[i] != 'B' and s[j] != 'B':
            res += cnt[i - 1][2]
            if i + i - j >= 0: res -= s[i + i - j] == 'B'

print(res)

Submission status: Accepted

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

    C={"R":0,"G":0,"B":0}

    for i in range(N):
        C[S[i]]+=1

    excepter=0

    for i in range(N-1):
        for j in range(i+1,N):
            if S[i]==S[j]:
                continue
            k=j+(j-i)
            if k>N-1:
                break
            if S[i]==S[k] or S[j]==S[k]:
                continue
            excepter+=1

    res=C["R"]*C["G"]*C["B"]-excepter
    print(res)
if __name__=="__main__":
    main()

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:
N = int(input())
S = input()

from collections import defaultdict
# d = defaultdict(list)
d = defaultdict(int)

for ix, c in enumerate(S):
    # d[c].append(ix+1)
    d[c] += 1

# ans = len(d["B"]) * len(d["G"]) * len(d["R"])
ans = d["R"] * d["G"] * d["B"]

for i in range(1, N+1):
    ci = S[i-1]
    for j in range(i+1, N+1):
        cj = S[j-1]

        if ci == cj:
            continue

        k = j + j - i
        if k <= N:
            ck = S[k-1]
            if ci != ck and cj != ck:
                ans -= 1
        else:
            break
print(ans)

Submission status: Accepted

Solution code:
import collections
n = int(input())
s = [''] + list(input())
cnt_1 = collections.Counter(s)
 
sub = 0
for j in range(1, (n+1)//2):
    for i in range(1, n-2*j+1):
        tmp = s[i] + s[i+j] + s[i+j*2]
        cnt_2 = collections.Counter(tmp)
        if max(cnt_2.values()) == 1:
            sub += 1
 
print((cnt_1['R']*cnt_1['G']*cnt_1['B'])-sub)

Submission status: Accepted

Solution code:
import collections
n = int(input())
s = [''] + list(input())
cnt_1 = collections.Counter(s)

sub = 0
for j in range(1, (n+1)//2):
    for i in range(1, n-2*j+1):
        tmp = s[i] + s[i+j] + s[i+j*2]
        cnt_2 = collections.Counter(tmp)
        if cnt_2['R'] == 1 and cnt_2['G'] == 1 and cnt_2['B'] == 1:
            sub += 1

print((cnt_1['R']*cnt_1['G']*cnt_1['B'])-sub)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
r=[0]*n
g=[0]*n
b=[0]*n
R=0; G=0; B=0
for i in range(n):
    if s[i]=="R":
        r[i]=1
        R+=1
    elif s[i]=="G":
        g[i]=1
        G+=1
    else:
        b[i]=1
        B+=1
s=0
for j in range(n):
    for k in range(n):
        if r[j]==1 and g[k]==1:
            if j!=k:
                j1,k1=(max(j,k),min(j,k))
                if (j1-k1)+j1<n:
                    if b[(j1-k1)+j1]==1:
                        s+=1
                if k1-(j1-k1)>=0:
                    if b[k1-(j1-k1)]==1:
                        s+=1
                if (j1-k1)>1 and (j1-k1)%2==0:
                    if b[k1+(j1-k1)//2]==1:
                        s+=1
print(R*G*B-s)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
from collections import deque

def solve(N: int, S: str):
    rp = 0
    gp = 0
    bp = 0
    rsum = [0]*(N+1)
    bsum = [0]*(N+1)
    gsum = [0]*(N+1)

    for i, x in enumerate(S[::-1]):
        if x ==  "R": rp = 1
        elif x =="G": gp = 1
        else: bp = 1
        rsum[N-1-i] = rsum[N-1-i+1] + rp
        gsum[N-1-i] = gsum[N-1-i+1] + gp
        bsum[N-1-i] = bsum[N-1-i+1] + bp
        rp = 0
        gp = 0
        bp = 0
    ans = 0
    for i in range(N):
        for j in range(i+1, N):
            if S[i] != S[j]:
                if j+j-i<N: k = S[j+j-i]
                else: k = ""
                if (S[i] == "R" and S[j] == "B") or (S[j] == "R" and S[i] == "B"):
                    ans += gsum[j] - (k=="G")*1
                elif (S[i] == "R" and S[j] == "G") or (S[j] == "R" and S[i] == "G"):
                    ans += bsum[j] - (k=="B")*1
                elif (S[i] == "G" and S[j] == "B") or (S[j] == "G" and S[i] == "B"):
                    ans += rsum[j] - (k=="R")*1

    print(ans)
    return
 
 
# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    S = next(tokens)  # type: str
    solve(N, S)
 
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
from collections import deque

def solve(N: int, S: str):
    r = []
    g = []
    b = []
    rp = 0
    gp = 0
    bp = 0

    rsum = [0]*(N+1)
    bsum = [0]*(N+1)
    gsum = [0]*(N+1)

    for i, x in enumerate(S[::-1]):
        if x ==  "R":
            r.append(i)
            rp = 1
        elif x =="G":
            g.append(i)
            gp = 1
        else:
            b.append(i)
            bp = 1
        rsum[N-1-i] = rsum[N-1-i+1] + rp
        gsum[N-1-i] = gsum[N-1-i+1] + gp
        bsum[N-1-i] = bsum[N-1-i+1] + bp
        rp = 0
        gp = 0
        bp = 0
    ans = 0
    for i in range(N):
        for j in range(i+1, N):
            if S[i] != S[j]:
                if j+j-i<N: k = S[j+j-i]
                else: k = ""
                if (S[i] == "R" and S[j] == "B") or (S[j] == "R" and S[i] == "B"):
                    ans += gsum[j] - (k=="G")*1
                elif (S[i] == "R" and S[j] == "G") or (S[j] == "R" and S[i] == "G"):
                    ans += bsum[j] - (k=="B")*1
                elif (S[i] == "G" and S[j] == "B") or (S[j] == "G" and S[i] == "B"):
                    ans += rsum[j] - (k=="R")*1
    

    print(ans)
  
    return
 
 
# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    S = next(tokens)  # type: str
    solve(N, S)
 
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest


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

    # 1文字ずつチェックするのでSET。
    s = list(input())

    # 全パターンの数(RGB1つずつ選択する場合)
    ans = s.count("R")*s.count("G")*s.count("B")

    # ここから条件(j-i≠k-j)を満たさないパターンを減算。
    s_len = len(s)
    minus = 0

    # i<=距離A=>j<=距離A=>k という状態で、距離Aが等しいパターンを列挙していく。
    for i in range(s_len-2):
        first = s[i]
        for j in range(1, 9999):

            # 文字数を超えるパターンは検証しない
            if i+j+j > s_len - 1:
                break

            # 1文字目,2文字目,3文字目が全部別の場合、除外するパターンである。(合計数から減算する)
            if first is not s[i+j] and first is not s[i+j+j] and s[i+j] is not s[i+j+j]:
                minus += 1

    print(ans - minus)




    # ans = 0
    # for i in range(len(s) - 2):
    #
    #     target = list("RGB".replace(s[i], ""))
    #     ans += s[i:len(s)].count(target[0]) * s[i:len(s)].count(target[1])
    #
    # for i in range(len(s) - 2)
    #
    #
    #
    #     for j in range(i+1, len(s) - 1):
    #         if s[i] is s[j]:
    #             continue
    #         target = "RGB".replace(s[i], "").replace(s[j], "")
    #
    #         ans += s[j:len(s)].count(target)
    #
    #         if j+(j-i) < len(s) :
    #             if s[j+(j-i)] is target:
    #                 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)

#     def test_入力例_3(self):
#         input = """4080
# RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBBBRBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBBBRBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBBBRBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBBBRBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBBBRBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBBBRBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBBBRBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBBBRBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBBBRBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBBBRBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBBBRBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBBBRBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBBBRBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBBBRBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBBBRBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBBBRBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBBBRBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBBBRBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBBBRBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBBBRBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBBBRBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBBBRBRBGRBGGBBRRGBBRRRBGGBRBGBRB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GBRBGBRBGBRBBBGBBBBRBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBBBRBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBBB"""
#         output = """1800"""
#         self.assertIO(input, output)

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

Submission status: Accepted

Solution code:
N=int(input())
S=input()
ans=0
RGB=[0,0,0]
for i in range(N):
  if S[i]=="R":
    RGB[0]+=1
  if S[i]=="G":
    RGB[1]+=1
  if S[i]=="B":
    RGB[2]+=1
ans=RGB[0]*RGB[1]*RGB[2]    
for i in range(N-2):
  s1=S[i]
  for k in range(i+1,N-1):
    s2=S[k]
    t=k+k-i
    if s1==s2:
      continue
    if t<=N-1:
      s3=S[t]
      if s3!=s1 and s3!=s2:
        ans-=1  
    else:
      break
print(ans)

Submission status: Accepted

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))

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

cnt = 0
cnt += S.count("R") * S.count("G") * S.count("B")

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

for k in range(N):
    for j in range(k):
        i = 2 * j - k
        if i < 0:
            continue
        if S[i] != S[j] and S[j] != S[k] and S[i] != S[k]:
            cnt -= 1 

print(cnt)

Submission status: Accepted

Solution code:
def main():
    rc = S.count("R")
    gc = S.count("G")
    bc = S.count("B")
    ns = 0
    for i in range(N-2):
        for k in range(i+2, N):
            if (i+k)%2 == 0 and S[i] != S[k]:
                j = (i+k)//2
                ns += 1 if S[j] != S[i] and S[j] != S[k] else 0
    print(rc*gc*bc - ns)


if __name__ == "__main__":
    N = int(input())
    S = input()
    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 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]:
            ans -= 1
#         print(i,j,k)

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=str(input())
t=len(s)
#setの方が参照速度が速い
r=set()
g=set()
b=set()
a=0
for i in range(n):
    if s[i]=='R':
        r.add(i)
    if s[i]=='G':
        g.add(i)
    if s[i]=='B':
        b.add(i)
d=len(r)*len(g)*len(b)
for ri in r:
    for gi in g:
        if 2*ri-gi in b:
            d-=1
        if 2*gi-ri in b:
            d-=1
        if (gi+ri)/2 in b:
            d-=1

print(d)

Submission status: Accepted

Solution code:
n=int(input())
s=str(input())
t=len(s)
#setの方が参照速度が速い
r=set()
g=set()
b=set()
a=0
for i in range(n):
    if s[i]=='R':
        r.add(i)
    if s[i]=='G':
        g.add(i)
    if s[i]=='B':
        b.add(i)
d=len(r)*len(g)*len(b)
for ri in r:
    for gi in g:
        rg=gi-ri
        if rg+gi in b:
            d-=1
        if  ri-rg in b:
            d-=1
        if (gi+ri)/2 in b:
            d-=1

print(d)

Submission status: Accepted

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

k += '.'*n

R = [0] * (n*3)
G = [0] * (n*3)
B = [0] * (n*3)

dic = { 'R' : 0, 'G' : 0, 'B' : 0 };
for i in range(n-1, -1, -1):
    dic[k[i]] += 1
    R[i] = dic['R']
    G[i] = dic['G']
    B[i] = dic['B']

ans = 0
for i in range(n):
    for j in range(i+1, n):
        if k[i] != k[j]:
            charset = ['R', 'G', 'B'];
            charset.remove(k[i]);
            charset.remove(k[j]);
            ##
            if charset[0] == 'R':
                ans += R[j + 1]
                ans -= (1 if k[j + (j - i)] == 'R' else 0)
            if charset[0] == 'G':
                ans += G[j + 1]
                ans -= (1 if k[j + (j - i)] == 'G' else 0)
            if charset[0] == 'B':
                ans += B[j + 1]
                ans -= (1 if k[j + (j - i)] == 'B' else 0)

print(ans)

Submission status: Accepted

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

r = set()
g = set()
b = set()
for i in range(n):
    if s[i]=="R":
        r.add(i)
    elif s[i]=="G":
        g.add(i)
    else:
        b.add(i)

score = len(r)*len(g)*len(b)
for i in r:
    for j in g:
        dist=abs(j-i)
        if (i>j):
            bi = i+dist
            if bi in b:
                score -=1
            bi = j-dist
            if bi in b:
                score -=1
        else:
            bi = j+dist
            if bi in b:
                score-=1
            bi = i-dist
            if bi in b:
                score-=1
        if((i+j)%2==0 and (i+j)/2 in b):
            score-=1
# print(count_)
print(score)

Submission status: Accepted

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

r=set()
g=set()
b=set()
for i in range(n):
    if s[i]=='R':
        r.add(i)
    if s[i]=='G':
        g.add(i)
    if s[i]=='B':
        b.add(i)
score = len(r)*len(g)*len(b)
for i in r:
    for j in g:
        dist=abs(j-i)
        if (i>j):
            bi = i+dist
            if bi in b:
                score -=1
            bi = j-dist
            if bi in b:
                score -=1
        else:
            bi = j+dist
            if bi in b:
                score-=1
            bi = i-dist
            if bi in b:
                score-=1
        if((i+j)%2==0 and (i+j)/2 in b):
            score-=1
# print(count_)
print(score)

Submission status: Accepted

Solution code:
from sys import stdin,stdout


def solve(S,N):
    color = [[] for _ in range(3)]

    for i in range(N):
        if S[i] == 'R': color[0].append(i)
        elif S[i] == 'G': color[1].append(i)
        else: color[2].append(i)
    
    
    #Exclusion 
    total = len(color[0])*len(color[1])*len(color[2])

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


    


if __name__ == "__main__": 
    #Input 
    N = int(stdin.readline().rstrip())
    S = stdin.readline().rstrip()
    stdout.write(str(solve(S,N)))

Submission status: Accepted

Solution code:
N=int(input())

S=input()

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

for i in range(len(S)):
    if S[i]=='R':
        R.append(i)
    elif S[i]=='G':
        G.append(i)
    else:
        B.append(i)

ex=0
for i in range(1,len(S)-1):
    for j in range(i+1,len(S)):
        if 2*j-i<=len(S):
            if S[i-1]!=S[j-1] and S[j-1]!=S[2*j-i-1] and S[i-1]!=S[2*j-i-1]:
                ex+=1

print((len(R)*len(G)*len(B))-ex)

Submission status: Accepted

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]


def main():
    N = int(input())
    S = list(input())
 
    c = [[] for _ in range(3)] #c = [[]] * 3 とすると全ての要素にappendされてしまうのでNG
    for i in range(len(S)):
        if S[i] == 'R':
            c[0].append(i)
        elif S[i] == 'G':
            c[1].append(i)
        else : # 'B'のとき
            c[2].append(i)
    total = len(c[0]) * len(c[1]) * len(c[2])

    reigai = 0 #先に条件2を満たすものを考えてから、条件1に当てはまるかを判定
    for i in range(N - 2):
        for j in range(i + 1, N - 1):
            k = j + (j - i)
            if k >= N:
                break
            if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                reigai += 1
    print(total - reigai)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]


def main():
    N = int(input())
    S = list(input())
 
    c = [[] for _ in range(3)] #c = [[]] * 3 とすると全ての要素にappendされてしまうのでNG
    for i in range(len(S)):
        if S[i] == 'R':
            c[0].append(i)
        elif S[i] == 'G':
            c[1].append(i)
        else : # 'B'のとき
            c[2].append(i)
    total = len(c[0]) * len(c[1]) * len(c[2])

    reigai = 0 #先に条件2を満たすものを考えてから、条件1に当てはまるかを判定
    for i in range(N - 2):
        for j in range(i + 1, N - 1):
            k = j + (j - i)
            if k >= N:
                break
            if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                reigai += 1
    print(total - reigai)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

cnt = 0

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

Submission status: Accepted

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

def main():

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

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

    ans = r*g*b

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

main()

Submission status: Accepted

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

r=set()
g=set()
b=set()
for i in range(n):
    if s[i]=='R':
        r.add(i)
    if s[i]=='G':
        g.add(i)
    if s[i]=='B':
        b.add(i)

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

for ri in r:
    for gi in g:
        dist = abs(ri-gi)
        if ri > gi:
            bi = dist + ri
            if bi in b:
                d -= 1
            bi = gi - dist
            if bi in b:
                d -= 1
        else:
            bi = dist + gi
            if bi in b:
                d -= 1
            bi = ri - dist
            if bi in b:
                d -= 1

        if (ri + gi) % 2 == 0:
            bi = (ri+gi)/2
            if bi in b:
                d -= 1
print(d)