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:
n = int(input())
s = str(input())

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

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

Submission status: Time Limit Exceeded

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
import numpy as np

N = INT()
S = list(input())

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


A = list(range(len(S)))
cnt = 0
for x, y in combinations(A, 2):
	if (x+y)//2 == (x+y)/2:
		z = (x+y)//2
		if S[x] != S[y] and S[x] != S[z] and S[y] != S[z]:
			cnt += 1

print(r*g*b-cnt)

Submission status: Time Limit Exceeded

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

r = 0
g = 0
b = 0

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

ans = r*g*b

for i in range(0, 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] and j - i == k - j:
                ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
import copy
from queue import Queue

n = int(input())
#n, k = [int(i) for i in input().split()]
#table = [[int(i) for i in input().split()] for j in range(n)]
#table = [list(input()) for n in range(h)]
#s = [int(i) for i in input().split()]
#s = [int(i) for i in range(n)]
#s = [int(input()) for i in range(n)]
s = input()
list = list(s)
# dp = [[0 for _ in range(2)] for _ in range(13)]

#a = [[0 for _ in range(n+1)] for _ in range(n+1)]
#b = [[0 for _ in range(n+1)] for _ in range(n+1)]

kotae = 0

for i in range(n-3) :
    for j in range(i+1,n-1) :
        for k in range(j+1,n) :
            if((list[i] != list[j])and((list[i] != list[k])and((list[j] != list[k])and((j-i) != (k - j))))) :
                kotae += 1

print(kotae)

Submission status: Time Limit Exceeded

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

for i in range(N-3):
  for j in range(i+1, N-1):
    if S[i] != S[j]:
      for k in range(j+1, N):
       if 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 = list(input())
ans = 0

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

c = 0
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 j - i == k - j:
                    continue
                if s[i] != s[k] and s[j] != s[k]:
                    c += 1
print(c)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
rr = 0
gg = 0
bb = 0
for i in range(n):
    if s[i] == "R":
        rr += 1
    if s[i] == "G":
        gg += 1
    if s[i] == "B":
        bb += 1

ans = rr * gg * bb

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

Submission status: Time Limit Exceeded

Solution code:
def fun(a, b, c):
    global ans
    for i in range(len(a)):
        for j in range(len(b)):
            if (a[i] > b[j]):
                continue
            for k in range(len(c)):
                if (b[j] > c[k]):
                    continue
                if b[j] - a[i] != c[k] - b[j]:
                    ans += 1


n = int(input())
s = input()
r = [0] * n
g = [0] * n
b = [0] * n
rr = 0
gg = 0
bb = 0
ans = 0
for i in range(n):
    if s[i] == "R":
        r[rr] = i
        rr += 1
    if s[i] == "G":
        g[gg] = i
        gg += 1
    if s[i] == "B":
        b[bb] = i
        bb += 1

r = r[:rr]
g = g[:gg]
b = b[:bb]

fun(r, g, b)
fun(r, b, g)
fun(g, r, b)
fun(g, b, r)
fun(b, r, g)
fun(b, g, r)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def fun(a, b, c):
    global ans
    for i in range(len(a)):
        for j in range(len(b)):
            for k in range(len(c)):
                if a[i] < b[j] and b[j] < c[k] and b[j] - a[i] != c[k] - b[j]:
                    ans += 1


n = int(input())
s = input()
r = [0] * n
g = [0] * n
b = [0] * n
rr = 0
gg = 0
bb = 0
ans = 0
for i in range(n):
    if s[i] == "R":
        r[rr] = i
        rr += 1
    if s[i] == "G":
        g[gg] = i
        gg += 1
    if s[i] == "B":
        b[bb] = i
        bb += 1

r = r[:rr]
g = g[:gg]
b = b[:bb]

fun(r, g, b)
fun(r, b, g)
fun(g, r, b)
fun(g, b, r)
fun(b, r, g)
fun(b, g, r)

print(ans)

Submission status: Time Limit Exceeded

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

st_list = ['R', 'G', 'B']
ans = 0
for i, i_s in enumerate(s):
    first = [i, i_s]
    s_2 = s[i+1:]
    
    for j, j_s in enumerate(s_2):
        if (first[1]!=j_s):
            second = [j, j_s]
            s_3 = s_2[j+1:]
        else:
            continue

        for st_ in st_list:
            if (st_ != first[1] and st_ != second[1]):
                search_w = st_
	
        ans += s_3.count(search_w)
        if len(s_3) > second[0]:
            if s_3[second[0]] == search_w:
                ans -= 1

print(ans)

Submission status: Time Limit Exceeded

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

count = 0
for i in range(len(S)):
  for j in range(i,len(S)):
    if S[i] == S[j]: continue
    for k in range(j,len(S)):
      if k-j == j-i: continue
      if S[i] == S[k] or S[j] == S[k]:continue
      count+=1
print(count)

Submission status: Time Limit Exceeded

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

dic = {'R':0, 'G':0, 'B':0}
for i in range(N):
  dic[S[i]] += 1

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

for i in range(N):
  for j in range(i+1, N):
    if 2*j - i < len(S):
      if S[i] == 'R' and S[j] == 'G' and S[2*j - i] == 'B':
        ans -= 1

      if S[i] == 'R' and S[j] == 'B' and S[2*j - i] == 'G':
        ans -= 1

      if S[i] == 'G' and S[j] == 'R' and S[2*j - i] == 'B':
        ans -= 1

      if S[i] == 'G' and S[j] == 'B' and S[2*j - i] == 'R':
        ans -= 1
      
      if S[i] == 'B' and S[j] == 'G' and S[2*j - i] == 'R':
        ans -= 1

      if S[i] == 'B' and S[j] == 'R' and S[2*j - i] == 'G':
        ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n=int(input())
s=list(input())
C=Counter(s)
r,g,b=C["R"],C["G"],C["B"]
ans=r*g*b

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
r=s.count("R")
g=s.count("G")
b=s.count("B")
ans=r*g*b

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
r=s.count("R")
g=s.count("G")
b=s.count("B")
ans=r*g*b

for i in range(n-2):
    for j in range(i+1,n-1):
        if 2*j-i<=n-1:
            a,b,c=s[i],s[j],s[2*j-i]
            if a==b or b==c or c==a:
                pass
            else:
                ans-=1

print(ans)

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)

b=len(B)
ans=0

for j in R:
    for k in G:
        cnt=b
        if 2*j-k in B:cnt-=1
        if 2*k-j in B:cnt-=1
        if (j+k)/2 in B:cnt-=1
        ans+=cnt

print(ans)

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)

b=len(B)
ans=0

for j in R:
    for k in G:
        cnt=b
        if 2*j-k in B:cnt-=1
        if 2*k-j in B:cnt-=1
        if (j+k)/2 in B:cnt-=1
        ans+=cnt

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
r = s.count("R")
g = s.count("G")
b = s.count("B")
total = r*g*b
rgb = ord('R') + ord('G') + ord("B")

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

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)
count = 0
unchi = b
for i in r:
	for j in g:
		u = abs(i-j)
		count += [0,1][(max(i,j)+u) in unchi] + [0,1][(min(i,j)-u) in unchi] + [0,1][u%2 == 0 and (i+j)//2 in unchi]
print(len(r)*len(g)*len(b)-count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import itertools
N = int(input())
S = list(input())
num = [i for i in range(0,N)]
C = list(itertools.combinations(num,3))
ans = 0
for c in C:
    if c[1]-c[0] == c[2]-c[1]:
        continue
    if S[c[0]] != S[c[1]] and S[c[1]] != S[c[2]] and S[c[0]] != S[c[2]]:
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
R=[]
G=[]
B=[]
ans=0
x=0
a=0
b=0
c=0
for i in S:
    if i=="R":
        R.append(x)
    if i=="G":
        G.append(x)
    if i=="B":
        B.append(x)
    x+=1
for j in R:
    for k in G:
        if 2*k-j in B:
            B.pop(B.index(2*k-j))
            a+=1
        if 2*j-k in B:
            B.pop(B.index(2*j-k))   
            b+=1
        if (j+k)/2 in B:
            B.pop(B.index((j+k)/2))
            c+=1
        ans+=len(B)
        if a!=0:
            B.append(2*k-j)
            a=0
        if b!=0:
            B.append(2*j-k)
            b=0
        if c!=0:
            B.append((j+k)/2)
            c=0
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections

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

c = collections.Counter(s)
r = c['R'] * c['G'] * c['B']

for i in range(n):
    for k in range(i, n):
        j = (i + k) // 2
        if (i + k) % 2 == 0 and 0 < 2 < n:
            if s[i] != s[k] and s[k] != s[j] and s[i] != s[j]:
                r -= 1

print(r)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-

# 入力はstringで入る．
# intにしたければ int(input())
# floatで受け取ると 1.0 になる．

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

Sijk = []
for i in range(0,N):
    for j in range(i+1,N):
        if S[i] != S[j]:
            for k in range(j+1,N):
                if j-i != k-j:
                    Sijk.append(set([S[i],S[j],S[k]]))

print(Sijk.count(set(['R','G','B'])))

Submission status: Time Limit Exceeded

Solution code:
import sys
import os

def file_input():
    f = open('Beginner_Contest_162/input.txt', 'r')
    sys.stdin = f

def main():
    #file_input()
    N=input()
    S=input()
    # print(S)

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
import os

def file_input():
    f = open('Beginner_Contest_162/input.txt', 'r')
    sys.stdin = f

def main():
    #file_input()
    N=input()
    S=input()
    # print(S)

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=list(input())
import collections
c=collections.Counter(S)
if len(c)==3:
  
  u=1
  for i in c.values():
    u=u*i 
  for i in range(N-2):
    for j in range(i+1,int((N-1+i)/2)+1):
      if 2*j-i<=N-1:
      
        if set([S[i],S[j],S[2*j-i]])=={'B','R','G'} :
          u=u-1
  print(u)       
else:
  print(0)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=list(input())
import collections
c=collections.Counter(S)
if len(c)==3:
  
  u=1
  for i in c.values():
    u=u*i 
  for i in range(N-2):
    for j in range(i+1,N-1):
      if 2*j-i<=N-1:
      
        if set([S[i],S[j],S[2*j-i]])=={'B','R','G'} :
          u=u-1
  print(u)       
else:
  print(0)

Submission status: Time Limit Exceeded

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

ans = 0
for i, i_s in enumerate(s):
    first = [i, i_s]
    s_2 = s[i+1:]
    
    for j, j_s in enumerate(s_2):
        if (first[1]!=j_s):
            second = [j, j_s]
            s_3 = s_2[j+1:]
        else:
            continue
        
        for k, k_s in enumerate(s_3):
            if (first[1]!=k_s and second[1]!=k_s and second[0]!=k):
                ans += 1
            else:
                continue
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(map(str,input()))
ans = 0
b = 0
for i in range(N):
  for j in range(i,N):
    b = j - i
    if S[i] != S[j]:
      if j + b > N:
        for k in range(j,N):
          if S[i] != S[k] and S[j] != S[k]:
            ans += 1
      else:
        for k in range(j,j+b):
          if S[i] != S[k] and S[j] != S[k]:
            ans += 1
        for k in range(j+b+1,N):
          if S[i] != S[k] and S[j] != S[k]:
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(map(str,input()))
ans = 0
b = 0
for i in range(N):
  for j in range(i,N):
    if S[i] != S[j]:
      for k in range(j,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:
import itertools
n=int(input())
s=input()
ans=0
for l in itertools.combinations(range(0,n),3):
    i,j,k=l
    if j-i!=k-j:
        if s[i]!=s[j] and s[i]!=s[k] and s[j]!=s[k]:
            ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
S = list(S)
count = 0
for i in range(N):
  for j in range(i+1, N):
    if S[i] == S[j]:
      continue
    for k in range(j+1, N):
      if S[i] == S[j] or S[j] == S[k] or S[i] == S[k]:
        continue
      if j-i == k-j:
          continue
      count += 1
print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

ans = 0

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

for r in R:
    for g in G:
        for b in B:
            if 2 * r == g + b or 2 * g == r + b or 2 * b == r + g:
                continue
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

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


def main():
    N = int(input())
    S = list(input())
    ans = 0
    for i in range(0, 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] \
                   and j - i != k - j:
                    ans += 1
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
n = int(input())
s = input()

l = [i for i in range(n)]
nums = list(combinations(l,3))

ans = 0
for i,j,k in nums:
  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: 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-1):
    for j in range(i+1,n):
        k = 2*j -i
        if k <= n-1:
            if s[i]!=s[j] and s[j]!=s[k] and s[k]!=s[i]:
                ans -= 1

print(ans)

Submission status: Time Limit Exceeded

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

sum_r = 0
sum_g = 0
sum_b = 0

l_r = []
l_g = []
l_b = []

for i in reversed(s):
    if i == 'R':
        sum_r += 1
    elif i == 'G':
        sum_g += 1
    else:
        sum_b += 1
    l_r.append(sum_r)
    l_g.append(sum_g)
    l_b.append(sum_b)

l_r.reverse()
l_g.reverse()
l_b.reverse()

s_r = {'G', 'B'}
s_g = {'R', 'B'}
s_b = {'G', 'R'}

sum_n = 0
len_s = len(s)
cn = 0

for i, v in enumerate(s):
    for j, w in enumerate(s):
        if j < i:
            continue
        if j == len_s - 1:
            continue
        if not v == w:
            se = {v, w}
            if se == s_r:
                sum_n += l_r[j]
                if 2 * j - i < len_s:
                    if s[2 * j - i] == 'R':
                        sum_n -= 1
            elif se == s_g:
                sum_n += l_g[j]
                if 2 * j - i < len_s:
                    if s[2 * j - i] == 'G':
                        sum_n -= 1
            else:
                sum_n += l_b[j]
                if 2 * j - i < len_s:
                    if s[2 * j - i] == 'B':
                        sum_n -= 1

print(sum_n)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = str(input())
S = list(S)
R = S.count("R")
G = S.count("G")
B = S.count("B")
count = R*G*B
double = 0
for i in range(N-2):
    for j in range(i+1,N-1):
        k = 2*j-i
        if k < N:
            s = set([S[i],S[j],S[k]])
            if len(s) == 3:
                count -= 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import itertools
import bisect

def main():
    N = int(input())
    S = input()
    d = {'R': [], 'G': [], 'B': []}
    for i, c in enumerate(S):
        d[c].append(i)
    #print(d)
    #answer = set()
    cnt = 0
    for xx, yy, zz in itertools.permutations(('R', 'G', 'B'), 3):
        x, y, z = d[xx], d[yy], d[zz]
        for i in x:
            y_start = bisect.bisect_left(y, i)
            for jj in range(y_start, len(y)):
                j = y[jj]
                assert i < j
                z_start = bisect.bisect_left(z, j)
                # z_point = bisect.bisect_left(z, 2*j-i)
                # #print(z_point, z)
                # if (len(z) <= z_point) or (z[z_point] - j != j - i):
                #     cnt += len(z)
                # else:
                #     cnt += len(z) - 1
                for kk in range(z_start, len(z)):
                    k = z[kk]
                    assert j < k
                    if k-j > j-i:
                        #超えたのでもうチェックしなくていい
                        cnt += len(z) - kk
                        break
                    elif k-j == j-i:
                        cnt += len(z) - kk - 1
                        break
                    else:
                        #assert k > j > i
                        #answer.add((i+1, j+1, k+1))
                        cnt += 1
    print(cnt)
    #assert len(answer) == cnt
    #print(answer)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import itertools
import bisect

def main():
    N = int(input())
    S = input()
    d = {'R': [], 'G': [], 'B': []}
    for i, c in enumerate(S):
        d[c].append(i)
    #print(d)
    #answer = set()
    cnt = 0
    for xx, yy, zz in itertools.permutations(('R', 'G', 'B'), 3):
        x, y, z = d[xx], d[yy], d[zz]
        for i in x:
            y_start = bisect.bisect_left(y, i)
            for jj in range(y_start, len(y)):
                j = y[jj]
                assert i < j
                z_start = bisect.bisect_left(z, j)
                for kk in range(z_start, len(z)):
                    k = z[kk]
                    assert j < k
                    if k-j == j-i:
                        continue
                    #assert k > j > i
                    #answer.add((i+1, j+1, k+1))
                    cnt += 1
    print(cnt)
    #assert len(answer) == cnt
    #print(answer)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import itertools

def main():
    N = int(input())
    S = input()
    d = {'R': [], 'G': [], 'B': []}
    for i, c in enumerate(S):
        d[c].append(i)
    #print(d)
    #answer = set()
    cnt = 0
    for xx, yy, zz in itertools.permutations(('R', 'G', 'B'), 3):
        x, y, z = d[xx], d[yy], d[zz]
        for i in x:
            for j in y:
                if (j < i):
                    continue
                for k in z:
                    if k < j:
                        continue
                    elif k-j == j-i:
                        continue
                    #assert k > j > i
                    #answer.add((i+1, j+1, k+1))
                    cnt += 1
    print(cnt)
    #assert len(answer) == cnt
    #print(answer)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(input())
count = 0
for i in range(n-2):
    for j in range(i+1,n-1):
        if s[i] != s[j]:
            a = ['R','G','B']
            a.remove(s[i])
            a.remove(s[j])
            x = s[j:].count(a[0])
            count += x
            if 2*j-i <= n-1:
                if s[2*j-i] == a[0]:
                    count -= 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
N=int(input())
S = input()
 
for i in range(N):
  r = S.count("R")
  g = S.count("G")
  b = S.count("B")
  ans = r * g * b

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

print(ans)

Submission status: Time Limit Exceeded

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

for i in range(N):
  r = S.count("R")
  g = S.count("G")
  b = S.count("B")
  ans = r * g * b

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
d = {'R':0,'G':0,'B':0}
for i in s:
    d[i] += 1
c = d['R'] * d['G'] * d['B']

for i in range(n):
    for j in range(i+1,n):
        k = 2 * j - i
        if(k <= j or k >= n):continue
        if(s[i] == s[j]):continue
        if(s[j] == s[k]):continue
        if(s[i] == s[k]):continue
        c -= 1

print(c)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
d = {'R':0,'G':0,'B':0}
for i in s:
    d[i] += 1
c = d['R'] * d['G'] * d['B']

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

print(c)

Submission status: Time Limit Exceeded

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

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

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

print(c)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
import collections

N = int(input())
S = input()
tonum = {"R": 0, "G": 1, "B": 2}
ans = 0
for i in range(1, N - 1):
    d = tonum[S[i]]
    c2 = collections.Counter()
    for j in range(i + 1, N):
        c2[tonum[S[j]]] += 1
    for j in range(i):
        dj = tonum[S[j]]
        if dj == d:
            continue
        dk = 3 - d - dj
        ans += c2[dk]
        kk = i + (i - j)
        if kk < N and tonum[S[kk]] == dk:
            ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=list(input())
Color={"B","R","G"}
sum=0
for i in range(len(S)):
    for j in range(i+1,len(S)):
        if S[i]==S[j]:
            continue
        else:
            for k in range(j+1,len(S)):
                remain=Color-{S[i],S[j]}
                if S[k]==''.join(list((remain))):
                    if j-i != k-j:
                        sum += 1
print(sum)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import math
import queue
from collections import defaultdict
 
def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()
def factorization(n):
	res = []
	if n%2==0:
		res.append(2)
	for i in range(3,math.floor(n//2)+1,2):
		if n%i==0:
			c = 0
			for j in res:
				if i%j==0:
					c=1
			if c==0:
				res.append(i)
	return res
def fact2(n):
	p = factorization(n)
	res = []
	for i in p:
		c=0
		z=n
		while 1:
			if z%i==0:
				c+=1
				z/=i
			else:
				break
		res.append([i,c])
	return res
def fact(n):#階乗
	ans = 1
	m=n
	for _i in range(n-1):
		ans*=m
		m-=1
	return ans
def comb(n,r):#コンビネーション
	l = min(r,n-r)
	m=n
	u=1
	for _i in range(l):
		u*=m
		m-=1
	return u//fact(l)
def printQueue(q):
	r=q
	ans=[0]*r.qsize()
	for i in range(r.qsize()-1,-1,-1):
		ans[i] = r.get()
	print(ans)
def binary_search(li,item):#二分探索
	left,right = 0, len(li)
	while left<=right:
		mid = (left+right)//2
		if li[mid]==item:
			return mid
		elif li[mid]<item:
			left = mid+1
		else:
			right = mid-1
	return -1

N = readInt()
S = readChar()
rgb = {"R":[],"G":[],"B":[]}
for i in range(N):
	rgb[S[i]].append(i)

c=s=0
c=len(rgb["R"])*len(rgb["G"])*len(rgb["B"])
for r in rgb["R"]:
	for g in rgb["G"]:
		if len(S)>2*max(r,g)-min(r,g) and S[2*max(r,g)-min(r,g)]=="B":
			s+=1
for r in rgb["R"]:
	for g in rgb["B"]:
		if len(S)>2*max(r,g)-min(r,g) and S[2*max(r,g)-min(r,g)]=="G":
			s+=1
for r in rgb["B"]:
	for g in rgb["G"]:
		if len(S)>2*max(r,g)-min(r,g) and S[2*max(r,g)-min(r,g)]=="R":
			s+=1

ans=c-s
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
import queue
from collections import defaultdict
 
def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()
def factorization(n):
	res = []
	if n%2==0:
		res.append(2)
	for i in range(3,math.floor(n//2)+1,2):
		if n%i==0:
			c = 0
			for j in res:
				if i%j==0:
					c=1
			if c==0:
				res.append(i)
	return res
def fact2(n):
	p = factorization(n)
	res = []
	for i in p:
		c=0
		z=n
		while 1:
			if z%i==0:
				c+=1
				z/=i
			else:
				break
		res.append([i,c])
	return res
def fact(n):#階乗
	ans = 1
	m=n
	for _i in range(n-1):
		ans*=m
		m-=1
	return ans
def comb(n,r):#コンビネーション
	l = min(r,n-r)
	m=n
	u=1
	for _i in range(l):
		u*=m
		m-=1
	return u//fact(l)
def printQueue(q):
	r=q
	ans=[0]*r.qsize()
	for i in range(r.qsize()-1,-1,-1):
		ans[i] = r.get()
	print(ans)
def binary_search(li,item):#二分探索
	left,right = 0, len(li)
	while left<=right:
		mid = (left+right)//2
		if li[mid]==item:
			return mid
		elif li[mid]<item:
			left = mid+1
		else:
			right = mid-1
	return -1

N = readInt()
S = readChar()
rgb = {"R":[],"G":[],"B":[]}
for i in range(N):
	rgb[S[i]].append(i)

c=s=0
c=len(rgb["R"])*len(rgb["G"])*len(rgb["B"])
for r in rgb["R"]:
	for g in rgb["G"]:
		if 2*max(r,g)-min(r,g) in rgb["B"]:
			s+=1
for r in rgb["R"]:
	for g in rgb["B"]:
		if 2*max(r,g)-min(r,g) in rgb["G"]:
			s+=1
for r in rgb["B"]:
	for g in rgb["G"]:
		if 2*max(r,g)-min(r,g) in rgb["R"]:
			s+=1

ans=c-s
print(ans)