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: Time Limit Exceeded

Solution code:
import bisect
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)
res = 0
for i in range(n):
  if s[i] == 'R':
    x = bisect.bisect_left(G, i)
    sss = set()
    for j in range(x, len(G)):
      y = bisect.bisect_left(B, G[j])
      res += len(B) - y
      sss.add(2*G[j]-i)
    res -= len(sss & set(B))
    x = bisect.bisect_left(B, i)
    sss = set()
    for j in range(x, len(B)):
      y = bisect.bisect_left(G, B[j])
      res += len(G) - y
      sss.add(2*B[j]-i)
    res -= len(sss & set(G))
  elif s[i] == 'G':
    x = bisect.bisect_left(B, i)
    sss = set()
    for j in range(x, len(B)):
      y = bisect.bisect_left(R, B[j])
      res += len(R) - y
      sss.add(2*B[j]-i)
    res -= len(sss & set(R))
    x = bisect.bisect_left(R, i)
    sss = set()
    for j in range(x, len(R)):
      y = bisect.bisect_left(B, R[j])
      res += len(B) - y
      sss.add(2*R[j]-i)
    res -= len(sss & set(B))
  else:
    x = bisect.bisect_left(G, i)
    sss = set()
    for j in range(x, len(G)):
      y = bisect.bisect_left(R, G[j])
      res += len(R) - y
      sss.add(2*G[j]-i)
    res -= len(sss & set(R))
    x = bisect.bisect_left(R, i)
    sss = set()
    for j in range(x, len(R)):
      y = bisect.bisect_left(G, R[j])
      res += len(G) - y
      sss.add(2*R[j] - i)
    res -= len(sss & set(G))
print(res)

Submission status: Time Limit Exceeded

Solution code:



def main():
    N = int(input())
    S = list(input())
    ans = 0
    for i in range(N):
        first_s = S[i]
        for j in range(i+1, N):
            if S[j] != first_s:
                second_s = S[j]
                for k in range(j+1, N):
                    if (S[k] != first_s) and (S[k] != second_s):
                        if k == 2*j-i:
                            continue
                        ans += 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = str(input())
 
cnt = 0
for i in range(1, n-1):
  a = s[i-1]
  for j in range(i+1, n):
    b = s[j-1]
    if a != b:
      for k in range(j+1, n+1):
        c = s[k-1]
        if (j-i) != (k-j) and b != c and a != c:
          cnt += 1
print(cnt)
 
#19ms

Submission status: Time Limit Exceeded

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

r, g, b = [0] * n, [0] * n, [0] * n
for i in range(0, n):
    if i == 0:
        if s[i] == 'R':
            r[i] = 1
        elif s[i] == 'G':
            g[i] = 1        
        elif s[i] == 'B':
            b[i] = 1
    else:
        if s[i] == 'R':
            r[i] = r[i-1] + 1
            g[i] = g[i-1]
            b[i] = b[i-1]
        elif s[i] == 'G':
            r[i] = r[i-1]
            g[i] = g[i-1] + 1
            b[i] = b[i-1]
        elif s[i] == 'B':
            r[i] = r[i-1]
            g[i] = g[i-1]
            b[i] = b[i-1] + 1   

cnt = 0
for i in range(0, n-2):
    for j in range(n-1, i+1, -1):
        if s[i] != s[j]:
            if s[i] != 'B' and s[j] != 'B':
                cnt += b[j] - b[i]
                if (i+j) % 2 == 0 and s[(i+j)//2] == 'B':
                    cnt -= 1
            elif s[i] != 'G' and s[j] != 'G':
                cnt += g[j] - g[i]
                if (i+j) % 2 == 0 and s[(i+j)//2] == 'G':
                    cnt -= 1
            elif s[i] != 'R' and s[j] != 'R':
                cnt += r[j] - r[i]
                if (i+j) % 2 == 0 and s[(i+j)//2] == 'R':
                    cnt -= 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
#data = [i for i in range(N)]
#result = []
l = len(S)
cnt = 0
for i in range(l):
  for j in range(i + 1, l):
    for k in range(j + 1, l):
    	if (((S[i] != S[j] and S[i]!=S[k]) and (S[i]!=S[k] and S[j]!=S[k]) and (S[i] != S[j] and S[j]!=S[k])) and (j-i) != (k-j)) and( (i != j and i!= k) and  (j!=k  and i != j) and (i!= k and j!=k) ) :
                cnt = cnt +1
print(cnt)

Submission status: Time Limit Exceeded

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

counta = 0
i = 0

while i < N:
    j = i + 1
    while j < N:
        if S[j] != S[i]:
            k = j + 1
            while k < N:
                if not(S[k] == S[i] or S[k] == S[j]):
                    if k - j != j - i:
                        counta += 1
                k += 1
        j += 1
    i += 1

print(counta)

Submission status: Time Limit Exceeded

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

c = 0

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

print ( c )

Submission status: Time Limit Exceeded

Solution code:
import math
import sys
import collections
def main():
    N=int(input())
    Tr=tuple(input())
    R=[]
    G=[]
    B=[]
    for i in range(N):
       if Tr[i]=="R":
           R.append(i)
       if Tr[i]=="G":
           G.append(i)
       if Tr[i]=="B":
           B.append(i)
    cnt=0
    for i in R:
        for j in G:
            for k in B:
                if max(i,j,k)-(i+j+k-max(i,j,k)-min(i,j,k))!=(i+j+k-max(i,j,k)-min(i,j,k))-min(i,j,k):
                    cnt+=1

    print(cnt)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

ans = 0
sn = len(s)

r = []
if s[0] == "R":
    r.append(1)
else:
    r.append(0)
g = []
if s[0] == "G":
    g.append(1)
else:
    g.append(0)
b = []
if s[0] == "B":
    b.append(1)
else:
    b.append(0)

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

for i in range(n - 2):
    for j in range(i + 1, n - 1):
        if s[i] != s[j]:
            tmp = dict()
            tmp[s[i]] = 0
            tmp[s[j]] = 0
            target_idx = j + (j - i)

            if tmp.get("B", 1) == 1:
                ans += b[-1] - b[j]
                if target_idx >= sn:
                    continue
                if s[target_idx] == "B":
                    ans -= 1
            elif tmp.get("G", 1) == 1:
                ans += g[-1] - g[j]
                if target_idx >= sn:
                    continue
                if s[target_idx] == "G":
                    ans -= 1
            else:
                ans += r[-1] - r[j]
                if target_idx >= sn:
                    continue
                if s[target_idx] == "R":
                    ans -= 1

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# Pypy3のほうがTLE数少なかった、速そう

import itertools
import sys
input = sys.stdin.readline

n = int(input().replace("\n",""))
s = input().replace("\n","")
orgs = s

ans = 0


for i in range(n):
    for j in range(i+1, n):
        if s[i] != s[j]:
            if ('R' != s[i]) and ('R' != s[j]):
                ans += s[j+1:].count('R')
                if len(s) > j+(j-i):
                    if s[j+(j-i)] == 'R':
                        ans -= 1
            elif ('G' != s[i]) and ('G' != s[j]):
                ans += s[j+1:].count('G')
                if len(s) > j+(j-i):
                    if s[j+(j-i)] == 'G':
                        ans -= 1
            elif ('B' != s[i]) and ('B' != s[j]):
                ans += s[j+1:].count('B')
                if len(s) > j+(j-i):
                    if s[j+(j-i)] == 'B':
                        ans -= 1


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

print(ans)

Submission status: Time Limit Exceeded

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

ans = 0

for i in range(N-3):
    for j in range(i + 1, N - 1):
        if S[i] != S[j]:
            x = {'R', 'G', 'B'} - {S[i], S[j]}
            ans += S[j+1:N].count(*x)

            if (2 * j - i <= N-1) and  ({S[j+j-i]} == x):
                ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=list(input())
a=0
for i in range(0,N-2):
    for j in range(i+1,N-1):
        if S[i]==S[j]:
            pass
        if S[i] !=S[j]:
            for k in range(j+1,N):
                if j-i==k-j:
                    pass
                if j-i!=k-j:
                    if S[i] !=S[j] and S[j]!=S[k] and S[k]!=S[i]:
                        a+=1
                else:
                    pass
print(a)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
count =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[j] != s[k] and s[k] != s[i]:
                if j - i != k - j:
                    count +=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import collections

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

CNT_S = collections.Counter(S)

cnt_r = CNT_S['R']
cnt_b = CNT_S['B']
cnt_g = CNT_S['G']

total = cnt_r * cnt_g * cnt_b
subdu = 0


for pos in range(N):
    for i in range(1, N):
    
        index_r = pos
        index_m = pos + i
        index_l = pos + 2 * i
        
        if (index_l < N and index_m < N and index_r < N):
            if (S[index_l] != S[index_m] and S[index_l] != S[index_r] and S[index_m] != S[index_r]):
                subdu += 1


ans = total - subdu

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!

# coding: utf-8
# Your code here!
# coding: utf-8
import sys
sys.setrecursionlimit(10000000)

#const
dxdy=((1,0),(0,1))
# my functions here!
def pin(type=int):
    return map(type,input().rstrip().split())

def resolve():  
    N=int(input())
    S=input()
    r,g,b=[],[],[]
    for i,s in enumerate(S):
        if s=="R":r.append(i)
        elif s=="B":b.append(i)
        else: g.append(i)
    #print("columnis",r,g,b)
    #RGBのリストが作れたので、このうち１個ずつとっていけば
    #<1>の条件を満たすことができる（ijkの順序が決まっていることから、
    #最後にこれらが<2>を満たすことを確認する？または、満たさないものを除く
    #<2> i<=j<=k　に対して　j-i = k-j でない
    #"この式を整理すると"k-j*2+i=0ではない:i+j+k=j*3ではない
    #逆にこの式を満たす（平均の関係になっているなら、jは必ず真ん中になる
    
    cnt=0
    for R in r:
        for G in g:
            s=2*R-G
            t=2*G-R
            flag1= (0<=s<=N+1)
            flag2= (0<=t<=N+1)
            flag3= ((R+G)%2==0)
            
            tx=0
            for B in b:
                conds=flag1+flag2+flag3
                #print(conds,(flag1,flag2,flag3))
                #print(B,t)
                if flag1 == 1:
                    if B==s:
                        flag1=0
                        tx+=1
                if flag2 == 1:
                    if B==t:
                        flag2=0
                        tx+=1
                if flag3 == 1:
                    if B==((R+G)//2):
                        flag3=0
                        tx+=1
                if conds==0 or B==b[-1]:
                    #print(t)
                    cnt+=tx
                    break
                    #print(R,G,B)
    print(len(r)*len(g)*len(b)-cnt)
        
"""         
#printデバッグ消した？
#前の問題の結果見てないのに次の問題に行くの？
"""
"""
お前カッコ閉じ,るの忘れてるだろ
"""
import sys
from io import StringIO
import unittest


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """4
RRGB"""
        output = """1"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB"""
        output = """1800"""
        self.assertIO(input, output)


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

Submission status: Time Limit Exceeded

Solution code:

n = int(input())
s = input()
s = list(s)
ans = 0

for i in range(0, n):
    for j in range(i+1, n):
        if s[i] == s[j]:
            continue
        for k in range(j+1, n):
            if j-i == k-j:
                continue
            if s[i] == s[k]:
                continue
            if s[j] == s[k]:
                continue
            ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
S = np.array(list(input()))
ans = 0
b = np.where(S=='B')[0]
for r_i in np.where(S == 'R')[0]:
    for g_i in np.where(S=='G')[0]:
        ans += len(b)
        min_ = min(r_i, g_i)
        max_ = max(r_i, g_i)
        if 2 * min_ - max_ in b:
            ans -= 1
        if 2 * max_ - min_ in b:
            ans -= 1
        if (r_i + g_i) / 2 in b:
            ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(input())
tot = 0
R = [i for i, x in enumerate(s) if x == 'R']
G = [i for i, x in enumerate(s) if x == 'G']
B = [i for i, x in enumerate(s) if x == 'B']
tot += len(R) * len(G) * len(B)
for i in range(len(R)):
    for j in range(len(G)):
        for k in range(len(B)):
            if 2 * R[i] == G[j] + B[k] or 2 * G[j] == R[i] + B[k] or 2 * B[k] == G[j] + R[i]:
                tot -= 1
print(tot)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n= int(input())
    slis = tuple(input())
    total = 0
    lis_R = [n for n, v in enumerate(slis) if v == "R"]
    lis_G = [n for n, v in enumerate(slis) if v == "G"]
    lis_B = [n for n, v in enumerate(slis) if v == "B"]

    lenR = len(lis_R)
    lenG = len(lis_G)
    lenB = len(lis_B)
    total += lenR * lenG * lenB

    for i in lis_R:
        for j in lis_G:
            for k in lis_B:
                if abs(i-j) == abs(j-k) or abs(i-k)==abs(j-k) or abs(k-i)==abs(j-i):
                    total -= 1
    print(total)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:

def main():
    N = int(input())
    S = input()
    count = 0

    for i in range(1, N-1):
        for j in range(i+1, N):
            if S[i-1] == S[j-1]:
                continue
            RGB_list = []
            RGB_list.append(S[i - 1])
            RGB_list.append(S[j - 1])
            for k in range(j+1, N+1):
                if k == 2 * j - i:
                    continue
                if not (S[k-1] in RGB_list):
                    count += 1

    print(count)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:

import itertools

N = int(input())
S = input()
ans = 0
i = 0
j = 0
k = 0

for v in itertools.combinations(range(N), 3):
    i = v[0]
    j = v[1]
    k = v[2]
    
    if (j - i) != (k - j):
        if S[i] == S[j] or S[j] == S[k] or S[k] == S[i]:
            continue
        else:
            ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

cnt = 0
for i in range(len(S)):
    for j in range(i, len(S)):
        if S[j] != S[i]:
            for k in range(j, len(S)):
                if S[i] != S[k] and S[k] != S[j] and (j-i) != (k-j):
                    cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

count = 0

for i in range(n):
    if s[i] == 'R':
        for j in range(i+1, n):
            if s[j] == 'G':
                d = j - i
                for k in range(j+1, n):
                    if s[k] == 'B' and k - j != d:
                        count += 1
            elif s[j] == 'B':
                d = j - i
                for k in range(j+1, n):
                    if s[k] == 'G' and k - j != d:
                        count += 1

for i in range(n):
    if s[i] == 'G':
        for j in range(i+1, n):
            if s[j] == 'R':
                d = j - i
                for k in range(j+1, n):
                    if s[k] == 'B' and k - j != d:
                        count += 1
            elif s[j] == 'B':
                d = j - i
                for k in range(j+1, n):
                    if s[k] == 'R' and k - j != d:
                        count += 1

for i in range(n):
    if s[i] == 'B':
        for j in range(i+1, n):
            if s[j] == 'G':
                d = j - i
                for k in range(j+1, n):
                    if s[k] == 'R' and k - j != d:
                        count += 1
            elif s[j] == 'R':
                d = j - i
                for k in range(j+1, n):
                    if s[k] == 'G' and k - j != d:
                        count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
S = '_' + S

rs = [0]
bs = [0]
gs = [0]

for s in S[-1::-1]:
    rs.append(rs[-1])
    bs.append(bs[-1])
    gs.append(gs[-1])
    if s == 'R':
        rs[-1] += 1
    if s == 'B':
        bs[-1] += 1
    if s == 'G':
        gs[-1] += 1

rs = [0] + rs[-1::-1]
bs = [0] + bs[-1::-1]
gs = [0] + gs[-1::-1]

s = 0
for i in range(1, N - 1):
    for j in range(i + 1, N):
        dif = j - i
        if j + dif > N:
            xxx = ''
        else:
            xxx = S[j + dif]
        if S[j] != S[i]:
            if 'G' != S[i] and 'G' != S[j]:
                s += gs[j + 1] - int(xxx == 'G')
            if 'B' != S[i] and 'B' != S[j]:
                s += bs[j + 1] - int(xxx == 'B')
            if 'R' != S[i] and 'R' != S[j]:
                s += rs[j + 1] - int(xxx == 'R')

print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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


ridxlist = []
gidxlist = []
bidxlist = []

for i in range(len(S)):
    if S[i] == 'R':
        ridxlist.append(i)
    if S[i] == 'G':
        gidxlist.append(i)
    if S[i] == 'B':
        bidxlist.append(i)

cnt = 0
for r in ridxlist:
    for g in gidxlist:
        for b in bidxlist:
            if abs(r - g) != abs(g - b) and abs(r - g) != abs(r - b) and abs(g - b) != abs(r - b):
                cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
s = [S[i] for i in range(N)]
R_list = []
G_list = []
B_list = []
for i in range(N):
    if s[i] == "R":
        R_list.append(i+1)
    elif s[i] == "G":
        G_list.append(i+1)
    elif s[i] == "B":
        B_list.append(i+1)
ans = 0
for i in range(len(R_list)):
    for j in range(len(G_list)):
        for k in range(len(B_list)):
            list = [R_list[i],G_list[j],B_list[k]]
            list.sort()
            if list[1] - list[0] != list[2] - list[1]:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

for k in range(n):
    for j in range(n):
        for i in range(n):
            if i < j < k and (j - i) != (k - j):
                a = (s[i], s[j], s[k])
                #print(a)
                a_set = set(a)
                #print(a_set)
                if len(a_set) == 3:
                    count += 1
            else:
                continue

print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
cnt = 0
for i in range(n-2):
    for d1 in range(1, n-1-i):
        for d2 in range(1-d1, n-i-2*d1):
            if d2 != 0:
                j = i + d1
                k = i + 2*d1 + d2
                if len(set([s[i],s[j],s[k]]))==3:
                    cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
import numpy as np

def solve(N: int, S: str):
    r = []
    g = []
    b = []
 
    for i, x in enumerate(S):
        if x ==  "R":r.append(i)
        elif x =="G":g.append(i)
        else:b.append(i)

    r = np.array(r)
    g = np.array(g)
    b = np.array(b)
  
    sb = set(b)
    ans = 0
    for r_ in r:
        # rg
        for g_ in g[g>r_]:
            # rgb
            if (g_ + (g_ - r_)) in sb:
                ans -= 1
            # brg
            if (r_ - (g_ - r_)) in sb:
                ans -= 1
            # rbg
            if ((r_ + g_)/2) in sb:
                ans -= 1
            ans += len(b)

        # gr
        for g_ in g[g<r_]:
            # grb
            if (r_ + (r_ - g_)) in sb:
                ans -= 1
            # bgr
            if (g_ - (r_ - g_)) in sb:
                ans -= 1
            # gbr
            if ((r_ + g_)/2) in sb:
                ans -= 1
            ans += len(b)
    print(ans)
  
    return
 
 
# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    S = next(tokens)  # type: str
    solve(N, S)
 
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

cnt=0
def index_multi(l, x):
    return [i for i, _x in enumerate(l) if _x == x]
r = index_multi(s, "R")
g = index_multi(s, "G")
b = index_multi(s, "B")

for i in r:
    for j in g:
        if j>i:
            for k in b:
                if k>j:
                    if j-i!=k-j:
                        if s[i]!=s[j] and s[i]!=s[k] and s[j]!=s[k]:
                            cnt += 1
for i in r:
    for j in b:
        if j>i:
            for k in g:
                if k>j:
                    if j-i!=k-j:
                        if s[i]!=s[j] and s[i]!=s[k] and s[j]!=s[k]:
                            cnt += 1
for i in g:
    for j in r:
        if j>i:
            for k in b:
                if k>j:
                    if j-i!=k-j:
                        if s[i]!=s[j] and s[i]!=s[k] and s[j]!=s[k]:
                            cnt += 1
for i in g:
    for j in b:
        if j>i:
            for k in r:
                if k>j:
                    if j-i!=k-j:
                        if s[i]!=s[j] and s[i]!=s[k] and s[j]!=s[k]:
                            cnt += 1
for i in b:
    for j in r:
        if j>i:
            for k in g:
                if k>j:
                    if j-i!=k-j:
                        if s[i]!=s[j] and s[i]!=s[k] and s[j]!=s[k]:
                            cnt += 1
for i in b:
    for j in g:
        if j>i:
            for k in r:
                if k>j:
                    if j-i!=k-j:
                        if s[i]!=s[j] and s[i]!=s[k] and s[j]!=s[k]:
                            cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
def my_index_multi(l, x):
    return [i for i, _x in enumerate(l) if _x == x]

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

R = my_index_multi(S, 'R')
G = my_index_multi(S, 'G')
B = my_index_multi(S, 'B')

ans = 0

for i in R:
    for j in G:
        for k in B:
            x = sorted([i, j, k])
            if x[2] - x[1] == x[1] - x[0]:
                continue
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import fractions

def in_ctrl():
    _input = [_.rstrip() for _ in sys.stdin.readlines()]
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    N = int(_input[0])
    S = _input[1]
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    return N,S


def out_ctrl(s):
    print(s)


if __name__ == "__main__":
    N,S = in_ctrl()
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    cnt = 0
    R = list(map(bool,list(map(int,list(S.replace('R','1').replace('G','0').replace('B','0'))))))
    G = list(map(bool,list(map(int,list(S.replace('R','0').replace('G','1').replace('B','0'))))))
    B = list(map(bool,list(map(int,list(S.replace('R','0').replace('G','0').replace('B','1'))))))
    for i,r in enumerate(R):
       if r:
           for j,g in enumerate(G[i+1:]):
               if g:
                    try:
                        if B[i+2*j+2]:
                            cnt += sum(B[i+j+2:])-1
                        else:
                            cnt += sum(B[i+j+2:])
                    except:
                        cnt += sum(B[i+j+2:])
           for j,g in enumerate(B[i+1:]):
               if g:
                    try:
                        if G[i+2*j+2]:
                            cnt += sum(G[i+j+2:])-1
                        else:
                            cnt += sum(G[i+j+2:])
                    except:
                        cnt += sum(G[i+j+2:])
    for i,r in enumerate(G):
       if r:
           for j,g in enumerate(R[i+1:]):
               if g:
                    try:
                        if B[i+2*j+2]:
                            cnt += sum(B[i+j+2:])-1
                        else:
                            cnt += sum(B[i+j+2:])
                    except:
                        cnt += sum(B[i+j+2:])
           for j,b in enumerate(B[i+1:]):
               if b:
                    try:
                        if R[i+2*j+2]:
                            cnt += sum(R[i+j+2:])-1
                        else:
                            cnt += sum(R[i+j+2:])
                    except:
                        cnt += sum(R[i+j+2:])
    for i,r in enumerate(B):
       if r:
           for j,g in enumerate(R[i+1:]):
               if g:
                    try:
                        if G[i+2*j+2]:
                            cnt += sum(G[i+j+2:])-1
                        else:
                            cnt += sum(G[i+j+2:])
                    except:
                        cnt += sum(G[i+j+2:])
           for j,b in enumerate(G[i+1:]):
               if b:
                    try:
                        if R[i+2*j+2]:
                            cnt += sum(R[i+j+2:])-1
                        else:
                            cnt += sum(R[i+j+2:])
                    except:
                        cnt += sum(R[i+j+2:])
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    out_ctrl(cnt)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
n = int(input())
s = input()
ans = 0
for i in range(0, n - 2):
    for j in range(i + 1, n - 1):
        if s[i] == s[j]:
            continue
        for k in range(j + 1, n):
            if s[i] != s[j] and s[j] != s[k] and s[k] != s[i] and j - i != k - j:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

check = dict()
def get_pattern2(i, j):
    a = s[i]
    b = s[j]
    total = 0
    check_str = ""
    if a == "R" or b == "R":
        check_str += "R"
    if a == "G" or b == "G":
        check_str += "G"
    if a == "B" or b == "B":
        check_str += "B"

    for k in range(j+1, n):
        if check_str+str(k) in check:
            total += check[check_str+str(k)]
            break
        if a != s[k] and b != s[k]:
            total += 1
    check[check_str+str(j+1)] = total
    return total

def get_pattern(i):
    a = s[i]
    total = 0
    for j in range(i+1, n):
        if a+str(j) in check:
            total += check[a+str(j)]
            break
        if a != s[j]:
            total += get_pattern2(i, j)
    check[a+str(i+1)] = total

    # k-j = i-jなものをぬく
    for j in range(i+1, n):
        k = j + j - i
        if k >= n:
            break
        if a != s[k] and s[j] != s[k] and a != s[j]:
            total -= 1

    return total

total = 0
for i in range(n-1, -1, -1):
    total += get_pattern(i)
print(total)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
from itertools import combinations
N=int(input())
S=list(input())
Pattern=[x for x in combinations(range(N),3) if not (len(set(S[y] for y in x))!=3 or x[2]-x[1] == x[1]-x[0])]
print(len(Pattern))

Submission status: Time Limit Exceeded

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 (0,len(r2)):
    for j in range (0,len(g2)):
        for k in range (0,len(b2)):
            lis2 =[r2[i], g2[j], b2[k]]
            lis= sorted(lis2)
            if lis[1]-lis[0] == lis[2]-lis[1]:
                ans -=1
                #print(r2[i],g2[j],b2[k])
print(ans)

Submission status: Time Limit Exceeded

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

s_r = []
s_g = []
s_b = []

for i in range(n):
  if s[i] == "R":
    s_r.append(i)
  elif s[i] == "G":
    s_g.append(i)
  else:
    s_b.append(i)

ans = 0
    
def conditions(a, b):
  if (a+b) % 2 == 0:
    x = (a+b) / 2
    y = 2*b - a
    z = 2*a - b
  else:
    x = -1
    y = 2*b - a
    z = 2*a - b
  return (x, y, z)
  
for item in s_r:
  for item2 in s_g:
    count = 0
    for i in conditions(item, item2):
      if i in s_b:
        count += 1
    ans += len(s_b) - count

print(ans)