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
input = sys.stdin.readline
sys.setrecursionlimit(2147483647)
def main():
    N = int(input())
    S = input().rstrip("\r\n")
    R = []
    G = []
    B = []
    for i, s in enumerate(S):
        if s == "R":
            R.append(i)
        elif s == "G":
            G.append(i)
        else:
            B.append(i)
    ans = 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 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()

count = s.count('R')*s.count('B')*s.count('G')

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

print(count)

Submission status: Accepted

Solution code:
from itertools import product
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):
        k = 2*j-i
        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:
#author : dokueki

import sys
INT_MAX = sys.maxsize
INT_MIN = -(sys.maxsize)
sys.setrecursionlimit(10**7)
mod = 1000000007


def IOE():
    sys.stdin = open("input.txt", "r")
    sys.stdout = open("output.txt", "w")


def main():
    n = int(sys.stdin.readline())
    s = input()
    R, G, B = set(), set(), set()
    # tot = len(r) * len(g) * len(b)
    for i in range(n):
        if s[i] == "R":
            R.add(i + 1)
        if s[i] == "G":
            G.add(i + 1)
        if s[i] == "B":
            B.add(i + 1)
    tot = len(R)*len(G)*len(B)
    for r in R:
        for g in G:
            l = g - r
            if g + l in B:
                tot -= 1
            if r - l in B:
                tot -= 1
            if l % 2 == 0 and l // 2 + r in B:
                tot -= 1
    print(tot)

    # print(tot)


if __name__ == "__main__":
    try:
        IOE()
    except:
        pass
    main()

Submission status: Accepted

Solution code:
N=int(input())
S=input()
 
r = len(S.replace("G", "").replace("B", ""))
g = len(S.replace("R", "").replace("B", ""))
b = N - r - g
 
ans = r * g
ans = ans * b
 
for i in range( N ):
    for j in range(i, N):
        k = 2 * j - i
        if (i < j) & (j < k) & (k < N) :
            s = S[i] + S[j] + S[k]
            if ("R" in s) & ("G" in s) & ("B" in s):
                ans -= 1
 
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
r = len(S.replace("G", "").replace("B", ""))
g = len(S.replace("R", "").replace("B", ""))
b = N - r - g

ans = r * g
ans = ans * b

for i in range( N ):
    for j in range(i+1, int((N+i)/2 +1 )):
        k = 2 * j - i
        if k < N:
            s = S[i] + S[j] + S[k]
            if ("R" in s) & ("G" in s) & ("B" in s):
                ans -= 1

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
r = len(S.replace("G", "").replace("B", ""))
g = len(S.replace("R", "").replace("B", ""))
b = N - r - g

ans = r * g
ans = ans * b

for i in range( N ):
    for j in range(i, int((N+i)/2 + 1)):
        k = 2 * j - i
        if k < N:
            s = S[i] + S[j] + S[k]
            if ("R" in s) & ("G" in s) & ("B" in s):
                ans -= 1

print(ans)

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):
        k=2*j-i
        if k<N:
            if S[i]!=S[j] and S[i]!=S[k] and S[j]!=S[k]: cnt-=1
print(cnt)

Submission status: Accepted

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

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

ret = r*g*b

for i in range(1,int((n+1)/2)):
	for j in range(0,n-2*i):
		if(s[j] != s[j+i]) and (s[j+i] != s[j+2*i]) and (s[j+2*i] != s[j]):
			ret -= 1
#			print(i,j,j+i,j+2*i,s[j],s[j+i],s[j+2*i]," !!")
#		else:
#			print(i,j,j+i,j+2*i,s[j],s[j+i],s[j+2*i],)

print(ret)

Submission status: Accepted

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

R = 0
G = 0
B = 0

for i in range(N):
	if S[i] == 'R':
		R += 1
	elif S[i] == 'G':
		G += 1
	else:
		B += 1
ans = R*G*B
for i in range(N-2):
	for j in range(i+1, N-1):
		k = j+(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:
N = int(input())
S = input()

rcount = S.count("R")
gcount = S.count("G")
bcount = N-rcount-gcount



count = rcount * gcount * bcount
k = 0
for i in range(N-1):
    for j in range(i+1,N):
        k = 2*j-i
        if k < N :
            if 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 = input()

# 方針
# S_i!=S_j!=S_kである全ての組み合わせ(i,j,k)からj-i=k-jの組み合わせを引く

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

total_comb = r*g*b

count = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        # この時点でS_i=S_jであるのでスキップ
        if S[i] == S[j]:
            continue
        # kはiとjを定めれば一意に決まる
        k = 2 * j - i
        # kは単調増加なので、この時点でkがN-1より大きい場合、この(i,j)の組み合わせは計算しない
        if k > N - 1:
            break
        # 文字が同じであればスキップ
        if S[i] == S[k] or S[j] == S[k]:
            continue
        # ここでようやく、S_i != S_j != S_kかつj-i=k-jと判定できる
        count += 1

print(total_comb - count)

Submission status: Accepted

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

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

tmp=0

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

ans = ans - tmp
print(ans)

Submission status: Accepted

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

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

# R, G, Bの数をカウント
R_count = 0
G_count = 0
B_count = 0
for s in list(S):
    if s == 'R':
        R_count += 1
    elif s == 'G':
        G_count += 1
    elif s == 'B':
        B_count += 1

# R, G, Bの全組み合わせ数を求める
ans = R_count * G_count * B_count

# 3文字が等間隔に存在する組み合わせがあれば引いていく
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]:
            ans -= 1

print(ans)

Submission status: Accepted

Solution code:
s = int(input())
n = input()
ans = 0
r_set = set()
g_set = set()
b_set = set()

for _ in range(s):
    if n[_] == 'R':
        r_set.add(_)
    elif n[_] == 'G':
        g_set.add(_)
    else:
        b_set.add(_)

len_b = len(b_set)

for r in r_set:
    for g in g_set:
        diff = abs(r - g)
        minus = set()
        if r < g:
            if g + diff in b_set:
                minus.add(g+diff)
            if r - diff in b_set:
                minus.add(r-diff)
        else:
            if r + diff in b_set:
                minus.add(r+diff)
            if g - diff in b_set:
                minus.add(g-diff)
        if (r + g) / 2 in b_set:
            minus.add((r+g)/2)
        ans += len_b - len(minus)

print(ans)

Submission status: Accepted

Solution code:
def main():
    n=int(input())
    s=input()
    ans = 1

    r_cnt = s.count("R")
    g_cnt = s.count("G")

    ans = r_cnt*g_cnt*(n-r_cnt-g_cnt)
 
    for i in range(n):
        for j in range(i,n):
            s1 = s[i]
            s2 = s[j]
            if s1 != s2:
                t = (s1,s2)
                for s3 in "RGB":
                    if s3 not in t:
                        if 2*j-i < n and s[2*j-i] == s3:
                            ans -= 1
                        continue

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:

#S = "RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB"# * 1000
#N = len(S)

N=int(input())
S=input()
r = len(S.replace("G", "").replace("B", ""))
g = len(S.replace("R", "").replace("B", ""))
b = N - r - g
 
ans = r * g * b
 
is_break = False
for i in range(N):
    for j in range(i, int((N+i)/2 + 1)):
        k = 2 * j - i
        if k < N:
            s = S[i] + S[j] + S[k]
            if ("R" in s) & ("G" in s) & ("B" in s):
                    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)
    elif s[i]=="G":
        g.append(i)
    else:
        b.append(i)
b=set(b)
lb=len(b)
ans=0
for i in r:
    for j in g:
        x,y=min(i,j),max(i,j)
        ans_=lb
        if x-(y-x) in b:
            ans_-=1
        if y+(y-x) in b:
            ans_-=1
        if (x%2==y%2) and (y-x)//2+x in b:
            ans_-=1
        ans+=ans_
print(ans)

Submission status: Accepted

Solution code:
import bisect,sys
import math
from collections import deque

def sRaw():
    return input().rstrip("\r")


def iRaw():
    return int(input())


def ssRaw():
    return input().split()


def isRaw():
    return list(map(int, ssRaw()))

INF = 1 << 29

DIV = (10**9) + 7

def mod_inv_prime(a, mod=DIV):
    return pow(a, mod-2, mod)


def mod_inv(a, b):
    r = a
    w = b
    u = 1
    v = 0
    while w != 0:
        t = r//w
        r -= t*w
        r, w = w, r
        u -= t*v
        u, v = v, u
    return (u % b+b) % b


def CONV_TBL(max, mod=DIV):
    fac, finv, inv = [0]*max, [0]*max, [0]*max
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    inv[1] = 1
    for i in range(2, max):
        fac[i] = fac[i-1]*i % mod
        inv[i] = mod - inv[mod % i] * (mod//i) % mod
        finv[i] = finv[i-1]*inv[i] % mod

    class CONV:
        def __init__(self):
            self.fac =fac
            self.finv = finv
            pass

        def ncr(self, n, k):
            if(n < k):
                return 0
            if(n < 0 or k < 0):
                return 0
            return fac[n]*(finv[k]*finv[n-k] % DIV) % DIV
        
    return CONV()



sys.setrecursionlimit(200005)


def main():
    N = iRaw()
    S = sRaw()
    nRGB = [sum([1 for c in S if c == C]) for C in "RGB"]
    zen = nRGB[0]*nRGB[1]*nRGB[2]

    ans = 0
    ho = 0
    for a in range(1,N):
        c2 = S[a]
        for r in range(1,a+1):
            if a+r >= N:
                continue
            c1 = S[a-r]
            c3 = S[a+r]
            if c1!=c2 and c2!=c3 and c1!=c3:
                ho+=1             
    return zen-ho

if __name__ == "__main__":
    print(main())

Submission status: Accepted

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

# N = 39
# S = "RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB"

def calculate(n, s):
    arr = list(s)

    rNum = arr.count("R")
    gNum = arr.count("G")
    bNum = arr.count("B")

    sum = 0

    for step in range(1,math.ceil(n / 2) + 1):
        for i in range(n - 2*step):
            s = "".join([arr[i],arr[i+step],arr[i+step*2]])
            if s == "RGB" or s == "RBG" or s == "BGR" or s == "BRG" or s == "GBR" or s == "GRB":
                sum = sum + 1


    print(rNum*gNum*bNum - sum)



calculate(N, S)

Submission status: Accepted

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

r_num, g_num, b_num = 0, 0, 0
for i in s :
    if i == "R":
        r_num+=1
    elif i== "G":
        g_num +=1
    else:
        b_num+=1

ans = r_num * g_num *b_num

for i in range(len(s)):
    for j in range(i,len(s)):
        k = 2*j -i 
        if k <= j :
            continue
        else:
            if len(s) > k and k > 0  and s[i] != s[j] and s[j] != s[k] and s[i] != s[k]:
                ans  -=1

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

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

sum = r*g*b

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

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]:
            continue
        k = j - i + j
        if k > n-1:
            break
        if s[i] == s[k] or s[j] == s[k]:
                continue
        cnt -= 1
print(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 = j*2-i
        if k<n:
            if s[i]!=s[j]!=s[k]!=s[i]:
                ans -=1
print(ans)

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]:
            continue
        k = j - i + j
        if k > n-1:
            continue
        if s[i] == s[k] or s[j] == s[k]:
                continue
        cnt -= 1
print(cnt)

Submission status: Accepted

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

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

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

ans = 0

for j in range(1,n-1):
    if s[j]=='R':
        ans += G[j]*(B[n]-B[j+1])
        ans += B[j]*(G[n]-G[j+1])
        for i in range(j):
            if j+i+1<n:
                if s[j-i-1]=='B' and s[j+i+1]=='G':
                    ans-=1
                elif s[j-i-1]=='G' and s[j+i+1]=='B':
                    ans-=1
            else:
                break
    elif s[j]=='G':
        ans += R[j]*(B[n]-B[j+1])
        ans += B[j]*(R[n]-R[j+1])
        for i in range(j):
            if j+i+1<n:
                if s[j-i-1]=='R' and s[j+i+1]=='B':
                    ans-=1
                elif s[j-i-1]=='B' and s[j+i+1]=='R':
                    ans-=1
            else:
                break
    elif s[j]=='B':
        ans += G[j]*(R[n]-R[j+1])
        ans += R[j]*(G[n]-G[j+1])
        for i in range(j):
            if j+i+1<n:
                if s[j-i-1]=='R' and s[j+i+1]=='G':
                    ans-=1
                elif s[j-i-1]=='G' and s[j+i+1]=='R':
                    ans-=1
            else:
                break
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):
  a = S[i]
  for j in range(i+2,N,2):
    b = S[j]
    k = (i+j)//2
    if sorted([a,S[k],b]) == ['B','G','R']:
      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):
        k = 2*j-i
        if k < n:
            if s[k] != s[i] and s[k] != s[j] and s[i] != s[j]:
                    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):
    for j in range(1,n//2+1):
        if i+2*j>=n:
            break
        
        if s[i]!=s[i+j] and s[i+j]!=s[i+2*j] and s[i]!=s[i+2*j]:
            ans -= 1
print(ans)

Submission status: Accepted

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

r = 0
g = 0
b = 0


for ss in s:
    if ss == "R":
        r += 1
    elif ss == "G":
        g += 1
    elif ss == "B":
        b += 1
ng = 0
i = 0
while i < n:
    j = 1
    while i+2*j < n:
        if s[i] != s[i+j] and s[i+j] != s[i+2*j] and s[i] != s[i+2*j]:
            ng += 1
        j += 1
    i += 1
print(r*g*b-ng)

Submission status: Accepted

Solution code:

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

r = 0
g = 0
b = 0
for c in S:
    if c =="R":
        r +=1    
    if c =="G":
        g +=1
    if c =="B":
        b +=1
_avoid = 0
for i in range(N):
    for j in range(i,N):
        k = 2*j -i
        if k >= N:
            continue
        if k > j and not S[i] == S[j] and not S[i] == S[k] and not S[j] == S[k]:
            _avoid +=1
print(r*g*b-_avoid)

Submission status: Accepted

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

L = [0,0,0]
for i in range(N):
    if S[i] == "R":
        L[0] +=1
    elif S[i] == "G":
        L[1] +=1
    else:
        L[2] +=1

Ans = L[0]*L[1]*L[2]

for i in range(N):
  s = S[i]
  n = min(N, (N+i)//2 + 1)
  for j in range(i+1,n):
    t = S[j]
    if s != t:
      k = 2*j-i
      if k <= N-1:
        u = S[k]
        if t != u and u != s:
          Ans -=1
print(Ans)

Submission status: Accepted

Solution code:
#!/usr/bin/python3


def remain(a, b):
    if a == 'R':
        if b == 'G':
            return 'B'
        else:
            return 'G'
    elif a == 'G':
        if b == 'R':
            return 'B'
        else:
            return 'R'
    else:
        # a == 'B'
        if b == 'R':
            return 'G'
        else:
            return 'R'
    

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

cs = {}

cs['R'] = [ 0 for i in range(n) ]
cs['G'] = [ 0 for i in range(n) ]
cs['B'] = [ 0 for i in range(n) ]

for i in range(-1, - n - 1, -1):
    if s[i] == 'R':
        cs['R'][i] = cs['R'][i + 1] + 1
        cs['G'][i] = cs['G'][i + 1]            
        cs['B'][i] = cs['B'][i + 1]
    elif s[i] == 'G':
        cs['R'][i] = cs['R'][i + 1]
        cs['G'][i] = cs['G'][i + 1] + 1
        cs['B'][i] = cs['B'][i + 1]            
    elif s[i] == 'B':
        cs['R'][i] = cs['R'][i + 1]
        cs['G'][i] = cs['G'][i + 1]            
        cs['B'][i] = cs['B'][i + 1] + 1

            
c = 0
for i in range(n):
    for j in range(i + 1, n):
        if s[i] != s[j]:
            r = remain(s[i], s[j])
            c += cs[r][j]
            if j + j - i < n and s[j + j - i] == r:
                c -= 1
            
print(c)

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
  elif i == 'B':
    b += 1

res = r * g * b

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

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
S = input()
ans = 0

for j in range(1, N-1):
    cl, cr = Counter(S[:j]), Counter(S[j+1:])
    
    for i in set(cl.keys())-set(S[j]):
        ans += cl[i]*(N-1-j-cr[S[j]]-cr[i])
            
    for i in range(1, min(j, N-1-j)+1):
        if S[j] != S[j-i] and S[j] != S[j+i] and S[j-i]!=S[j+i]:
            ans -= 1
    
print(ans)

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

print(res)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
num_r = 0
num_g = 0
num_b = 0

for i in range(N):
  if S[i] == "R":
    num_r += 1
  if S[i] == "G":
    num_g += 1
  if S[i] == "B":
    num_b += 1

rgb = num_r*num_g*num_b
x = 0

for i in range(N):
  for m in range(N):
    j = i + m
    k = j + m
    if k > N - 1:
      break
    elif S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
      x += 1
rgb -= x
print(rgb)

Submission status: Accepted

Solution code:
import collections
#定数
n = int(input())
S = input()

#ここの計算量は？
#R,G,Bそれぞれの出現回数
C = collections.Counter(S)
r = C['R']
g = C['G']
b = C['B']
#条件1
ans = r * g * b

#最大O(n^2)
for j in range(1, n-1):
    sj = S[j] 
    if j > n-1-j:
        a = n-1-j
    else:
        a = j
    for d in range(1, a+1):
        si = S[j-d]
        sk = S[j+d]
        if si != sj and si != sk and sj != sk:
            #条件2を満たさないが条件1を満たすもの
            ans -= 1
        else:
            pass
print(ans)

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
def main():
    n = int(input())
    s = input()
    r = s.count('R')
    g = s.count('G')
    b = n - r - g
    ans = r * g * b
    for i in range(n):
        for d in range(n):
            j = i + d
            k = j + d
            if k >= n:
                break
            if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
                ans -= 1
    print(ans)    

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import collections

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

count = collections.Counter(S)

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

for distance in range(1, (N - 1) // 2 + 1):
    for i in range(N):
        if i + 2 * distance >= N:
            break
        
        if len({S[i], S[i + distance], S[i + 2 * distance]}) == 3:
             ans -= 1
    

print(ans)

Submission status: Accepted

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

r=0
g=0
b=0

for i in range(N):
  if S[i]=="R":
    r+=1
  elif S[i]=="G":
    g+=1
  else:
    b+=1
    
n=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:
      break
    if S[i]==S[k] or S[j]==S[k]:
      continue
    n -= 1
      
print(n)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
r_list = []
g_list = []
b_set = set()
for i in range(n):
    if s[i] == "R":
        r_list.append(i)
    if s[i] == "G":
        g_list.append(i)
    if s[i] == "B":
        b_set.add(i)
def tousable(p, q):
    res = []
    if p % 2 == q % 2:
        res.append((p+q)//2)
    res.append(2*p-q)
    res.append(2*q-p)
    return res
b_length = len(b_set)
ans = 0
for i in r_list:
    for j in g_list:
        minus = 0
        for k in tousable(i, j):
            if k in b_set:
                minus += 1
        ans += b_length - minus
print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
from itertools import product  


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

R_i = [i for i, s in enumerate(S) if s == "R"]  
G_i = [i for i, s in enumerate(S) if s == "G"]  
B_i = {i for i, s in enumerate(S) if s == "B"}  

R = len(R_i)  
G = len(G_i)  
B = len(B_i)  

xs = map(sorted, product(R_i, G_i))  

s = 0  
for x in xs:  
    k = 2 * x[1] - x[0]  # 2j-i  
    if k in B_i:  
        s += 1  
    i = 2 * x[0] - x[1]  # 2j-k  
    if i in B_i:  
        s += 1  
    j = (x[0] + x[1]) / 2  # (i+k)/2  
    if j in B_i:  
        s += 1  

print(R * G * B - s)

Submission status: Accepted

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

#output
from collections import Counter
answer = 0
if N > 3:
    for i in range(1, N-1):
        if S[i] == "R":
            a = Counter(S[:i])["G"]
            b = Counter(S[i+1:])["B"]
            c = Counter(S[:i])["B"]
            d = Counter(S[i+1:])["G"]
            answer += a*b + c*d
            for j in range(1, min(i+1, N-i)):
                if S[i-j] != "R" and S[i+j] != "R" and S[i-j] != S[i+j]:
                    answer -= 1
        elif S[i] == "B":
            a = Counter(S[:i])["R"]
            b = Counter(S[i+1:])["G"]
            c = Counter(S[:i])["G"]
            d = Counter(S[i+1:])["R"]
            answer += a*b + c*d
            for j in range(1, min(i+1, N-i)):
                if S[i-j] != "B" and S[i+j] != "B" and S[i-j] != S[i+j]:
                    answer -= 1
        elif S[i] == "G":
            a = Counter(S[:i])["R"]
            b = Counter(S[i+1:])["B"]
            c = Counter(S[:i])["B"]
            d = Counter(S[i+1:])["R"]
            answer += a*b + c*d
            for j in range(1, min(i+1, N-i)):
                if S[i-j] != "G" and S[i+j] != "G" and S[i-j] != S[i+j]:
                    answer -= 1

print(answer)

Submission status: Accepted

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

Rs = 0
Gs = 0
Bs = 0
for il in S:
    if il == 'R':
        Rs += 1
    if il == 'G':
        Gs += 1
    if il == 'B':
        Bs += 1
num = 0
for i in range(N):
    for step in range(1, N):
        j = i + step
        k = i + 2*step
        if k > N - 1:
            break
        if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
            num += 1
print(Rs*Gs*Bs - num)

Submission status: Accepted

Solution code:
n=int(input())
s=list(input())
ans=s.count('R')*s.count('G')*s.count('B')
for i in range(n):
    if s[i]=='R':
        s[i]=1
    elif s[i]=='G':
        s[i]=2
    elif s[i]=='B':
        s[i]=4
for i in range(n):
    for j in range(i+1,n):
      k=2*j-i
      if k>=n:
        continue
      if s[i]+s[j]+s[k]==7:
          ans-=1
print(ans)

Submission status: Accepted

Solution code:
def main():
    N=int(input())
    S=input()
    k1=S.count("R")
    k2=S.count("B")
    ans=k1*k2*(len(S)-k1-k2)
    for i in range(N-2):
        for j in range(i+1,N-1):
            if j+(j-i)<N and S[i]!=S[j]:
                if S[j+(j-i)]!=S[i] and S[j+(j-i)]!=S[j]:
                    ans-=1
    print(ans)
main()

Submission status: Accepted

Solution code:
from sys import stdin
N = list(map(int, (stdin.readline().strip().split())))[0]
S= list(stdin.readline().strip())
ans = S.count("R")*S.count("G")*S.count("B")
for i in range(N):
    for j in range(i+1,N):
        k=2*j-i
        if k>=N:break
        if S[i]!=S[j] and S[j]!=S[k] and S[i] !=S[k]:
                ans-=1
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin

n=int(input())
s=input()
r=0
g=0
b=0
for i in range(n):
    if s[i]=='R':
        r+=1
    if s[i]=='G':
        g+=1
    if s[i]=='B':
        b+=1
ans=r*g*b
for i in range(n):
    for j in range(n):
        now1=i+j
        now2=i+j+j
        if now2>=n:
            continue
        if(s[i]!=s[now1] and s[now1]!=s[now2] and s[now2]!=s[i]):
            ans-=1
print(str(ans)+"\n")

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 = j-i+j
        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 defaultdict, deque
import sys
import heapq
import bisect
import math
import itertools
import string
import queue
import copy
import time
# import numpy as np
from fractions import gcd

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


def inp(): return int(sys.stdin.readline())


def inp_list(): return list(map(int, sys.stdin.readline().split()))


def lcm(x, y): return (x * y) // gcd(x, y)


N = inp()
S = input()

"""
    Conditions:
        1. S[i] \ne S[j] and S[j] \ne S[k] and S[k] \ne S[i]
        2. j - i \ne k - j
"""

r = S.count('R')
g = S.count('G')
b = S.count('B')
# 条件1を満たす組は rgb 個。それから「条件1を満たしてかつ条件 2 を満たさない」組を引く。条件を満たさないもののほうが多い。


cnt = 0
for i in range(N):
    for j in range(i + 1, N):
        k = j + (j - i) # => k - j == j - i
        if k > N - 1: break
        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:
n = int(input())
s = input()
ans = s.count("R")*s.count("G")*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[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=0

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

Submission status: Accepted

Solution code:
from itertools import product
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)
    elif s[i] == 'B':
        b.append(i)

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

print(len(r) * len(g) * len(b) - ng_cnt)

Submission status: Accepted

Solution code:
from collections import Counter

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

RGB = Counter(S)

su = RGB['R']*RGB['G']*RGB['B']

for i in range(N-2):
    for k in range(i+2,N,2):
        j = (i + k)//2
        a = [S[i],S[j],S[k]]
        if 'R' not in a:
            continue
        if 'G' not in a:
            continue
        if 'B' not in a:
            continue
        su -= 1

print(su)

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

Submission status: Accepted

Solution code:
n = int(input())
s = str(input())
r, g, b = map(s.count, "RGB")
ans = r*g*b
for i in range(n):
    for j in range(i+1, n):
        k = 2*j - i
        if k<n and len(set([s[i], s[j], s[k]])) == 3:
            ans -= 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
X = input()
ans = 0
r=0
g=0
b=0
G = []
R = []
B = []
for i in range(0,N):
    if(X[i] == 'G'):
        G.append(i)
        g+=1
for k in range(0,N):
    if(X[k] == 'R'):
        R.append(k)
        r+=1
for j in range(0,N):
    if(X[j] == 'B'):
        B.append(j)
        b+=1
ans = r*b*g
for i in range(0,N):
    for j in range(i+1,N):
        if(2*j-i < N) and X[i] != X[j]:
            if(X[2*j-i] != X[i]) and (X[2*j-i] != X[j]):
                ans -= 1
print(ans)