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: Runtime Error

Solution code:
import numpy as np
import pandas as pd

#N=int(input())
#S=input()
S_list = pd.Series([x for x in S])
r = sum(S_list == "R")
g = sum(S_list == "G")
b = sum(S_list == "B")
 
ans = r * g * b
 
for i in range(N):
  for k in [N - k_ - 1 for k_ in range(N)]:
    tar_j = i + k
    if (tar_j > 2 * i) & (tar_j < 2 * k):
      if np.mod(tar_j, 2) == 0:
        j = int(tar_j / 2)
        s = S[i] + S[j] + S[k]
        if ("R" in s) & ("G" in s) & ("B" in s):
          ans -= 1
    else:
      break
print(ans)

Submission status: Runtime Error

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 not S[i] == S[j] and not S[j] == S[k] and not S[k] == S[i]:
            sum -= 1
print(sum)

Submission status: Runtime Error

Solution code:
import numpy as np
N=int(input())
S=input()
r = sum(S == "R")
g = sum(S == "G")
b = sum(S == "B")

ans = r * g * b

for i in range(N):
  for k in [N - k_ - 1 for k_ in range(N)]:
    tar_j = i + k
    if (tar_j > 2 * i) & (tar_j < 2 * k):
      if np.mod(tar_j, 2) == 0:
        j = tar_j / 2
        s = S[i] + S[j] + S[k]
        if ("R" in s) & ("G" in s) & ("B" in s):
          ans -= 1
    else:
      break
print(ans)

Submission status: Runtime Error

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+1,N):
    b = S[j]
    if (i+j)%2 :
      continue
    k = (i+j)//2
    if [a,S[k],b].sort() == ['B','G','R']
      ans -= 1
      
print(ans)

Submission status: Runtime Error

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

ans = 0
for a in range(0,n-2):
    for b in range(a,n-1):
        e = b-a
        #if s[a]!=s[b]:
            for c in range(b,n):
                f = c-b
                if e!=f:
                    if s[a]!=s[c] and s[b]!=s[c] and s[a]!=s[b]:
                        ans += 1

print(ans)

Submission status: Runtime Error

Solution code:
def main():
    N=int(input())
    S=input()
    ans=0
    for i in range(N-2):
        for j in range(i+1,N-1):
            lis=["R","G","B"]
            if S[i]!=S[j]:
                lis=["R","G","B"]
                lis.remove(S[i],S[j])
                k=S[j+1:].count(lis[0])
                ans=ans+k
            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: Runtime Error

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

print(r * g * b - cnt)

Submission status: Runtime Error

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

#output
from collections import Counter
answer = 0
if N <= 3:
    continue
else:
    for i in range(N-2):
        for j in range(i+1, N-1):
            k = 2*j-i
            if S[i] == "R":
                if S[j] == "G":
                    answer += Counter(S[j+1:])["B"]
                    if k < N:
                        if S[k] == "B":
                            answer -= 1
                elif S[j] == "B":
                    answer += Counter(S[j+1:])["G"]
                    if k < N:
                        if S[k] == "G":
                            answer -= 1
            elif S[i] == "G":
                if S[j] == "R":
                    answer += Counter(S[j+1:])["B"]
                    if k < N:
                        if S[k] == "B":
                            answer -= 1
                elif S[j] == "B":
                    answer += Counter(S[j+1:])["R"]
                    if k < N:
                        if S[k] == "R":
                            answer -= 1
            elif S[i] == "B":
                if S[j] == "R":
                    answer += Counter(S[j+1:])["G"]
                    if k < N:
                        if S[k] == "G":
                            answer -= 1
                elif S[j] == "G":
                    answer += Counter(S[j+1:])["R"]
                    if k < N:
                        if S[k] == "R":
                            answer -= 1
                
print(answer)

Submission status: Runtime Error

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(r * g * b - ng_cnt)

Submission status: Runtime Error

Solution code:
n = int(input())
s = list(input())
#
ans = 0
def R(x):
    if x == 0:
        if s[0] == 'R':
            return 1
        else:
            return 0
    else:
        if s[x] == 'R':
            return R(x-1) + 1
        else:
            return R(x-1)
def B(x):
    if x == 0:
        if s[0] == 'B':
            return 1
        else:
            return 0
    else:
        if s[x] == 'B':
            return B(x-1) + 1
        else:
            return B(x-1)
def G(x):
    if x == 0:
        if s[0] == 'G':
            return 1
        else:
            return 0
    else:
        if s[x] == 'G':
            return G(x-1) + 1
        else:
            return G(x-1)

for i in range(n-2):
    for j in range(i+1,n-1):
        if s[i] != s[j]:
            if s[i] != 'R' and s[j] != 'R':
                if 2*j-i <= n-1 and s[2*j-i] == 'R':
                    ans += R(n-1) - R(j) - 1
                else:
                    ans += R(n-1) - R(j)
            elif  s[i] != 'B' and s[j] != 'B':
                if 2*j - i <= n-1 and s[2*j-i] == 'B':
                    ans += B(n-1) - B(j) - 1
                else:
                    ans += B(n-1) - B(j)
            elif s[i] != 'G' and s[j] != 'G':
                if 2*j - i <= n-1 and s[2*j-i] == 'G':
                    ans += G(n-1) - G(j) - 1
                else:
                    ans += G(n-1) - G(j)
        else:
            pass
print(ans)

Submission status: Runtime Error

Solution code:
import sys
import clipboard

# テストケースが複数行の場合、iOSだと一気に読み込んでしまう。
# 本番環境と同じ挙動になるように、
# iOSではテストケースをいったんテキストファイルにコピーして
# テキストファイルから input() する
if sys.platform == 'ios':
	sample = clipboard.get()
	file = open('input_file.txt', 'w')
	file.write(sample)
	file.close()
	sys.stdin = open('input_file.txt')
# -----------------------------
						

# D - RGB Triplets

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

r, g, b = 0, 0, 0

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

ans = r * g * b

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

print(ans)

Submission status: Runtime Error

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)
    elif moji[i]=="B":
        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={r:0,g:1,b:2}
    if len(r)>len(g):
        tmp=min_len[r]
        min_len[r]=min_len[g]
        min_len[g]=tmp
    if len(g)>len(b):
        tmp=min_len[g]
        min_len[g]=min_len[b]
        min_len[b]=tmp
    if len(b)>len(r):
        tmp=min_len[b]
        min_len[b]=min_len[r]
        min_len[r]=tmp
    
    for key,value in min_len.items():
        if value==0:
            a1=key
        elif value==1:
            a2=key
        elif value==2:
            a3=key
    
    for i in a1:
        for j in a2:
            if -abs(i-j)+min(i,j) in a3:
                res-=1
            if abs(i-j)+max(i,j) in a3:
                res-=1
            if abs(i-j)%2==0 and  int(abs(i-j)/2)+min(i,j) in a3:
                res-=1
    print(res)

Submission status: Runtime Error

Solution code:


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

cnt = S.count('B')*S.count('G')*S.count('R')
# for i in range(1,N-1):
#     for j in range(i+1,N):
#         if S[i-1] != S[j-1]:
#             for k in range(j+1,N+1):
#                 if S[i-1]!=S[k-1] and S[j-1]!=S[k-1] and j-i!=k-j:
#                     cnt += 1
for i in range(N-2):
    for j in range(i,N-1):
        k = 2*j-i
        if k>j and k<=N:
            if S[i]==S[j] or S[i]==S[k] or S[j]==S[k] :
                cnt -= 1


print(cnt)

Submission status: Runtime Error

Solution code:


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

cnt = S.count('B')*S.count('G')*S.count('R')
# for i in range(1,N-1):
#     for j in range(i+1,N):
#         if S[i-1] != S[j-1]:
#             for k in range(j+1,N+1):
#                 if S[i-1]!=S[k-1] and S[j-1]!=S[k-1] and j-i!=k-j:
#                     cnt += 1
for i in range(1,N-1):
    for j in range(i+1,N):
        k = 2*j-i
        if k>j and k-1<=N:
            if S[i-1]==S[j-1] or S[i-1]==S[k-1] or S[j-1]==S[k-1] :
                cnt -= 1


print(cnt)

Submission status: Runtime Error

Solution code:
k=int(input())
l=[1, 9, 30, 76, 141, 267, 400, 624, 885, 1249, 1590, 2208, 2689, 3411, 4248, 5248, 6081, 7485, 8530, 10248, 11889, 13687, 15228, 17988, 20053, 22569, 25242, 28588, 31053, 35463, 38284, 42540, 46581, 50893, 55362, 61824, 65857, 71247, 76884, 84388, 89349, 97881, 103342, 111528, 120141, 128047, 134580, 146316, 154177, 164817, 174438, 185836, 194157, 207927, 218812, 233268, 245277, 257857, 268182, 288216, 299257, 313635, 330204, 347836, 362973, 383709, 397042, 416448, 434025, 456967, 471948, 499740, 515581, 536073, 559758, 583960, 604833, 633651, 652216, 683712, 709065, 734233, 754734, 793188, 818917, 846603, 874512, 909496, 933081, 977145, 1006126, 1041504, 1073385, 1106467, 1138536, 1187112, 1215145, 1255101, 1295142, 1342852, 1373253, 1422195, 1453816, 1502376, 1553361, 1595437, 1629570, 1691292, 1726717, 1782111, 1827492, 1887772, 1925853, 1986837, 2033674, 2089776, 2145333, 2197483, 2246640, 2332104, 2379085, 2434833, 2490534, 2554600, 2609625, 2693919, 2742052, 2813988, 2875245, 2952085, 3003306, 3096024, 3157249, 3224511, 3306240, 3388576, 3444609, 3533637, 3591322, 3693924, 3767085, 3842623, 3912324, 4027884, 4102093, 4181949, 4270422, 4361548, 4427853, 4548003, 4616104, 4718640, 4812789, 4918561, 5003286, 5131848, 5205481, 5299011, 5392008, 5521384, 5610705, 5739009, 5818390, 5930196, 6052893, 6156139, 6239472, 6402720, 6493681, 6623853, 6741078, 6864016, 6953457, 7094451, 7215016, 7359936, 7475145, 7593865, 7689630, 7886244, 7984165, 8130747, 8253888, 8403448, 8523897, 8684853, 8802826, 8949612, 9105537, 9267595, 9376656, 9574704, 9686065, 9827097, 9997134, 10174780, 10290813, 10493367, 10611772, 10813692]
print(l[k-1])

Submission status: Runtime Error

Solution code:


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

cnt = 0

for i in range(1,N-1):
    for j in range(i+1,N):
        k = 2*j-i
        if S[i-1]==S[j-1] or S[i-1]==S[k-1] or S[j-1]==S[k-1] :
            cnt += 1


print(N-cnt)

Submission status: Runtime Error

Solution code:


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

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


print(N-cnt)

Submission status: Runtime Error

Solution code:


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

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


print(N**3-cnt)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
from itertools import product

x1 = int(input())
x2 = input()

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

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

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

s = 0
for a in xs:
    k = a[1]*2 - a[0]  # 2j-i
    if k in B_i:
        s +=1
    i = a[0]*2 - a[1]  # 2j-k
    if i in B_i:
        s +=1
    j = (a[0] + a[1]) / 2
    if j.is_integer():
        j = int(j)
        if j in B_i:
            s +=1
print(R*G*B - s)

Submission status: Runtime Error

Solution code:
import sys
A=[a.rstrip() for a in sys.stdin.readlines(3)]
N=int(A[0])
S="".join(["A",A[1]])

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

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

Submission status: Runtime Error

Solution code:
x1 = int(input())
x2 = input()

R_i = [i for i, x in enumerate(x2) if x == "R"]
G_i = [i for i, x in enumerate(x2) if x == "G"]
B_i = [i for i, x in enumerate(x2) if x == "B"]

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

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

s = 0
for a in xs:
    k = a[1]*2 - a[0]  # 2j-i
    if k in B_i:
        s +=1
    i = a[0]*2 - a[1]  # 2j-k
    if i in B_i:
        s +=1
    j = (a[0] + a[1]) / 2
    if j.is_integer():
        j = int(j)
        if j in B_i:
            s +=1
print(R*G*B - s)

Submission status: Runtime Error

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

print(ans)

Submission status: Runtime Error

Solution code:
def main():
    n = int(input())
    S = input()
    '''
    r_inds = []
    g_inds = []
    b_inds = []
    for i in range(len(S)):
        if S[i] == "R":
            r_inds.append(i)
        elif S[i] == "G":
            g_inds.append(i)
        else:
            b_inds.append(i)

    total_comb = len(r_inds)*len(g_inds)*len(b_inds)
    ded = 0
    for i in r_inds:
        for j in g_inds:
            s_ind = min(i, j)
            b_ind = max(i, j)
            diff = b_ind - s_ind
            bigger_ind = b_ind + diff
            smaller_ind = s_ind - diff
            if bigger_ind < len(S) and bigger_ind in b_inds:
                ded += 1
            if smaller_ind >= 0 and smaller_ind in b_inds:
                ded += 1
            if diff % 2 == 0:
                mid_diff = diff // 2
                mid_ind = s_ind + mid_diff
                if mid_ind in b_inds:
                    ded += 1
    print(total_comb - ded)
    '''
    color_num = defaultdict(int)
    num_subst = 0

    for i in range(len(S) - 1):
        color_num[S[i]] += 1 
        for j in range(i + 1, len(S)):
            if S[i] != S[j]:
                diff = j - i
                idx = j + diff
                if idx < len(S) and S[idx] != S[i] and S[idx] != S[j]:
                    num_subst += 1
    color_num[S[len(S) - 1]] += 1 
    total_num_comb = color_num["R"] * color_num["G"] * color_num["B"]
    print(total_num_comb - num_subst)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

ans = 0

def first(a,b,c):
    if N[a] != N[b] and N[a] != N[c] and N[b] != N[c]:
        return 0
    else:
        return 1

def second(i,j,k):
    if j-i != k-j:
        return 0
    else:
        return 0

for i in range(1,N+1):
    for j in range(1,N+1):
        for k in range(1,N+1):
            check = 0
            check += first(i,j,k)
            check += second(i,j,k)
            if check == 0:
                ans += 1

print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
s=list(input())
a=s.count("R")
b=s.count("G")
c=s.count("B")
ans=0
for i in range(n-2):
  for j in range(i+1,n-1):
    k=2*j-i
    if n>=k and (s[i]==s[j] or s[i]==s[k] or s[j]==s[k]): 
      ans=ans+1
print(abc-ans)

Submission status: Runtime Error

Solution code:
n = int(input())
s = str(input())
r = n.count('R')
g = n.count('B')
b = n.count('G')
ans = r * b * g
for i in range(1, n + 1):
    for j in range(1, n + 1):
        k = j + j - i
        if k > n or k <= 0:
            break
        if s[i - 1] != s[j - 1] and s[j - 1] != s[k - 1] and s[k - 1] != s[i - 1]:
            ans -= 0.5
print(ans)

Submission status: Runtime Error

Solution code:
n,s = input().split()
n = int(n)
sum = 0
for i in range(n):
  for j in range(i+1,n):
    for k in range(j+1,n):
      if s[i]!=s[j] and s[i]!=s[k] and s[j]!=s[k] and (j-i)!=(k-j):
        sum += 1
print(sum)

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
count = 0
def findlet(let,ind):
    add = st[ind:].count(let)
    if add > 0:
        if 2*ind < len():
            if st[2*ind] == let:
                add -= 1
        global count
        count += add
for i in range(N-2):
    st = S[i:]
    if st[0] == "R":
        for j in [x for x, y in enumerate(st) if y == 'G']:
            findlet("B",j)
        for j in [x for x, y in enumerate(st) if y == 'B']:
            findlet("G",j)
    elif st[0] == "G":
        for j in [x for x, y in enumerate(st) if y == 'B']:
            findlet("R",j)
        for j in [x for x, y in enumerate(st) if y == 'R']:
            findlet("B",j)
    else:
        for j in [x for x, y in enumerate(st) if y == 'R']:
            findlet("G",j)
        for j in [x for x, y in enumerate(st) if y == 'G']:
            findlet("R",j)
print(count)

Submission status: Runtime Error

Solution code:

# -*- coding: utf-8 -*-

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

count=0

rs, gs, bs=[], [], []
for i,s in enumerate(S):
    if s =='R':
        rs.append(i+1)
    elif s=='G':
        gs.append(i+1)
    else: bs.append(i+1)

n1=len(rs)
n2=len(gs)
n3=len(bs)
ls=[rs,gs,bs]
ns=[n1, n2, n3]
l1=ls[ns.index(min(ns))]
l3=ls[ns.index(max(ns))]
ls.remove(l1)
ls.remove(l3)
l2=ls[0]
n=len(l3)

for i in l1:
    for j in l2:
        co=0
        i1=min([i,j])
        i2=max([i,j])
        o1=2*i-j
        o2=2*j-i
        o3=0.5*(i+j)
        if  o1 in l3: co+=1
        if  o2 in l3: co+=1
        if  o3 in l3: co+=1
        count+=n-co

            
print(count)

Submission status: Runtime Error

Solution code:
n = int(input())
s = str(input())
ans = 0
for i in range(n):
    for j in range(n):
        k = j + j - i
        if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
            ans += 1
print(ans)

Submission status: Runtime Error

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)
ans = len(r)*len(g)*len(b)
def func(a,b,c):
    ans = 0
    for i in a:
        for k in [num1 for num1 in b if num1 > i+1]:
            if (k + i) // 2 in j:
                    ans -= 1
    return ans
print(ans + func(r,g,b) + func(r,b,g) + func(g,r,b) + func(g,b,r) + func(b,g,r) + func(b,r,g))

Submission status: Runtime Error

Solution code:
length = int(input())
text = input()

r_count = text.count("R")
g_count = text.count("G")
b_count = text.count("B")

answer = r_count * g_count * b_count

for i in range(length-2):
    for j in range(i+1, length-1):
        k = i + (i-j)*2
        if k < length and text[i]!=text[j] and text[i]!=text[k] and text[j]!=text[k]:
            answer -= 1
        
print(answer)

Submission status: Runtime Error

Solution code:
from bisect import bisect_left

N = int(input())
S = input()
R = []
G = []
B = []

for idx, s in enumerate(S):
    if s == 'R':
        R.append(idx+1)
    if s == 'G':
        G.append(idx+1)
    if s == 'B':
        B.append(idx+1)
# print(R, G, B)

R0, G0, B0 = R, G, B

sum = 0

for i in range(6):
    if i == 0:
        R, G, B, temp = R0, G0, B0, 'B'
    elif i == 1:
        R, G, B, temp = R0, B0, G0, 'G'
    elif i == 2:
        R, G, B, temp = B0, R0, G0, 'G'
    elif i == 3:
        R, G, B, temp = B0, G0, R0, 'R'
    elif i == 4:
        R, G, B, temp = G0, R0, B0, 'B'
    elif i == 5:
        R, G, B, temp = G0, B0, R0, 'R'

    for s in R:
        if s > G[-1]:
            break
        idx1 = bisect_left(G, s)
        for g in G[idx1:]:
            if g > B[-1]:
                break
            idx2 = bisect_left(B, g)
            sum += len(B[idx2:])
            if 2 * g - s <= N:
                if S[2 * g - s - 1] == temp:
                    sum -= 1

print(sum)

Submission status: Runtime Error

Solution code:
import sys


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    n = int(readline())
    s = str(readline().rstrip().decode('utf-8'))
    r = s.count("R")
    g = s.count("G")
    b = s.count("B")
    cnt = r *      if s[i] != s[j] and s[j] != s[k] and s[i] != s[k]:
                    cnt -= 1
    print(cnt)


if __name__ == '__main__':
    solve()

Submission status: Runtime Error

Solution code:
s=str(input())
s=list(s)
 
import numpy as np
 
def なんか(a,b,c):
    ans=0
    for i in a:
        tmp_b = b[b>i]
        for j in tmp_b:
            if j+j-i in c:
                ans+=len(c[(c>j)])-1
            else:
                ans+=len(c[(c>j)])
        tmp_c = c[c>i]
        for j in tmp_c:
            if j+j-i in b:
                ans+=len(b[(b>j)])-1
            else:
                ans+=len(b[(b>j)])
    return ans
  
 
r1=[]
b1=[]
g1=[]
 
for i, v in enumerate(s):
    if v=="R":
        r1+=[i]
    elif v=="B":
        b1+=[i]
    else:
        g1+=[i]
        
r1 = np.array(r1)
b1 = np.array(b1)
g1 = np.array(g1)
 
print(なんか(r1, g1, b1)+なんか(g1, b1, r1)+なんか(b1,r1,g1))
s=str(input())
s=list(s)

import numpy as np

def なんか(a,b,c):
    ans=0
    for i in a:
        tmp_b = b[b>i]
        for j in tmp_b:
            if j+j-i in c:
                ans+=(len(c)-1)*len(tmp_b)
            else:
                ans+=len(c)*len(tmp_b)
    return ans
  

r1=[]
b1=[]
g1=[]

for i, v in enumerate(s):
    if v=="R":
        r1+=[i]
    elif v=="B":
        b1+=[i]
    else:
        g1+=[i]
        
r1 = np.array(r1)
b1 = np.array(b1)
g1 = np.array(g1)

print(なんか(r1, g1, b1)+なんか(g1, b1, r1)+なんか(b1,r1,g1))
提出情報

Submission status: Runtime Error

Solution code:
import itertools

N = int(input())
S = input()
out = S.count('R') * S.count('G') * S.count('B')
for i in range(1, N):
  for j in range(N-i*2):
    if (S[j], S[j+i], S[j+i*2]) in RGBs:
      out -= 1
print(out)

Submission status: Runtime Error

Solution code:
import sys
#sys.exit()
#sorted( ,reverse=True)
N = int(input())
#S,T = map(int, input().split())
#A = list(map(int, input().split()))
#print(N)
#print(S,T)
#print(A)
S = str(input())
r = S.count("R")
g = S.count("G")
b = S.count("B")
ans = (r*g*b)
#r2 = [i for i, x in enumerate(S) if x == 'R']
#g2 = [i for i, x in enumerate(S) if x == 'G']
#b2 = [i for i, x in enumerate(S) if x == 'B']
ans = (len(r2)*len(g2)*len(b2))
for i in range (N-2):
    for j in range (i+1,N-1):
        k = 2*j-i
        if k < N and S[i]!=S[j] and S[j] != S[k] and S[k] != S[i]:
                ans -=1
                #print(r2[i],g2[j],b2[k])
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import numpy as np
N=int(input())
S = input()

c=S.count
r,g,b=c('R'),c('G'),c('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] and S[j] != S[k] and S[i] != S[i]:
      ans -= 1
    
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
N=int(input())
S = input()

c=S.count
r,g,b=c('R'),c('G'),c('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] and s[j] != s[k] and s[i] != s[i]:
      ans -= 1
    
print(ans)

Submission status: Runtime Error

Solution code:
from collections import*
n=int(input())
s=input()
c=0
r=s.count('R')
g=s.count('G')
b=s.count('B')
rgb=r*g*b

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

print(rgb)

Submission status: Runtime Error

Solution code:
n = int(input())
s = str(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)
  else:
    B.append(i+1)

  
ans = 0

for i in range(len(R)):
  for j in range(len(G)):
    test += len(B)
    temp = 0
    if 2*R[i] - G[j] >0 and 2*R[i] - G[j] <= n:
      if s[2*R[i] - G[j] -1] == "B":
        temp += 1
    if 2*G[j] - R[i] >0 and 2*G[j] - R[i] <= n:
      if s[2*G[j] - R[i]-1] == "B":
        temp += 1
    if (R[i]+G[j])%2 == 0:
      if s[(R[i]+G[j])//2-1] == "B":
        temp += 1
    ans += len(B)- temp

print(ans)

Submission status: Runtime Error

Solution code:
def main():
    N = int(input())
    S = list(str(input()))
    sum = 0
    for i in range(0, N-2):
        si = S[i]
        for j in range(i, N-1):
            sj = S[j]
            k = 2*j -i  # Nを超える
            if k <= N:
                sk = S[k]
                if j - i != k -j and si != sj and si != sk and sj != sk:
                    sum += 1
    print(sum)


main()

Submission status: Runtime Error

Solution code:
def main():
    N = int(input())
    S = list(str(input()))
    sum = 0
    for i in range(0, N-2):
        si = S[i]
        for j in range(i, N-1):
            sj = S[j]
            k = 2*j -i
            sk = S[k]
            if j - i != k -j and si != sj and si != sk and sj != sk:
                sum += 1
    print(sum)


main()

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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

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

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

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

print(ans)

Submission status: Runtime Error

Solution code:
def calc(i, j, B1, len_b):
    extra = 0
    d = j - i

    # i < j < k
    k1 = d + j
    if k1 < len_b:
        extra += B1[k1]

    # k < i < j
    k2 = i - d
    if 0 <= k2 and k2 < len_b:
        extra += B1[k2]

    # i < k < j
    s = i + j
    if s % 2 == 0:
        k3 = s // 2
        if k3 < len_b:
            extra += B1[k3]
    return extra

def main():
    N = int(input())
    S = input()
    R, G, B = [], [], []
    num_r, num_g, num_b = 0, 0, 0
    for i in range(N):
        s = S[i]
        if s == 'R':
            R.append(i)
            num_r += 1
        elif s == 'G':
            G.append(i)
            num_g += 1
        elif s == 'B':
            B.append(i)
            num_b += 1
    len_b = (B[-1] + 1)
    B1 = [0]*len_b
    for b in B:
        B1[b] = 1
    # print(R, G, B)
    ans = 0
    for i in R:
        for j in G:
            ans += num_b
            if i < j:
                ans -= calc(i, j, B1, len_b)
            else:
                ans -= calc(j, i, B1, len_b)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
result = 0
'''
for i in range(N):
    print(result)
    for j in range(i+1,N):
        if S[i] != S[j]:
            for k in range(j ,N):
                if k- j == j - i:
                    continue
                if S[j] != S[k] and S[i] != S[k]:
                    result +=1
print(result)
'''
r = S.count('R')
g = S.count('G')
b = S.count('B')
target = set('RGB')
result = r * g * b
for i in range(N):
    for j in range(1, min(i, (N - i + 1)) + 1):
        if target == set(S[i] + S[i-j] + S[j + i]):
            result -= 1
print(result)

Submission status: Runtime Error

Solution code:
def main():
    n=int(input())
    s=input()
    ans=0
    d=[[0]*n for _ in range(3)]
    m=[[0]*n for _ in range(3)]
    l=[[0,2,1],[2,1,0],[1,0,2]]

    for i in range(n):
        d[ord(s[i])%3][i]=1
        m[ord(s[i])%3][i]=1

    for j in range(3):
        for i in range(n-1,0,-1):
            m[j][i-1]+=m[j][i]

    for i in range(n-1):
        for j in range(i+1,n):
            if s[i]==s[j]:
                continue
            ans+=m[l[ord(s[i])][ord(s[j])]][j+1]
            if j*2-i<n:
                ans-=d[l[ord(s[i])][ord(s[j])]][j*2-i]

    print(ans)
        
main()