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

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

ans=r_cnt*g_cnt*b_cnt

for i in range(N):
  for d in range(N):
    j=i+d
    k=j+d
    if k>=N:break
#等号がないと範囲外参照になる、0-indexだからk=S[N]は無い      
    if S[i]!=S[j] and S[i]!=S[k] and S[k]!=S[j]:
      ans-=1
      
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
s = list(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
sub = 0
for i in range(N-1):
    for j in range(i+1,N):
        if s[i]==s[j]:
            continue
        k = 2*j - i
        if k >= N:
            continue
        else:
            if (s[k]!=s[i] and s[k]!=s[j]):
                sub += 1
print(all-sub)

Submission status: Accepted

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

cnt = {'R' : 0, 'G' : 0, 'B' : 0}
for Si in S:
    cnt[Si] += 1

ans = 1    
for key, val in cnt.items():
    ans *= val
# print("#", ans)
    
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)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
a = S.count("R") * S.count("G") * S.count("B")
c = 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] and S[i] != S[k] and S[j] != S[k]:
            c += 1
print(a - c)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
r=g=b=0
for ss in s:
  if ss=="R":r+=1
  elif ss=="G":g+=1
  elif ss=="B":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 not(s[i]==s[j] or s[j]==s[k] or s[i]==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(i+1, (N+i-1)//2+1):
        if set([S[i], S[j], S[2*j-i]]) == {"R", "G", "B"}:
            ans -= 1

print(ans)

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
rgb = r*g*b
minus = 0
for i in range(n):
    dis = 1
    while i+dis*2 < n:
        if s[i] != s[i+dis] and s[i] != s[i+dis*2] and s[i+dis] != s[i+dis*2]:
            minus += 1
        dis += 1
print(rgb-minus)

Submission status: Accepted

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

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

ans = r * g * b

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

Submission status: Accepted

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

if n <= 2:
    print(0)

C = [[0]*(n+1) for i in range(3)]
for i in range(n):
    for j in range(3):
        C[j][i+1] = C[j][i]
    if s[i] == 'R':
        C[0][i+1] += 1
    elif s[i] == 'G':
        C[1][i+1] += 1
    else:
        C[2][i+1] += 1
#print(C)

D = [[0]*(n+1) for i in range(3)]
s.reverse()
for i in range(n):
    for j in range(3):
        D[j][i+1] = D[j][i]
    if s[i] == 'R':
        D[0][i+1] += 1
    elif s[i] == 'G':
        D[1][i+1] += 1
    else:
        D[2][i+1] += 1
#print(D)
D = [list(reversed(d)) for d in D]
#print(D)

s.reverse()
ans = 0
for i in range(1, n-1):
    #print(ans)
    if s[i] == 'R':
        ans += C[1][i]*D[2][i+1] + C[2][i]*D[1][i+1]
    elif s[i] == 'G':
        ans += C[0][i]*D[2][i+1] + C[2][i]*D[0][i+1]
    else:
        ans += C[0][i]*D[1][i+1] + C[1][i]*D[0][i+1]
    for j in range(1, i+1):
        if i+j >= n:
            continue
        if s[i-j] != s[i+j] and s[i-j] != s[i] and s[i+j] != s[i]:
            ans -= 1
print(ans)
#print(C[0][1], D[2][3])

Submission status: Accepted

Solution code:
N = int(input())
S = input()
R = S.count("R")
G = S.count("G")
B = S.count("B")

def f(S):
    N = len(S)
    ret = 0
    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[i] != S[k] and S[j] != S[k]:
                ret += 1
    return ret

ans = R * G * B - f(S)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
R = S.count('R')
G = S.count('G')
B = N-R-G
ans = R*G*B
for d in range(1,N//2+1):
    for i in range(N):
        if i + d*2 >= N: break
        a,b,c = S[i],S[i+d],S[i+d+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 = 0
for s in S:
    if s == 'R':
        r += 1
    if s == 'G':
        g += 1
    if s == 'B':
        b += 1

ans = r * g * b
for i in range(N - 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]:
            ans -= 1
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
S = input()
l = Counter(S)
diff = 0
for i in range(N-2):
  for j in range(1,(N-i-1)//2 + 1):
    # print(S[i:i+3*j:j])
    if set(S[i:i+3*j:j]) == {'R','G','B'}:
      diff += 1
print(l['R'] * l['G'] * l['B'] - diff)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
from collections import Counter
C=Counter(S)
ans=C['R']*C['G']*C['B']
for i in range(N):
    for j in range(i+1,N):
        if j+(j-i) >= N: break
        if S[i] != S[j] and S[j] != S[j+(j-i)] and S[i] != S[j+(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")
c = 0
for i in range(n):
    j = 1
    while i+j+j+1 <= n:
        if  len(set([s[i],s[i+j],s[i+j+j]]))==3:
            c += 1
        j += 1
print(r*g*b - c)

Submission status: Accepted

Solution code:
from collections import Counter
def solve():
    n = int(input())
    s = input()
    c = Counter(s)
    ans = c['R']*c['G']*c['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__':
    solve()

Submission status: Accepted

Solution code:
#!usr/bin/env python3
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.buffer.readline().split()]
def I(): return int(sys.stdin.buffer.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)
mod = 1000000007

def solve():
    def f(g,b):
        left = bisect.bisect_left(g,j)
        right = bisect.bisect_left(b,j)
        res = left*(len(b)-right)
        for i in range(left):
            k = bisect.bisect_left(b,j2-g[i])
            if k < len(b) and b[k]+g[i] == j2:
                res -= 1
        return res
    n = I()
    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)
    ans = 0
    for j in range(1,n-1):
        sj = s[j]
        j2 = 2*j
        if sj == "R":
            ans += f(g,b)+f(b,g)
        elif sj == "G":
            ans += f(r,b)+f(b,r)
        else:
            ans += f(r,g)+f(g,r)
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
n=int(input())
s=input()
r=g=b=0
for i in s:
  if i=="R":r+=1
  elif 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 not(s[i]==s[j] or s[j]==s[k] or s[i]==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")
cnt = 0
for i in range(n):
    i_ = s[i]
    for j in range(i+1, n):
        if s[j] == i_: continue
        k = 2 * j - i
        j_ = s[j]
        if k >= n or s[k] == i_ or s[k] == j_: continue
        cnt += 1
print(ans - cnt)

Submission status: Accepted

Solution code:
n = int(input())
s = []
r, g, b = [0] * 3
for c in input():
    if c == 'R':
        s.append(0)
        r += 1
    elif c == 'G':
        s.append(1)
        g += 1
    else:
        s.append(2)
        b += 1

c = r * g * 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[j] != s[k] and s[k] != s[i]:
            c -= 1

print(c)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

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

num_list = []
r, g, b = 0, 0, 0
for i in range(n-1, -1, -1):
    if s[i] == "R":
        r += 1
    elif s[i] == "G":
        g += 1
    elif s[i] == "B":
        b += 1
    num_list.append([r, g, b])

num_list.reverse()

ans = 0
for i in range(n-2):
    if s[i] == "R":
        ans += num_list[i+1][1] * num_list[i+1][2]  # G , B
        for j in range(0, (n-i-1)//2):
            a, b, c = i, j+i+1, j+j+i+2
            # print(a, b, c)
            if {s[b], s[c]} == {"G", "B"}:
                ans -= 1
    if s[i] == "G":
        ans += num_list[i+1][0] * num_list[i+1][2]  # R, B
        for j in range(0, (n-i-1)//2):
            a, b, c = i, j+i+1, j+j+i+2
            # print(a, b, c)
            if {s[b], s[c]} == {"R", "B"}:
                ans -= 1
    if s[i] == "B":
        ans += num_list[i+1][0] * num_list[i+1][1]  # R, G
        for j in range(0, (n-i-1)//2):
            a, b, c = i, j+i+1, j+j+i+2
            # print(a, b, c)
            if {s[b], s[c]} == {"R", "G"}:
                ans -= 1
# print(num_list)
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 d in range(n):
        j=i+d
        k=j+d
        if k>=n:
            break
        if (s[i]!=s[j]) & (s[j]!=s[k]) & (s[k]!=s[i]):
            ans-=1

print(ans)

Submission status: Accepted

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

w=s.count("R")*s.count("G")*s.count("B")
cnt=0

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

answer=w-cnt
print(answer)

Submission status: Accepted

Solution code:
from itertools import combinations

n = int(input())
s = input()
rs = s.count("R")
gs = s.count("G")
bs = s.count("B")
ans = rs * gs * bs

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]:
            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')
max = r * g * b 
 
count = 0
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[j] != S[k] and S[i] != S[k]:
			count += 1
		
		
 
print(max - count)

Submission status: Accepted

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

n_R, n_G, n_B, n_total = 0, 0, 0, 0
for i in range(N):
  if S[i] == 'R':
    n_R+=1
  elif S[i] == 'G':
    n_G+=1
  else:
    n_B+=1
n_total += (n_R*n_G*n_B)
for j in range(1,N-1):
  i = j-1
  k = j+1
  while 1:
    if 0 <= i and k < N:
      if S[i] != S[j] and S[j] != S[k] and S[i] != S[k]:
        #print(i,j,k, S[i], S[j], S[k])
        n_total-=1
    else:
      break
    i -= 1
    k += 1
print(n_total)

Submission status: Accepted

Solution code:
def main():
    n, s = int(input()), input()
    r, g, b = 0, 0, 0
    for i in s:
        if i == 'R':
            r += 1
        if i == 'G':
            g += 1
        if i == 'B':
            b += 1

    ans = r * g * b
    s = tuple(s)
    v = {'R', 'G', 'B'}
    for i in range(n - 2):
        for j in range(n // 2 + 1):
            if i + j + j < n:
                if v == {s[i], s[i + j], s[i + j + j]}:
                    ans -= 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    n, s = int(input()), input()
    r, g, b = s.count('R'), s.count('G'), s.count('B')
    ans = r * g * b
    s = tuple(s)
    v = {'R', 'G', 'B'}
    for i in range(n - 2):
        for j in range(n // 2 + 1):
            if i + j + j < n:
                if v == {s[i], s[i + j], s[i + j + j]}:
                    ans -= 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    n, s = int(input()), input()
    r, g, b = s.count('R'), s.count('G'), s.count('B')
    ans = r * g * b
    v = {'R', 'G', 'B'}
    for i in range(n - 2):
        for j in range(n // 2 + 1):
            if i + j + j < n:
                if v == {s[i], s[i + j], s[i + j + j]}:
                    ans -= 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

def solve(N: int, S: str):
    counter = collections.defaultdict(int)
    for s in S:
        counter[s] += 1
    result = 1
    for k, v in counter.items():
        result *= v
    if len(counter) < 3:
        result = 0

    notcounter = 0
    for i in range(N):
        for j in range(i+1, N):
            diff = j - i
            if j + diff >= N:
                break
            if (S[i]!=S[j]) and (S[j]!=S[j+diff]) and (S[i]!=S[j+diff]):
                notcounter += 1
    print(result - notcounter)
    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:
n = int(input())
 
str = input()

r = str.count('R')
g = str.count('G')
b = str.count('B')
sum = r * g * b
for i in range(n-2):
  for j in range(i+1, n-1):
    k = 2 * j - i
    if k < n:
      if str[i] != str[j] and str[i] != str[k] and str[j] != str[k] :
        sum -= 1
    
print(sum)

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

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
inp = sys.stdin.buffer.readline                               
def I(): return list(map(int,inp().split()))
from collections import Counter

n=I()[0]
s=input() 
cut=0
for i in range(len(s)):
    for j in range(i+1,len(s)):
        if 2*j-i>=len(s): break
        if len({s[i],s[j],s[2*j-i]})==3: 
            cut+=1
d=Counter(s) ; mul=1
for i in d.values():
    mul*=i
if len(d)<3: print(0)
else: print(mul-cut)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    S = input()
    cnt = {
        'R': [0] * N,
        'G': [0] * N,
        'B': [0] * N
    }

    for i in range(N):
        if i >= 1:
            for c in cnt:
                cnt[c][i] = cnt[c][i-1]
        c = S[i]
        cnt[c][i] += 1
    ans = 0
    for i in range(N):
        for k in range(i+2, N):
            if S[i] != S[k]:
                c1 = S[i]
                c2 = S[k]
                ans += (k-i) - (cnt[c1][k] - cnt[c1][i]) \
                             - (cnt[c2][k] - cnt[c2][i])
                if (k-i) % 2 == 0:
                    j = i + (k-i) // 2
                    if S[i] != S[j] and S[j] != S[k]:
                        ans -= 1
    print(ans)


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')
max = r * g * b 

count = 0
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] or S[j] == S[k] or S[i] == S[k]:
			continue
		
		count += 1

print(max - count)

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
inp = sys.stdin.buffer.readline                               
def I(): return list(map(int,inp().split()))
from collections import Counter

n=I()[0]
s=input() 
cut=0
for i in range(len(s)):
    for j in range(i+1,len(s)):
        if 2*j-i>=len(s): break
        if not(s[i]==s[j] or s[i]==s[2*j-i] or s[j]==s[2*j-i]): 
            cut+=1
d=Counter(s) ; mul=1
for i in d.values():
    mul*=i
if len(d)<3: print(0)
else: print(mul-cut)

Submission status: Accepted

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

kumikazu = 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:
          break
        if (s[i] != s[j]) and (s[i] != s[k]) and (s[j] != s[k]):
                kumikazu -= 1
print(kumikazu)

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
inp = sys.stdin.buffer.readline                               
def I(): return list(map(int,inp().split()))
from collections import Counter
from numpy import prod

n=I()[0]
s=input() 
cut=0
for i in range(len(s)):
    for j in range(i+1,len(s)):
        if 2*j-i>=len(s): break
        if len({s[i],s[j],s[2*j-i]})==3:
            cut+=1
d=Counter(s)
if len(d)<3: print(0)
else: print(prod(list(d.values()))-cut)

Submission status: Accepted

Solution code:
import sys
si = sys.stdin.readline
so = sys.stdout.write


def main():
    n = int(si())
    s = si()[:-1]
    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 = j+j-i
            if k < n and s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
                cnt -= 1

    print(cnt)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

sum_R = 0
sum_B = 0
sum_G = 0
sum_A = 0

for i in range(N):
    if S[i] == "R":
        sum_R = sum_R +1
    if S[i] == "G":
        sum_G = sum_G+1
    if S[i] == "B":
        sum_B = sum_B +1
     
sum_A = sum_R*sum_G*sum_B

M = int((N-1)/2)

for j in range(1,M+1):
    for i in range(j):
        if S[i]!=S[j] and S[j]!=S[j+(j-i)] and S[i]!=S[j+(j-i)]:
            sum_A = sum_A -1

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
 
 
ans = S.count('R') * S.count('G') * S.count('B')
 
SN = []
 
for s in S:
    if s == 'R': SN.append(-1)
    elif s == 'G': SN.append(0)
    else: SN.append(1)
 
rem2 = ['RRR', 'GGG', 'BBB']
 
count = 0
for i in range(1, N-1):
    index = i
    left = i - 1
    right = i + 1
    f = True
    while f:
        if left == -1 or right == N:
            break
        if SN[left] == SN[index] and SN[index] == SN[right] == 0 : pass
        elif SN[left] + SN[index] + SN[right] == 0 :
            count+= 1
        
        left -= 1
        right += 1
 
print(ans-count)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

MOD = 10**9
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    return divisors

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


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

SN = []

for s in S:
    if s == 'R': SN.append(-1)
    elif s == 'G': SN.append(0)
    else: SN.append(1)

rem2 = ['RRR', 'GGG', 'BBB']

count = 0
for i in range(1, N-1):
    index = i
    left = i - 1
    right = i + 1
    f = True
    while f:
        if left == -1 or right == N:
            break
        if SN[left] == SN[index] and SN[index] == SN[right] == 0 : pass
        elif SN[left] + SN[index] + SN[right] == 0 :
            count+= 1
        
        left -= 1
        right += 1

print(ans-count)

Submission status: Accepted

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

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

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

print(RGB_cnt[0]*RGB_cnt[1]*RGB_cnt[2]-cnt)

Submission status: Accepted

Solution code:
n = int(input())
rgb = input()
r = set()
g = set()
b = set()
for i in range(len(rgb)):
    if rgb[i] == 'R':
        r.add(i + 1)
    if rgb[i] == 'G':
        g.add(i + 1)
    if rgb[i] == 'B':
        b.add(i + 1)
otv = 0
for i in r:
    for j in g:
        lb = len(b)
        if (i + j) % 2 == 0 and (i + j) // 2 in b:
            lb -= 1
        if 2 * j - i in b:
            lb -= 1
        if 2 * i - j in b:
            lb -= 1
        otv += lb
print(otv)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
nums=[[0]*N for _ in range(3)]
for i in range(N):
  if S[i]=='R':
    nums[0][i]+=1
  elif S[i]=='G':
    nums[1][i]+=1
  else:
    nums[2][i]+=1
    
for i in range(1, N):
  nums[0][i]+=nums[0][i-1]
  nums[1][i]+=nums[1][i-1]
  nums[2][i]+=nums[2][i-1]
  
ans=0
for i in range(N-1, 1, -1):
  if S[i]=='R':
    ans+=nums[1][i-1]*nums[2][i-1]
  elif S[i]=='G':
    ans+=nums[0][i-1]*nums[2][i-1]
  else:
    ans+=nums[0][i-1]*nums[1][i-1]
    
tmp=0
for j in range(1, N-1):
  i=j-1
  k=j+1
  while i>=0 and k<=N-1:
    if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
      ans-=1
    i-=1
    k+=1
print(ans-tmp)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
A = int(input())
B = input()
count_r = B.count("R")
count_g = B.count("G")
count_b = B.count("B")
ans = count_b * count_g * count_r
for i in range(A):
    for j in range(A):
        d = i+j
        f = d+j
        if f >= A:
            break
        if B[i] != B[d] and B[d] != B[f] and B[i] != B[f]:
            ans -= 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
rgb = s.count('R')*s.count('G')*s.count('B')
for r in (range(n-2)):
    for g in range(r+1,n-1):
        b = g-r+g
        if(b>=n):
            break
        if(s[r] != s[g] and s[r] != s[b] and s[g] != s[b]):
            rgb -= 1
print(rgb)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
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):
    k = 2 * j - i
    if ( k >= n ):
      break
    if (s[i] != s[j]) & (s[j] != s[k]) & (s[i] != s[k]):
      RGB -= 1
print(RGB)

Submission status: Accepted

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

cntr=0
cntg=0
cntb=0

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

cntng = 0
cntng += cntr*(cntr-1)//2*(cntg+cntb)
cntng += cntg*(cntg-1)//2*(cntr+cntb)
cntng += cntb*(cntb-1)//2*(cntr+cntg)
cntng += cntr*(cntr-1)*(cntr-2)//6
cntng += cntg*(cntg-1)*(cntg-2)//6
cntng += cntb*(cntb-1)*(cntb-2)//6



for i in range(n):
    for sa in range(1,n):
        if i + 2*sa >= n:
            break
        if s[i] != s[i+sa] and s[i+sa] != s[i+2*sa] and s[i+2*sa] != s[i]:
            cntng+=1

print(n*(n-1)*(n-2)//6 - cntng)

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:
        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:
import re
import sys
from itertools import product
from typing import Callable, Set, Tuple


scan: Callable[[], str] = sys.stdin.readline


def main():
    n: int = int(scan().rstrip())
    s: str = scan().rstrip()

    r_n: int = s.count("R")
    g_n: int = s.count("G")
    b_n: int = s.count("B")

    exceptions = 0
    for i in range(n):
        candidates = {"R", "G", "B"} - {s[i]}
        for j in range(1, (n - i + 1) // 2):
            if candidates == {s[i + j], s[i + 2 * j]}:
                exceptions += 1

    answer = r_n * g_n * b_n - exceptions
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
a=int(input())
moji=input()
r=[]
g=[]
b=[]
for i in range(a):
    if moji[i]=="R":
        r.append(i)
    elif moji[i]=="G":
        g.append(i)
    else:
        b.append(i)
res=len(r)*len(g)*len(b)
if len(r)==0 or len(g)==0 or len(b)==0:
    print(0)
else:
    min_len={0:r,1:g,2:b}
    if len(min_len[0])>len(min_len[1]):
        tmp=min_len[0]
        min_len[0]=min_len[1]
        min_len[1]=tmp
    if len(min_len[1])>len(min_len[2]):
        tmp=min_len[1]
        min_len[1]=min_len[2]
        min_len[2]=tmp
    if len(min_len[0])>len(min_len[1]):
        tmp=min_len[0]
        min_len[0]=min_len[1]
        min_len[1]=tmp
    a1=[]
    for i in range(3):
        a1.append(min_len[i])
    A=set(a1[0])
    B=set(a1[1])
    C=set(a1[2])
    for i in A:
        for j in B:
            if -abs(i-j)+min(i,j) in C:
                res-=1
            if abs(i-j)+max(i,j) in C:
                res-=1
            if abs(i-j)%2==0 and  int(abs(i-j)/2)+min(i,j) in C:
                res-=1
    print(res)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
total = S.count('R') * S.count('G') * S.count('B')
cnt = 0

for i in range(0, N-2):
    for j in range(i+1, N-1):
        if S[i] == S[j]:
            continue
        k = 2 * j - i
        if k > N - 1:
            break
        if S[i] == S[k] or S[j] == S[k]:
            continue
        cnt += 1
print(total - cnt)

Submission status: Accepted

Solution code:
a=int(input())
moji=input()
r=set()
g=set()
b=set()
for i in range(a):
    if moji[i]=="R":
        r.add(i)
    elif moji[i]=="G":
        g.add(i)
    else:
        b.add(i)
res=len(r)*len(g)*len(b)
if len(r)==0 or len(g)==0 or len(b)==0:
    print(0)
else:
    min_len={0:r,1:g,2:b}
    a1=[]
    for i in range(3):
        a1.append(min_len[i])
    for i in a1[0]:
      for j in a1[1]:
        if -abs(i-j)+min(i,j) in a1[2]:
          res-=1
        if abs(i-j)+max(i,j) in a1[2]:
          res-=1
        if abs(i-j)%2==0 and  int(abs(i-j)/2)+min(i,j) in a1[2]:
          res-=1
    print(res)

Submission status: Accepted

Solution code:
N = int(input())
S = input()     
all = S.count("R") * S.count("G") * S.count("B")
reigai = 0
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]:
            reigai += 1
print(all - reigai)

Submission status: Accepted

Solution code:
a=int(input())
moji=input()
r=[]
g=[]
b=[]
for i in range(a):
    if moji[i]=="R":
        r.append(i)
    elif moji[i]=="G":
        g.append(i)
    else:
        b.append(i)
res=len(r)*len(g)*len(b)
if len(r)==0 or len(g)==0 or len(b)==0:
    print(0)
else:
    for i in range(0,a-2):
      for j in range(i+1,a-1):
        if 2*j-i<a and moji[i]!=moji[j] and moji[j]!=moji[2*j-i] and moji[2*j-i]!=moji[i]:
          res-=1
    print(res)

Submission status: Accepted

Solution code:
N = int(input())
S = input()     
all = S.count("R") * S.count("G") * S.count("B")
reigai = 0
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]:
            reigai += 1
print(all - reigai)

Submission status: Accepted

Solution code:
n = int(input())
s = str(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):
  for j in range(n):
    if 2 * j + i < n and s[i] != s[i + j] and s[i] != s[2 * j + i] and s[i + j] != s[2 * j + 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:
            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()

RGB = 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 ):
      break
    if (S[i] != S[j]) & (S[j] != S[k]) & (S[i] != S[k]):
      RGB -= 1


print(RGB)