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 itertools

N = int(input())
S = list(input())
Rs = [i for i, v in enumerate(S) if v == 'R']
Bs = [i for i, v in enumerate(S) if v == 'B']
Gs = [i for i, v in enumerate(S) if v == 'G']
combinations = []
for r in Rs:
    for b in Bs:
        for g in Gs:
            combinations.append(sorted([r, b, g]))
ans = [1 for comb in combinations if (S[comb[0]] != S[comb[1]] and S[comb[0]] != S[comb[2]] and S[comb[1]] != S[comb[2]]) and (comb[1] - comb[0] != comb[2] - comb[1])]
print(sum(ans))

Submission status: Time Limit Exceeded

Solution code:
import copy
n = int(input())
s = input()
ans = 0
for i in range(n-2):
    RGB = ['R', 'G', 'B']
    RGB.remove(s[i])
    Rc = s[i+1:].count('R')
    Gc = s[i+1:].count('G')
    Bc = s[i+1:].count('B')
    # print(RGB, Rc, Gc, Bc)
    for j in range(i+1, n-1):
        RGBcopy = copy.copy(RGB)
        if not s[j] in RGB:
            if s[j] == 'R':
                Rc = max(0, Rc-1)
            elif s[j] == 'G':
                Gc = max(0, Gc-1)
            else:
                Bc = max(0, Bc-1)
            continue
        
        if s[j] == 'R':
            Rc = max(0, Rc-1)
            RGBcopy.remove('R')
            if RGBcopy[0] == 'G':
                cnt = Gc
                if 2*j-i <= n-1:
                    if s[2*j-i] == 'G':
                        cnt = max(0, cnt-1)
            else:
                cnt = Bc
                if 2*j-i <= n-1:
                    if s[2*j-i] == 'B':
                        cnt = max(0, cnt-1)

        elif s[j] == 'G':
            Gc = max(0, Gc-1)
            RGBcopy.remove('G')
            if RGBcopy[0] == 'R':
                cnt = Rc
                if 2*j-i <= n-1:
                    if s[2*j-i] == 'R':
                        cnt = max(0, cnt-1)
            else:
                cnt = Bc
                if 2*j-i <= n-1:
                    if s[2*j-i] == 'B':
                        cnt = max(0, cnt-1)

        else:
            Bc = max(0, Bc-1)
            RGBcopy.remove('B')
            if RGBcopy[0] == 'G':
                cnt = Gc
                if 2*j-i <= n-1:
                    if s[2*j-i] == 'G':
                        cnt = max(0, cnt-1)
            else:
                cnt = Rc
                if 2*j-i <= n-1:
                    if s[2*j-i] == 'R':
                        cnt = max(0, cnt-1)
        # print(i, j, cnt, Rc, Gc, Bc)
        ans += cnt
        # print(RGBcopy)

print(ans)

Submission status: Time Limit Exceeded

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

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

# リストを作成
for i in range(0,N):
  if S[i]=="R":
    R += [i]
  elif S[i]=="G":
    G += [i]
  else:
    B += [i]

def search(ind, col):
  n=0
  useable_col = ["R","G","B"]
  useable_col.remove(col)
  for i,j in [[0,1],[1,0]]:
    l_col=useable_col[i]
    r_col=useable_col[j]
    if l_col=="R":
      l_col=R
    elif l_col=="G":
      l_col=G
    else:
      l_col=B
    if r_col=="R":
      r_col=R
    elif r_col=="G":
      r_col=G
    else:
      r_col=B
        
    l_useable = [ii for ii in l_col if ii<ind]
    r_useable = [ii for ii in r_col if ii>ind]
    if (l_useable == []) or (r_useable == []):
      continue

    # print(l_useable)
    # print(r_useable)
    l = min(ind, N-1-ind)
    for m in range(1,l+1):
      if ind-m in l_useable and ind+m in r_useable:
        n -= 1
    n += len(l_useable) * len(r_useable)
    # print(len(l_useable))
    # print(len(r_useable))
  return n

n=0
for pivot in range(1,N-1):
  if S[pivot] =="R":
    n += search(pivot, "R")
  elif S[pivot] =="G":
    n += search(pivot, "G")
  else:
    n += search(pivot, "B")

print(n)

Submission status: Time Limit Exceeded

Solution code:
b=input()
a=input()

r_list=[]
g_list=[]
b_list=[]
count=0
for k,i in enumerate(a):
  if i=='R' :
    r_list.append(k)
  elif i=='B' :
    b_list.append(k)
  else :
    g_list.append(k)

for i in r_list :
  for j in b_list:
    for k in g_list :
      if i+j!=2*k and  k+j!=2*i and k+i!=2*j :
        count+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
from bisect import bisect
from sys import stdin


def main():
    input = lambda: stdin.readline()[:-1]
    N = int(input())
    S = input()

    rgb = defaultdict(list)
    for i, s in enumerate(S):
        rgb[s].append(i)

    ans = 0
    for i, s in enumerate(S):
        if s == 'R':
            x = bisect(rgb['G'], i)
            for gi in rgb['G'][x:]:
                d = gi - i
                y = bisect(rgb['B'], gi)
                for bi in rgb['B'][y:]:
                    if d != bi - gi:
                        ans += 1
            x = bisect(rgb['B'], i)
            for bi in rgb['B'][x:]:
                d = bi - i
                y = bisect(rgb['G'], bi)
                for gi in rgb['G'][y:]:
                    if d != gi - bi:
                        ans += 1
        if s == 'G':
            x = bisect(rgb['R'], i)
            for ri in rgb['R'][x:]:
                d = ri - i
                y = bisect(rgb['B'], ri)
                for bi in rgb['B'][y:]:
                    if d != bi - ri:
                        ans += 1
            x = bisect(rgb['B'], i)
            for bi in rgb['B'][x:]:
                d = bi - i
                y = bisect(rgb['R'], bi)
                for ri in rgb['R'][y:]:
                    if d != ri - bi:
                        ans += 1
        if s == 'B':
            x = bisect(rgb['G'], i)
            for gi in rgb['G'][x:]:
                d = gi - i
                y = bisect(rgb['R'], gi)
                for ri in rgb['R'][y:]:
                    if d != ri - gi:
                        ans += 1
            x = bisect(rgb['R'], i)
            for ri in rgb['R'][x:]:
                d = ri - i
                y = bisect(rgb['G'], ri)
                for gi in rgb['G'][y:]:
                    if d != gi - ri:
                        ans += 1
    print(ans)


main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
import numpy as np
ans = 0
for i in range(0, N):
    for j in range(i, N):
        if i ==j or S[i] == S[j]:
            continue
        s = ['R', 'G', 'B']
        s.remove(S[i])
        s.remove(S[j])
        k_s = np.where(np.array(list(S[j:])) == s[0])[0] + j
        for k in k_s:
            if j == k or S[j] == S[k]:
                continue
            elif j-i != k-j and S[i] != S[k]:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline

N = int(input())
S = input().strip()

dic = {}
ans = 0

for i in range(N-2):
    dic['R'] = S[i+1:].count('R')
    dic['G'] = S[i+1:].count('G')
    dic['B'] = S[i+1:].count('B')
    for j in range(i+1, N-1):
        dic[S[j]] -= 1
        arr = ['R', 'G', 'B']
        if S[i] != S[j]:
            arr.remove(S[i])
            arr.remove(S[j])
            ans += dic[arr[0]]
            if j+j-i <= N-1 and S[j+j-i] == arr[0]:
                ans -= 1

print(ans)

Submission status: Time Limit Exceeded

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

ret = 0
R = list()
G = list()
B = list()
for i in range(len(S)):
    if S[i] == 'R':
        R.append(i)
    elif S[i] == 'G':
        G.append(i)
    elif S[i] == 'B':
        B.append(i)
l = [R,G,B]

for r in range(len(R)):
    for g in range(len(G)):
        for b in range(len(B)):
            l = [R[r], G[g], B[b]]
            l.sort()
            if l[2] - l[1] != l[1] - l[0]:
                ret += 1
print(ret)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()[:n+1]

index_a = [n for n, v in enumerate(s) if v == 'R']
index_b = [n for n, v in enumerate(s) if v == 'G']
index_c = [n for n, v in enumerate(s) if v == 'B']

indexes = [index_a, index_b, index_c]

res_set = set()

def get_count(_a, _b, _c):
  for ii_a, i_a in enumerate(indexes[_a]):
    for ii_b, i_b in enumerate(indexes[_b]):
      if i_a > i_b:
        continue

      for ii_c, i_c in enumerate(indexes[_c]):
        if i_b > i_c:
          continue

        if i_c - i_b != i_b - i_a:
          t = [i_a, i_b, i_c]
          res_set.add(tuple([t[_a], t[_b], t[_c]]))

get_count(0, 1, 2)
get_count(0, 2, 1)
get_count(1, 0, 2)
get_count(1, 2, 0)
get_count(2, 0, 1)
get_count(2, 1, 0)

print(len(res_set))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = str(input())
cnt = 0
for k in range(N):
    for j in range(N):
        for i in range(N):
            if i < j < k and S[i] != S[j] and S[j] != S[k] and S[k] != S[i] and (j - i) != (k - j):
                cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
#N = 39
#S = "RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB"
N = int(input())
S = input()
result = 0
a = 1
while (a <= N -3):
    b = a + 1
    while (b <= N - 1):
        c = b + 1
        while (c <= N):
            if ((b - a) == (c - b)):
                c += 1
                continue
            if (S[a - 1] != S[b - 1] and S[a - 1] != S[c - 1] and S[b - 1] != S[c - 1]):
                result += 1
            c += 1
        b += 1
    a += 1
print(result)

Submission status: Time Limit Exceeded

Solution code:
def main():
  N = int(input())
  S = input()
  
  cnt_R = [0]*(N+1)
  cnt_G = [0]*(N+1)
  cnt_B = [0]*(N+1)
  
  for i in range(1,N+1):
    if S[i-1] == 'R':
      cnt_R[i] = cnt_R[i-1] + 1
    else:
      cnt_R[i] = cnt_R[i-1]
    if S[i-1] == 'G':
      cnt_G[i] = cnt_G[i-1] + 1
    else:
      cnt_G[i] = cnt_G[i-1]
    if S[i-1] == 'B':
      cnt_B[i] = cnt_B[i-1] + 1
    else:
      cnt_B[i] = cnt_B[i-1]
  
  
  ans = 0
  for i in range(N-2):
    for j in range(i+1,N-1):
      if S[i] != S[j]:
        interval = j-i
        if S[i] == 'R' and S[j] == 'G' or S[i] == 'G' and S[j] == 'R':
          cnt = cnt_B[N] - cnt_B[j]
          if j+interval < N and S[j+interval] == 'B':
            cnt -= 1
          ans += cnt
        if S[i] == 'R' and S[j] == 'B' or S[i] == 'B' and S[j] == 'R':
          cnt = cnt_G[N] - cnt_G[j]
          if j+interval < N and S[j+interval] == 'G':
            cnt -= 1
          ans += cnt
        if S[i] == 'G' and S[j] == 'B' or S[i] == 'B' and S[j] == 'G':
          cnt = cnt_R[N] - cnt_R[j]
          if j+interval < N and S[j+interval] == 'R':
            cnt -= 1
          ans += cnt

  print(ans)





if __name__ == "__main__":
  main()

Submission status: Time Limit Exceeded

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

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

for i in range(N):
    if S[i]=="R":
        R.append(int(i))
    elif S[i]=="G":
        G.append(int(i))
    else:
        B.append(int(i))
 
ans = 0

for i in range(len(R)):
    for j in range(len(G)):
        for k in range(len(B)):
            if 3*(max(R[i],G[j],B[k])+min(R[i],G[j],B[k]))==2*(R[i]+G[j]+B[k]):
                continue
            else:
                ans+=1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=str(input())
S=list(S)
a=0
if 1<=N<=4000:
    if N<=3:
        print(0)
    else:
        i=0
        j=1
        k=2
        for k in range(2,N):
            for j in range(1,k):
                for i in range(0,j):
                    if (S[i]==S[j])or(S[j]==S[k])or(S[i]==S[k]):
                        continue
                    else:
                        if j-i==k-j:
                            continue
                        else:
                            a+=1
                    i+=1
                j+=1
            k+=1
        print(a)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
X = [0]*3
for s in S:
  if s == "R":
    X[0] += 1
  elif s == "B":
    X[1] += 1
  else:
    X[2] += 1
total = X[0]*X[1]*X[2]
flag = [False] * 3
for i in range(N-2):
  for j in range(i+1, N-1):
    if S[i] == "R":
      flag[0] = True
    elif S[i] == "B":
      flag[1] = True
    else:
      flag[2] = True
    if S[j] == "R":
      flag[0] = True
    elif S[j] == "B":
      flag[1] = True
    else:
      flag[2] = True
    k = j + j - i
    if k <= N-1:
      if S[k] == "R":
        flag[0] = True
      elif S[k] == "B":
        flag[1] = True
      else:
        flag[2] = True
    #print(total, i,j,k, flag)
    if all(flag):
      total -= 1
    flag = [False]*3
print(total)

Submission status: Time Limit Exceeded

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

for i in range(1,N+1):
    for j in range(1,N+1):
        for k in range(1,N+1):
            if (S[i-1]!=S[j-1]) and (S[j-1]!=S[k-1]) and(S[k-1]!=S[i-1]) and ((j+j)!=(i+k)) and (i<j<k):
                sum+=1
            else:
                continue
print(sum)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
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)
    
count = 0

for r in R:
  for g in G:
    for b in B:
      S = sorted([r,g,b])
      if S[0]+S[2]!=2*S[1]:
        count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
S = input()

index = defaultdict(list)

for i, s in enumerate(S):
    if s == "R":
        index["R"].append(i)
    elif s == "G":
        index["G"].append(i)
    else:
        index["B"].append(i)

G = sorted(index["G"])
R = sorted(index["R"])
B = sorted(index["B"])

count = 0
for r in R:
    for g in G:
        for b in B:
            l = sorted([r, g, b])
            if l[1]-l[0] != l[2]-l[1]:
                count += 1

            # s = min(r, g, b)
            # l = max(r, g, b)
            # if r != s and r != l:
            #     m = r
            # if g != s and g != l:
            #     m = g
            # if b != s and b != l:
            #     m = b
            # if m - s != l - m:
            #     count += 1
            

print(count)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S = input()
sum=0
for i in range(0, N - 2):
    a = S[i]
    if a == 'R':
        index=S.find('B',i+1)
        while(index!=-1):
            j=index
            sum += S[j + 1 :].count('G')
            if 2*j-i<N and(S[2 * j - i] == 'G'):
                sum -= 1
            if j==N-1:break
            index=S.find('B',j+1)
        index = S.find('G', i+ 1)
        while(index!=-1):
            j=index
            sum += S[j + 1 :].count('B')
            if 2*j-i<N and(S[2 * j - i] == 'B'):
                sum-=1
            if j==N-1:break
            index = S.find('G', j+ 1)
    elif a == 'G':
        index=S.find('B',i+1)
        while(index!=-1):
            j=index
            sum += S[j + 1 :].count('R')
            if 2*j-i<N and(S[2 * j - i] == 'R'):
                sum-=1
            if j==N-1:break
            index=S.find('B',j+1)
        index=S.find('R',i+1)
        while(index!=-1):
            j=index
            sum += S[j + 1 :].count('B')
            if 2*j-i<N and(S[2 * j - i] == 'B'):
                sum-=1
            if j==N-1:break
            index=S.find('R',j+1)
    else:
        index=S.find('R',i+1)
        while(index!=-1):
            j=index
            sum += S[j + 1 :].count('G')
            if 2*j-i<N and(S[2 * j - i] == 'G'):
                sum-=1
            if j==N-1:break
            index=S.find('R',j+1)
        index=S.find('G',i+1)
        while(index!=-1):
            j=index
            sum += S[j + 1 :].count('R')
            if 2*j-i<N and(S[2 * j - i] == 'R'):
                sum-=1
            if j==N-1:break
            index=S.find('G',j+1)
        #    if a!=S[j]:
        #        b=S[j]
        #        if a=='R' and b == 'G' or a=='G' and b=='R':
        #            sum += S[j + 1 :].count('B')
        #            if 2*j-i<N and(S[2 * j - i] == 'B'):
        #                sum-=1
        #        elif a == 'G' and b == 'B' or a == 'B' and b == 'G':
        #            sum += S[j + 1 :].count('R')
        #            if 2*j-i<N and(S[2 * j - i] == 'R'):
        #                sum-=1
        #        else:
        #            sum += S[j + 1 :].count('G')
        #            if 2*j-i<N and(S[2 * j - i] == 'G'):
        #                sum-=1
print(sum)

Submission status: Time Limit Exceeded

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

def restword(a,b):
    w=[a,b]
    if('R' not in w):
        return 'R'
    elif('G' not in w):
        return 'G'
    else:
        return 'B'

ans=0
for i in range(n):
    for j in range(i+1,n):
        if(s[i]!=s[j]):
            sk=s[j+1:]
            rest_k=restword(s[i], s[j])
            ans_k=sk.count(rest_k)
            if(j+j-i < n and s[j+j-i]==rest_k):
                ans_k-=1
            ans+=ans_k
        else:
            continue

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(input())
r_pos = []
g_pos = []
b_pos = []

for i in range(n):
    st = s[i]
    if st == "R":
        r_pos.append(i)

    if st == "G":
        g_pos.append(i)

    if st == "B":
        b_pos.append(i)

r_num = len(r_pos)
g_num = len(g_pos)
b_num = len(b_pos)


ans = r_num * g_num * b_num
for r in r_pos:
    for g in g_pos:
        x, y = min(r, g), max(r, g)

        dif = y - x
        left_pos = x - dif
        right_pos = y + dif
        mid_pos = 4000
        if dif % 2 == 0:
            mid_pos = x + dif // 2

        left = 0
        right = 0
        mid = 0
        if left_pos in b_pos:
            left = 1
        if right_pos in b_pos:
            right = 1
        if mid_pos in b_pos:
            mid = 1

        ans -= left + right + mid

print(ans)

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
a=0
for i in range(n):
    for j in range(i,n):
        x='RGB'
        if s[i]!=s[j]:
            x_= x.replace(s[i],'').replace(s[j],'')
            a+=s[j:].count(x_)

            if 2*(j+1)-i-1<n+1:
                if s[2*(j+1)-i-1-1]==x_:
                    a-=1

print(a)

Submission status: Time Limit Exceeded

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

ls = []
for s in S:
    ls += list(s)

ans = 0
for i in range(N-2):
    for j in range(i, N-1):
        for k in range(j, N):
            if ls[i] == ls[j]:
                continue
            elif ls[j] == ls[k]:
                continue
            elif ls[i] == ls[k]:
                continue
            else:
                if k-j == j-i:
                    continue
                else:
                    ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from itertools import product

N = int(input())
S = np.array(list(input()))



R = []
G = []
B = []
for i, s in enumerate(S):
    if s == "R":
        R.append(i)
    elif s == "G":
        G.append(i)
    else:
        B.append(i)

i = 0      
for p in product(R,G,B):
    if len(set(np.diff(sorted(p)))) == 1:
        continue
    if len(set([S[p[0]], S[p[1]], S[p[2]]])) != 3:
        continue
    i += 1
print(i)

Submission status: Time Limit Exceeded

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

r_list = [i for i, x in enumerate(s) if x == 'R']
g_list = [i for i, x in enumerate(s) if x == 'G']
b_list = [i for i, x in enumerate(s) if x == 'B']

ans = 0
for i in r_list:
    for j in g_list:
        ans += len(b_list)
        if (i + j) / 2 in b_list:
            ans -= 1
        if 2 * i - j in b_list:
            ans -= 1
        if 2 * j - i in b_list:
            ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(map(str, input()))
f=0
i_c = j_c = k_c =1
for i in range(N):
    for j in range(N):
        for k in range(N):
            if(k_c>j_c and j_c>i_c):
                if(j_c-i_c!=k_c-j_c):
                    if(
                        S[i]!=S[j] and 
                        S[i]!=S[k] and
                        S[j]!=S[k]):
                        # print("!!",i,j,k)
                        f+=1
                        # print(i_c,j_c,k_c)
            k_c+=1
        j_c+=1
        k_c = 1
    i_c+=1
    j_c = 1

print(f)

Submission status: Time Limit Exceeded

Solution code:
#D
n = int(input())
s = input()
s_l = list(s)
a = [0 for _ in range(n)]
for i in range(n):
    if s[i] =='R':
        a[i] = 0
    elif s[i] == 'G':
        a[i] = 1
    else:
        a[i] = 2
        

cnt = 0
for i in range(n-2):
    for j in range(i,n-1):
        if a[i] != a[j]:
            for k in range(j,n):
                if a[i] != a[k] and a[j] != a[k] and j-i != k-j:
                    cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]

from math import comb
# def comb(n,r):
#     if r==2:
#         return n*(n-1)//2
#     else:
#         return n*(n-1)*(n-2)//6
n=n0()
s=s0()
r=s.count("R")
g=s.count("G")
b=s.count("B")
ans=0
for i in range(1,(n-1)//2+1):
    for j in range(0,n-2*i):
        ans+=1
#         print(j,j+1,j+i*2)
        if s[j]==s[j+i]==s[j+2*i]:
            ans-=1
        elif s[j]==s[j+i] or s[j]==s[j+2*i] or s[j+i]==s[j+2*i]:
            ans-=1
            
ans+=comb(r,3)+comb(g,3)+comb(b,3)+comb(r,2)*(g+b)+comb(g,2)*(r+b)+comb(b,2)*(r+g)
print(comb(n,3)-ans)

Submission status: Time Limit Exceeded

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

r = []
g = []
b = []
for n, c in enumerate(s):
    if c == "R":
        r.append(n)
    elif c == "G":
        g.append(n)
    elif c == "B":
        b.append(n)
for x in r:
    for y in g:
        for z in b:
            ary = sorted([x,y,z])
            if (ary[2]-ary[1] != ary[1] - ary[0]):
                t += 1

print(t)

Submission status: Time Limit Exceeded

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

acc_r, acc_g, acc_b = [], [], []
cr, cg, cb = 0, 0, 0
for i in s:
    if i == "R":
        cr += 1
    elif i == "G":
        cg += 1
    elif i == "B":
        cb += 1

    acc_r.append(cr)
    acc_g.append(cg)
    acc_b.append(cb)

count = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        si = s[i]
        sj = s[j]
        if (si == "R" and sj == "G") or (si == "G" and sj == "R"):
            count += acc_b[-1] - acc_b[j]
            if j+(j-i) < n and s[j+(j-i)] == "B":
                count -= 1
        if (si == "G" and sj == "B") or (si == "B" and sj == "G"):
            count += acc_r[-1] - acc_r[j]
            if j+(j-i) < n and s[j+(j-i)] == "R":
                count -= 1
        if (si == "R" and sj == "B") or (si == "B" and sj == "R"):
            count += acc_g[-1] - acc_g[j]
            if j+(j-i) < n and s[j+(j-i)] == "G":
                count -= 1
                
print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
k=[i for i in range(0,n)]
from itertools import combinations
c=list(combinations(k,3))
ans=0
for i in range(len(c)):
  p=c[i][0]
  q=c[i][1]
  r=c[i][2]
  if s[p]!=s[q] and s[q]!=s[r] and s[p]!=s[r] and q-p!=r-q:
    ans=ans+1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
r = []
g = []
b = []
for i in range(len(S)):
    if S[i] == 'R':
        r.append(i)
    elif S[i] == 'G':
        g.append(i)
    else:
        b.append(i)
lr = len(r)
lg = len(g)
lb = len(b)
cnt = 0
for i in range(lr):
    for j in range(lg):
        for k in range(lb):
            l, m, n = r[i], g[j], b[k]
            if 2*(l+m+n) != 3*(max(l,m,n)+min(l,m,n)):
                cnt += 1
print(cnt)

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')
            elif 'B' != S[i] and 'B' != S[j]:
                s += bs[j + 1] - int(xxx == 'B')
            elif '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()
Rindexes = [i for i, x in enumerate(S) if x == 'R']
Gindexes = [i for i, x in enumerate(S) if x == 'G']
Bindexes = [i for i, x in enumerate(S) if x == 'B']
count = 0
for i in range(N-2):
  for j in range(i+1, N-1):
    if S[i] != S[j]:
      diff_ji = j - i
      if S[i] != 'R' and S[j] != 'R':
        count += len(list(filter(lambda k: j < k and j - i != k - j, Rindexes)))
      elif S[i] != 'G' and S[j] != 'G':
        count += len(list(filter(lambda k: j < k and j - i != k - j, Gindexes)))
      elif S[i] != 'B' and S[j] != 'B':
        count += len(list(filter(lambda k: j < k and j - i != k - j, Bindexes)))
print(count)

Submission status: Time Limit Exceeded

Solution code:
import bisect
N = int(input())  # N<= 4000 6*10^10なので、削減しないとだめ
S = list(input())


# Si Sj Skがどれも別のもの。ただしi<j<k
# また、j-i != k-j
# 1,2,3はだめ1,2,4はok
# 1,3,5もだめ
# この組の数を求める

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


indexes = {'R': [], "G": [], "B": []}
for i, char in enumerate(S):
    indexes[char].append(i + 1)
# print(permutation_hash)
count = 0
R_indexes_count = len(indexes['R'])
G_indexes_count = len(indexes['G'])
B_indexes_count = len(indexes['B'])
indexes_count = {'R': R_indexes_count,
                 'G': G_indexes_count, 'B': B_indexes_count}
# 短いやつを使うとかする？,
if R_indexes_count <= G_indexes_count and R_indexes_count <= B_indexes_count:
    min_type = 'R'
    if G_indexes_count <= B_indexes_count:
        second_min_type = 'G'
        last_type = 'B'
    else:
        second_min_type = 'B'
        last_type = 'G'
elif G_indexes_count <= R_indexes_count and G_indexes_count <= B_indexes_count:
    min_type = 'G'
    if R_indexes_count <= B_indexes_count:
        second_min_type = 'R'
        last_type = 'B'
    else:
        second_min_type = 'B'
        last_type = 'R'
else:
    min_type = 'B'
    if G_indexes_count <= R_indexes_count:
        second_min_type = 'G'
        last_type = 'R'
    else:
        second_min_type = 'R'
        last_type = 'G'

# print(min_type, second_min_type, last_type)
for R_index in indexes[min_type]:
    for G_index in indexes[second_min_type]:
        middle_index = (R_index + G_index) / 2
        remove_conut = 0
        if middle_index != R_index and middle_index != G_index:
            if middle_index in indexes[last_type]:
                remove_conut += 1
        div = abs(G_index - R_index)
        if G_index < R_index:
            if G_index - div in indexes[last_type]:
                remove_conut += 1
            if R_index + div in indexes[last_type]:
                remove_conut += 1
        else:
            if G_index + div in indexes[last_type]:
                remove_conut += 1
            if R_index - div in indexes[last_type]:
                remove_conut += 1
        count += (indexes_count[last_type] - remove_conut)

print(count)

Submission status: Time Limit Exceeded

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 = 0
if(N<=3):
  print(0)
else:
  for i in range(N):
    if(len(R)==0 or len(G)==0 or len(B)==0):
      break
    else:
      if(S[i]=="R"):
        R.pop(0)
        for j in range(len(G)):
          for k in range(len(B)):
            if((G[j]<B[k] and G[j]-i!=B[k]-G[j])or(B[k]<G[j] and B[k]-i!=G[j]-B[k])):
              ans += 1
      elif(S[i]=="G"):
        G.pop(0)
        for j in range(len(R)):
          for k in range(len(B)):
            if((R[j]<B[k] and R[j]-i!=B[k]-R[j])or(B[k]<R[j] and B[k]-i!=R[j]-B[k])):
              ans += 1
      else:
        B.pop(0)
        for j in range(len(R)):
          for k in range(len(G)):
            if((R[j]<G[k] and R[j]-i!=G[k]-R[j])or(G[k]<R[j] and G[k]-i!=R[j]-G[k])):
              ans += 1
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
N = int(input())
S = 'D' + input()


rgb_count = [[0 for i in range(3)] for j in range(N+1)]
for i in range(1, N+1):
    if S[i] == 'R':
        rgb_count[1][0] += 1
    elif S[i] == 'G':
        rgb_count[1][1] += 1
    else:
        rgb_count[1][2] += 1


dict={'R':0, 'G':1, 'B':2}
d = ['', 'B', 'G', 'R']
dic = {'R': [1,2], 'G': [0,2], 'B': [0,1]}
ret = rgb_count[1][0]*rgb_count[1][1]*rgb_count[1][2]
for i in range(2,N):
    ran = min([i-1, N-i])
    if ran < 1: continue
    for k in range(1, ran+1):
        if set([S[i-k], S[i], S[i+k]]) == set(['G', 'B', 'R']):
            ret -= 1

print(ret)