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:
import sys
import math
import bisect
from collections import defaultdict,deque

# input = sys.stdin.readline
def inar():
    return [int(el) for el in input().split()]
# def find(a,b,c):
#     gc=math.gcd(a,b)
#     return math.gcd(gc,c)
def main():
    n=int(input())
    string=input()
    r=[]
    g=[]
    b=[]
    for i in range(n):
        if string[i]=="R":
            r.append(i)
        elif string[i]=="G":
            g.append(i)
        else:
            b.append(i)
    ans=0
    r.sort()
    g.sort()
    b.sort()
    # print(r)
    # print(g)
    # print(b)
    # print(len(b))
    # ans1=0
    # fir=[]
    # for i in range(len(r)):
    #     for j in range(len(g)):
    #         for k in range(len(b)):
    #             ls=[r[i],g[j],b[k]]
    #             ls.sort()
    #             if ls[1]-ls[0]!=ls[2]-ls[1]:
    #                 ans1+=1
    #         fir.append(ans1)
    # # print(ans1)

    # print("-------------------check---------------")
    # are=[]
    for i in range(len(r)):
        for j in range(len(g)):
            ans+=len(b)
            chota=min(g[j],r[i])
            bada=max(g[j],r[i])
            diff=bada-chota

            left=bisect.bisect_left(b,bada+diff)
            right=bisect.bisect_left(b,chota-diff)

            lol=(bada+chota)
            if lol%2==0:
                beech=lol//2
                ind=bisect.bisect_left(b,beech)
                if ind<len(b) and b[ind]==beech:
                    ans-=1
            if (left<len(b) and b[left]==bada+diff):
                ans-=1
            if (right<len(b) and b[right]==chota-diff):
                ans-=1

            # are.append(ans)
    print(ans)
    # for i in range(len(are)):
    #     print(are[i],fir[i])


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
R = []
G = []
B = [0 for _ in range(N)]
b_cnt = 0
for i in range(N):
  if S[i] == 'R':
    R.append(i)
  elif S[i] == 'G':
    G.append(i)
  else:
    B[i] = 1
    b_cnt += 1
answer = 0
for r in R:
  for g in G:
    answer += b_cnt
    if (g-r)%2 == 0 and B[(r+g)//2] == 1:
      answer -= 1
    if 0 <= 2*g-r < N and B[2*g-r] == 1:
      answer -= 1
    if 0 <= 2*r-g < N and B[2*r-g] == 1:
      answer -= 1
print(answer)

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

Submission status: Accepted

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

res=0
R=[]
G=[]
B=[]
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
    R.append(r)
    G.append(g)
    B.append(b)
    
for i in range(N):
    for j in range(N):
        if not i<j:
            continue
        k=S[2*j-i] if 2*j-i<N else 'A'
        if set([S[i],S[j]])==set(['R','G']):
            res+=B[-1]-B[j]-(k=='B')
        elif set([S[i],S[j]])==set(['B','G']):
            res+=R[-1]-R[j]-(k=='R')
        elif set([S[i],S[j]])==set(['R','B']):
            res+=G[-1]-G[j]-(k=='G')
print(res)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    s = input()
    r_set, g_set, b_set = set(), set(), set()

    for i, c in enumerate(s):
        if c == "R":
            r_set.add(i)
        elif c == "G":
            g_set.add(i)
        elif c == "B":
            b_set.add(i)

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

    print(len(r_set) * len(g_set) * len(b_set) - cnt)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    s = input()
    r_set, g_set, b_set = set(), set(), set()

    for i, c in enumerate(s):
        if c == "R":
            r_set.add(i)
        elif c == "G":
            g_set.add(i)
        elif c == "B":
            b_set.add(i)

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

    print(len(r_set) * len(g_set) * len(b_set) - cnt)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    s = input()
    r_set, g_set, b_set = set(), set(), set()

    for i, c in enumerate(s):
        if c == "R":
            r_set.add(i)
        elif c == "G":
            g_set.add(i)
        elif c == "B":
            b_set.add(i)

    cnt = 0
    for i in range(n):
        for j in range(i + 1, n):
            k = 2 * j - i

            if i in r_set and j in g_set and k in b_set:
                cnt += 1
            elif i in r_set and j in b_set and k in g_set:
                cnt += 1
            elif i in g_set and j in r_set and k in b_set:
                cnt += 1
            elif i in g_set and j in b_set and k in r_set:
                cnt += 1
            elif i in b_set and j in r_set and k in g_set:
                cnt += 1
            elif i in b_set and j in g_set and k in r_set:
                cnt += 1

    print(len(r_set) * len(g_set) * len(b_set) - cnt)


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')
cnt=0
for i in range(n-1):
    for j in range(1, min(i+1, n-i)):
            if (s[i] != s[i-j]) and (s[i] != s[i+j]) and (s[i-j] != s[i+j]):
                cnt += 1
print(r*g*b-cnt)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
total = 1
for c in 'RGB':
  total *= s.count(c)
for i in range(1, n - 1):
  for j in range(1, min(i + 1, n - i)):
    if s[i] != s[i - j] and s[i - j] != s[i + j] and s[i] != s[i + j]:
      total -= 1
print(total)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
c = [[0 for i in range(3)] for i in range(len(s))]
color = ['R','G','B']

for i in range(3):
	c[0][i] = (s[0]==color[i])

for i in range(1, len(s)):
	for j in range(3):
		if s[i]==color[j]:
			c[i][j] = c[i-1][j]+1
		else:
			c[i][j] = c[i-1][j]

res = 0
for i in range(len(s)-2):
	for j in range(i+1, len(s)-1):
		if s[i] != s[j]:
			for k in range(3):
				if s[i] != color[k] and s[j]!= color[k]:
					#print(i,j,k)
					res += c[n-1][k] - c[j][k]

					if 2*j-i<n:
						#possible principle 2 (j-1==k-j)
						res += -c[2*j-i][k] + c[2*j-i-1][k]

print(res)

Submission status: Accepted

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

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

rgb = r*g*b

not_j2_num = 0

for i in range(len(s)-2):
    for j in range(i+1,len(s)-1):
        
        k=j+j-i
        
        # print(i,j,k)
        
        if k<len(s) and len(set([s[i],s[j],s[k]]))==3:
            
            not_j2_num += 1
            
print(rgb - not_j2_num)

Submission status: Accepted

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Tue Sep  8 01:23:04 2020

@author: liang
"""

N = int(input())
S = input()
R_list = list()
G_list = list()
B_list = list()

for i in range(N):
    if S[i] == 'R':
        R_list.append(i)
    elif S[i] == 'G':
        G_list.append(i)
    else:
        B_list.append(i)
#print(R_list, G_list, B_list)
ans = len(R_list)*len(G_list)*len(B_list)
"""
set1 = set()
for r in R_list:
    for g in G_list:
        # r < g
        if r > g:
           # print(r,g)
            r, g = g, r ##ダメ
           
            【rは絶対書き換えたらダメ】
            gのfor文中 rの値は固定して考えているので、
            外側ループの変数を書き換えるような演算をしてはいけない
              => for文内で別の変数（コピー）を用意し、そのコピーについて演算を行うこと
           
            #print(r,g)
        if 2*g - r < N and S[2*g-r] == 'B':
            set1.add((r,g))
            ans -= 1
        if 2*r - g >= 0 and S[2*r-g] == 'B':
            set1.add((r,g))
            ans -= 1
        if (r+g)%2 == 0 and S[(r+g)//2] =='B':
            set1.add((r,g))
            ans -= 1   
#ans = len(R_list)*len(G_list)*len(B_list)         
"""
for r in R_list:
    for g in G_list:
        up = max(r,g)
        down = min(r,g)
        diff = up - down
        if up + diff < N and S[up+diff] == 'B':
            ans -= 1
        if down - diff >= 0 and S[down - diff] == 'B':
            ans -= 1
        if diff%2 == 0 and S[down + diff//2] =='B':
            ans -= 1

print(ans)

Submission status: Accepted

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

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

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

lenb = len(B)

cnt = 0
for r in R:
    for g in G:
        up = max(r, g)
        down = min(r, g)
        diff = up - down
        
        chk = 0
        if up + diff <= N:
            if S[up+diff-1] == 'B':
                chk += 1
        if down-diff >= 1:
            if S[down-diff-1] == 'B':
                chk += 1
        if diff%2 == 0:
            if S[int(up-diff/2-1)] == 'B':
                chk += 1
        cnt += lenb - chk

print(cnt)

Submission status: Accepted

Solution code:
'''
Created on 2020/09/07

@author: harurun
'''
def main():
  import sys
  pin=sys.stdin.readline
  pout=sys.stdout.write
  perr=sys.stderr.write

  N=int(pin())
  S=pin()[:-1]
  r=0
  g=0
  b=0
  cnt=0
  for i in range(N):
    if S[i]=="R":
      r+=1
    elif S[i]=="G":
      g+=1
    elif S[i]=="B":
      b+=1
    for j in range(i+1,N):
      k=2*j-i
      if k>=N:
        continue 
      if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
        cnt+=1
  print(r*g*b-cnt)
  return 
main()

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
import numpy as np
from numba import njit
def main():
    n = int(input())
    s = np.array(list(read().rstrip()))

    r = np.sum(s == 'R')
    g = np.sum(s == 'G')
    b = np.sum(s == 'B')
    @njit
    def f(s):
        ret = 0
        for i1 in range(n):
            for i2 in range(i1 + 1, n):
                i3 = i2 * 2 - i1
                if i3 <= n - 1:
                    if s[i1] != s[i2] and s[i1] != s[i3] and s[i2] != s[i3]:
                        ret += 1
        return ret

    res = r * g * b - f(s)
    print(res)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
    n = int(input())
    s = input()

    res = s.count('R') * s.count('G') * s.count('B')
    if res == 0:
        print(0)
        sys.exit()
    for i1 in range(n):
        for i2 in range(i1+1, n):
            i3 = i2 * 2 - i1
            if i3 <= n - 1:
                if s[i1] != s[i2] and s[i1] != s[i3] and s[i2] != s[i3]:
                    res -= 1
    print(res)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
    n = int(input())
    s = input()

    res = s.count('R') * s.count('G') * s.count('B')
    if res == 0:
        print(0)
        sys.exit()
    for i1 in range(n):
        for i2 in range(i1+1, n):
            i3 = i2 * 2 - i1
            if i3 <= n - 1:
                res -= s[i1] != s[i2] and s[i1] != s[i3] and s[i2] != s[i3]
    print(res)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
    n = int(input())
    s = input()

    res = s.count('R') * s.count('G') * s.count('B')
    if res == 0:
        print(0)
        sys.exit()
    for i1 in range(n):
        for i2 in range(i1+1, n):
            if i2 * 2 - i1 <= n - 1:
                if s[i1] != s[i2]:
                    res -= s[i1] != s[i2*2 - i1] and s[i2] != s[i2*2 - i1]
    print(res)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    n = I()
    s = S()
    rgb = ["R","G","B"]
    rgb_st = set(rgb)
    rgb_acc = [[0]*n for _ in range(3)]
    index = rgb.index(s[0])
    rgb_acc[index][0] += 1
    ans = 0

    for i in range(1,n):
        for j in range(3):
            rgb_acc[j][i] = rgb_acc[j][i-1]
        index = rgb.index(s[i])
        rgb_acc[index][i] += 1
    
    for i in range(n-2):
        for j in range(i+1,n-1):
            if s[i] != s[j]:
                ex = (rgb_st-{s[i],s[j]}).pop()
                index = rgb.index(ex)
                cnt = rgb_acc[index][-1] - rgb_acc[index][j]
                if j<2*j-i<n:
                    if s[2*j-i] == ex:
                        cnt -= 1
                ans += cnt
    print(ans)
main()

Submission status: Accepted

Solution code:
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    n = I()
    s = S()
    rgb = [None for _ in range(n)]
    rgb_acc = [None for _ in range(n)]
    rgb_st = {"R","G","B"}
    rgb_lst = ["R","G","B"]
    for i, char in enumerate(s):
        if char == "R":
            rgb[i] = [1,0,0]
        elif char == "G":
            rgb[i] = [0,1,0]
        else:
            rgb[i] = [0,0,1]
    
    rgb_acc[0] = rgb[0]
    for i in range(1, n):
        rgb_acc[i] = rgb_acc[i-1][:]
        index = 0 if s[i]=="R" else 1 if s[i]=="G" else 2
        rgb_acc[i][index] += 1
    
    ans = 0
    for i in range(n-2):
        for j in range(i+1,n-1):
           if s[i]!=s[j]:
                ex = rgb_st - {s[i], s[j]}
                ex = ex.pop()
                index = 0 if ex=="R" else 1 if ex=="G" else 2
                cnt = rgb_acc[n-1][index]-rgb_acc[j][index]
                if j+1 <= 2*j-i < n:
                    if s[2*j-i] == ex:
                        cnt -= 1
                ans += cnt

    print(ans)
main()

Submission status: Accepted

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

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

for i in range(n-1):
  for j in range(i+1,n):
    k = 2*j-i
    if k > n-1 or s[i] == s[j] or s[i] == s[k] or s[j] == s[k]: continue
    rgb -= 1
print(rgb)

Submission status: Accepted

Solution code:
import math
import sys
import os
from operator import mul
import bisect

sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(_S())
def LS(): return list(_S().split())
def LI(): return list(map(int,LS()))

if os.getenv("LOCAL"):
    inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
    sys.stdin = open(inputFile, "r")
INF = float("inf")

N = I()
S = list(_S())
ans = 0

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

for i,s in enumerate(S):
    if s == 'R':
        r.append(i)
    if s == 'G':
        g.append(i)
    if s == 'B':
        b.append(i)

# j = bisect.bisect_right(<list>,<value>)
# print(r)
# print(g)
# print(b)

# for i in r:
#     for j in g:
#         for k in b:
#             tmp = sorted([i,j,k])
#             if not tmp[1]-tmp[0]==tmp[2]-tmp[1]:
#                 ans += 1

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

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

print(ans)

# R G B
# R B G
# G B R
# G R B
# B G R
# B R G

# i   j   k
#  - != -

# RBRBGRBGGB
# 1122233333
# 0000111233
# 0112223334

# B*3
# G*4

# GB系: (2,4), (4,5)
# RB系: (1,3),

Submission status: Accepted

Solution code:
def main():
	N = int(input())
	S = list(input())
	R = []
	G = []
	B = []
	cnt = 0
	for i in range(len(S)):
		if S[i] == "R":
			R.append(i)
		elif S[i] == "G":
			G.append(i)
		elif S[i] == "B":
			B.append(i)
	l = len(B)
	for i in range(len(R)):
		r = R[i]
		for j in range(len(G)):
			g = G[j]
			cnt += l
			if g > r:
				M = g
				m = r
			elif r > g:
				M = r
				m = g
			d = M - m
			if (M + d) < N and S[M + d] == "B":
				cnt -= 1
			if (m - d) >= 0 and S[m - d] == "B":
				cnt -= 1
			if (M + m) % 2 == 0 and S[int((M + m) / 2)] == "B":
				cnt -= 1
	print(cnt)

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

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
S = input()
D = Counter(S)
ans = 1
for d in D.values():
    ans *= d

if len(D.values()) < 3:
    ans = 0

for i in range(N):
    for x in range(1, (N-i+1)//2):
        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,S=int(input()),input()
r,g,b=0,0,0
for i in list(S):
    if i=='R':r+=1
    if i=='G':g+=1
    if i=='B':b+=1
ans=r*g*b
for i in range(N-2):
    for j in range(i+1,N-1):
        if j-i>N-j-1:break
        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 = 0
g = 0
b = 0
for x in s:
  if x == 'R':
    r += 1
  elif x == 'G':
    g += 1
  else:
    b += 1
all = r*g*b
ex = 0
i = 0
for i in range(n-2):
  for j in range(i+1,(n+i-1)//2+1):
    if s[i] != s[j] and s[j] != s[2*j-i] and s[2*j-i] != s[i]:
      ex += 1
      
print(all-ex)

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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


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

for i in range(n) :
    a=s[i]
    for j in range(i+1,n) :
        b=s[j]
        if 2*j-i>n-1 :
            break
        c=s[2*j-i]
        if (a!=b) and (b!=c) and (c!=a) :
            ans-=1

print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    s = input()
    # 条件1:全パターンはr*g*b
    r = s.count('R')
    g = s.count('G')
    b = s.count('B')
    # i, j, kの差が一致するもので条件1を満たすものを後から引く 
    cnt = 0
    # len(s)
    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] and s[j] != s[k] and s[k] != s[i]:
                    cnt += 1
    ans = r*g*b - cnt
    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(1,N-1):
  for j in range(i+1,N):
    k = 2 * j - i
    if k > N:
      break
    if S[i-1] != S[j-1] and S[j-1] != S[k-1] and S[k-1] != S[i-1]:
      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-2):
  for j in range(i+1, n-1):
    if j - i > n - j - 1:
      break
    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:
from collections import Counter
def main():
    n=int(input())
    s=input()
    d=Counter(s)
    a=d["R"]*d["G"]*d["B"]
    for i in range(n):
        t=min(n-i-1, i)
        for j in range(t+1):
            if s[i-j] != s[i] and s[i-j] != s[i+j] and s[i] != s[i+j]:
                a-=1
    print(a)
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
s=str(input())
if n<=2:
    print(0)
    exit()
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):
        if 2*j-i>n-1:
            break
        if s[i]!=s[j] and s[j]!=s[2*j-i] and s[i]!=s[2*j-i]:
            ans-=1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
Rc = 0
Gc = 0
Bc = 0
for i in range(N):
    if S[i] == 'R':
         Rc +=1
    elif S[i] == 'G':
         Gc+=1
    elif S[i] == 'B':
         Bc+=1
c = Rc*Gc*Bc

for i in range(N-2):
     for j in range(i+1,N-1):
          if S[i] == S[j]:
               continue
          if j + j - i >= N:
               continue
          if S[i] == S[j+j-i]:
               continue
          if S[j] == S[j+j-i]:
               continue
          c -= 1
print(c)

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

ms = (N-3)//2
t = 0
for i in range(ms + 1):
    for j in range(N - (2*i + 3) + 1):
        if S[j] != S[j + i + 1] and S[j] != S[j + 2*i + 2] and S[j + i + 1] != S[j + 2*i + 2]:
            t += 1

print(r * g * b - t)

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

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
r = s.count('R')
g = s.count('G')
b = s.count('B')
ans = r * g * b
for i in range(n-2):
    for j in range(i+1,n-1):
        if (j - i) + j < n:
            k = (j - i) + j
            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()
r=s.count('R')
g=s.count('G')
b=s.count('B')
cnt=0
for i in range(1,n+1):
    for j in range(i,n+1):
        k=2*j-i
        if k>n:
            continue
        if s[i]!=s[j] and s[j]!=s[k] and s[k]!=s[i]:
            cnt+=1
print(r*g*b-cnt)

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc162/tasks/abc162_d

import sys
# sys.setrecursionlimit(100000)


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


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    n = input_int()
    S = 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)
    cnt = 0
    for i in range(n - 1):
        for j in range(i + 1, n):
            s1 = S[i]
            s2 = S[j]
            k = j + (j - i)
            if k > n - 1:
                break
            s3 = S[k]
            if s1 != s2 and s2 != s3 and s3 != s1:
                cnt += 1
    ans = (len(r) * len(g) * len(b)) - cnt
    print(ans)
    return


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def abc162d_rgb_triplets():
    n = int(input())
    s = input()
    r_cnt = 0
    g_cnt = 0
    b_cnt = 0
    for i in range(n - 1, -1, -1):
        if s[i] == 'R':
            r_cnt += 1
        elif s[i] == 'G':
            g_cnt += 1
        else:
            b_cnt += 1

    result = r_cnt * g_cnt * b_cnt
    for i in range(n - 2):
        for j in range(i + 1, n - 1):
            k = j + (j - i)
            if s[i] == s[j] or k >= n or s[i] == s[k] or s[j] == s[k]: continue
            result -= 1
    print(result)



abc162d_rgb_triplets()

Submission status: Accepted

Solution code:
n = int(input())
s = input()
r, g, b = [], [], []
cnt = 1
for i in s:
    if i == 'R':
        r.append(cnt)
    elif i == 'G':
        g.append(cnt)
    else:
        b.append(cnt)
    cnt += 1
rd = set(map(lambda x: x * 2, r)) #以下2つ目の条件を満たさないもの(j - i = k - j)の数え上げ
gd = set(map(lambda x: x * 2, g))
bd = set(map(lambda x: x * 2, b))
ddc = 0
for i in r:
    for j in g:
        if i + j in bd:
            ddc += 1
for j in g:
    for k in b:
        if j + k in rd:
            ddc += 1
for i in r:
    for k in b:
        if i + k in gd:
            ddc += 1
print(len(r) * len(g) * len(b) - ddc) #1つ目の条件を満たすモノから2つ目の条件を満たさないものを引く

Submission status: Accepted

Solution code:
n = int(input())
s = input()
r, g, b = [], [], []
cnt = 1
for i in s:
    if i == 'R':
        r.append(cnt)
    elif i == 'G':
        g.append(cnt)
    else:
        b.append(cnt)
    cnt += 1
rd = set(map(lambda x: x * 2, r))
gd = set(map(lambda x: x * 2, g))
bd = set(map(lambda x: x * 2, b))
ddc = 0
for i in r:
    for j in g:
        if i + j in bd:
            ddc += 1
for j in g:
    for k in b:
        if j + k in rd:
            ddc += 1
for i in r:
    for k in b:
        if i + k in gd:
            ddc += 1
print(len(r) * len(g) * len(b) - ddc)

Submission status: Accepted

Solution code:
# import itertools
# import math
# import sys
# sys.setrecursionlimit(500*500)
# import numpy as np

N = int(input())
S = input()
# n, *a = map(int, open(0))
# N, M = map(int, input().split())
# A = list(map(int, input().split()))
# B = list(map(int, input().split()))
# tree = [[] for _ in range(N + 1)]
# B_C = [list(map(int,input().split())) for _ in range(M)]
# S = input()

# B_C = sorted(B_C, reverse=True, key=lambda x:x[1])
# all_cases = list(itertools.permutations(P))
# a = list(itertools.combinations_with_replacement(range(1, M + 1), N))
# itertools.product((0,1), repeat=n)

# A = np.array(A)
# cum_A = np.cumsum(A)
# cum_A = np.insert(cum_A, 0, 0)

# def dfs(tree, s):
#     for l in tree[s]:
#         if depth[l[0]] == -1:
#             depth[l[0]] = depth[s] + l[1]
#             dfs(tree, l[0])
# dfs(tree, 1)

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

cnt = 0
for i in range(N):
    for j in range(i, N):
        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(all - cnt)

Submission status: Accepted

Solution code:

def solve(N, S):
    ans = 0
    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
    # print(R, G, B, R*G*B)
    ans = R * G * B

    dif = 1
    while dif <= N//2+1:
        idx = 0
        while idx + dif + dif < N:
            if S[idx] != S[idx + dif] and S[idx+dif] != S[idx+2*dif] and S[idx+2*dif] != S[idx]:
                ans -= 1
            idx += 1
        dif += 1
    print(ans)
if __name__ == '__main__':
    # N = 4000
    # S = 'RGBR' * 1000
    N = int(input())
    S = input()

    solve(N, S)

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
R = []
G = []
for i in range(N):
  if S[i] == "R":
    R.append(i)
  elif S[i] == "G":
    G.append(i)
r = len(R)
g = len(G)
ans = r * g * (N - r - g)
for i in R:
  for j in G:
    a = 2 * i - j
    b = 2 * j - i
    c = i + j
    if 0 <= a < N and S[a] == "B":
      ans -= 1
    if 0 <= b < N and S[b] == "B":
      ans -= 1
    if c % 2 == 0 and S[c//2] == "B":
      ans -= 1
print(ans)

Submission status: Accepted

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

uns = 0
for d in range(1, (N - 1) // 2 + 1):
    for i in range(N - 2 * d):
        s, t, u = S[i], S[i + d], S[i + 2 * d]
        if s != t and t != u and u != s:
            uns += 1

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

ans = r * g * b - uns
print(ans)

Submission status: Accepted

Solution code:
def binary_search(data, value):
    left = 0            # 探索する範囲の左端を設定
    right = len(data) - 1            # 探索する範囲の右端を設定
    while left <= right:
        mid = (left + right) // 2            # 探索する範囲の中央を計算
        if data[mid] == value:
            # 中央の値と一致した場合は位置を返す
            return mid
        elif data[mid] < value:
            # 中央の値より大きい場合は探索範囲の左を変える
            left = mid + 1
        else:
            # 中央の値より小さい場合は探索範囲の右を変える
            right = mid - 1
    return -1            # 見つからなかった場合

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

r = [i+1 for i, x in enumerate(s) if x == 'R']
g = [i+1 for i, x in enumerate(s) if x == 'G']
b = [i+1 for i, x in enumerate(s) if x == 'B']

c = 0
for i in r:
    for j in g:
        if binary_search(b, 2*i - j) != -1:
            c += 1
        if binary_search(b, 2*j - i) != -1:
            c += 1
        if binary_search(b, (i+j)/2) != -1:
            c += 1

ans = (len(r)*len(g)*len(b) - c)
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
S=str(input())
ans=S.count('R')*S.count('G')*S.count('B')

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