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())
STR = input()
 
total = 0

DP = [[False] * (N + 1) for _ in range(N+1)]
for i in range(1, N + 1):
    for j in range(i + 1, N + 1):
        if DP[i][j]:
            continue
        if STR[i - 1] == STR[j - 1]:
            DP[i][j] = True
            continue
        for k in range(j + 1, N + 1):
            if DP[i][k] or DP[j][k]:
                continue
            if STR[j - 1] == STR[k - 1]:
                DP[j][k] = True
                continue
            if STR[k - 1] == STR[i - 1]:
                DP[i][k] = True
                continue
            if (j - i != k - j):
                total += 1

print(total)

Submission status: Time Limit Exceeded

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

r_list = []
g_list = []
b_list = []
for _ in range(n):
    if s[_] == "R":
        r_list.append(_)
    elif s[_] == "G":
        g_list.append(_)
    else:
        b_list.append(_)

count = 0
for r in r_list:
    for g in g_list:
        if min(r, g) - abs(r - g) in b_list:
            count += 1
        if max(r, g) + abs(r - g) in b_list:
            count += 1
        if (r + g) % 2 == 0:
            if (r + g) // 2 in b_list:
                count += 1
                
print(len(r_list) * len(g_list) * len(b_list) - count)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations

if __name__ == "__main__":
	N = int(input())
	S = tuple(input())

	sum = 0

	#l = tuple(range(N))
	comb = tuple(combinations(range(N),3))

	for c in comb:
		if (c[1] - c[0]) != (c[2] - c[1]):
			S_i = S[c[0]]; S_j = S[c[1]]; S_k = S[c[2]]
			if (S_i != S_j) and (S_i != S_k) and (S_j != S_k):
				sum += 1

	print(sum)

Submission status: Time Limit Exceeded

Solution code:
import sys
import collections

N = int(input())
S = [ch for ch in input()]
RGB = [0, 0, 0]
for ch in S:
    if ch == "R":
        RGB[0] += 1
    elif ch == "G":
        RGB[1] += 1
    else:
        RGB[2] += 1
total = 1
for count in RGB:
    if count == 0:
        print(0)
        sys.exit()
    total *= count

for i in range(len(S)):
    l, r = i - 1, i + 1
    while 0 <= l and r < len(S):
        if len(set([S[i], S[l], S[r]])) == 3:
            total -= 1
        l -= 1
        r += 1
print(total)

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)
        elif x =="B":b.append(i)
 
    r = np.array(r)
    g = np.array(g)
    b = np.array(b)
 
 
    def rgb(r, g, 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
                ans += len(b[b>g_])
                # brg
                if (r_ - (g_ - r_)) in sb:
                    ans -= 1
                ans += len(b[b<r_])

                # rbg
                if ((r_ + g_)/2) in sb:
                    ans -= 1
                ans += len(b[(r_<b)&(b<g_)])

            # gr
            for g_ in g[g<r_]:
                # grb
                if (r_ + (r_ - g_)) in sb:
                    ans -= 1
                ans += len(b[b>r_])
                # bgr
                if (g_ - (r_ - g_)) in sb:
                    ans -= 1
                ans += len(b[b<g_])
                # gbr
                if ((r_ + g_)/2) in sb:
                    ans -= 1
                ans += len(b[(g_<b)&(b<r_)])

        return ans
 
    k = rgb(r,g,b)
    print(k)
 
 
    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:
"""
import random
import functools
import copy
import bisect
import array
import re
import collections
import heapq
import fractions
import itertools
import string
import math
from operator import itemgetter as ig
from bisect import bisect_left, bisect_right, insort_left, insort_right
from itertools import permutations, combinations, product, accumulate, groupby
from heapq import heappush, heappop
from collections import deque, defaultdict, Counter
import sys
sys.setrecursionlimit(10 ** 7)
# import numpy as np

inf = 10 ** 20
INF = float("INF")
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]
ddn9 = ddn + [(0, 0)]
'''for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:'''
"""
from collections import Counter
import sys
sys.setrecursionlimit(10 ** 7)


def wi(): return list(map(int, sys.stdin.readline().split()))
# WideIntPoint


def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]


def ws(): return sys.stdin.readline().split()


def si(): return int(sys.stdin.readline())  # SingleInt


def ss(): return input()


def hi(n): return [si() for _ in range(n)]


def hs(n): return [ss() for _ in range(n)]  # HeightString


def s_list(): return list(input())


def mi(n): return [wi() for _ in range(n)]  # MatrixInt


def mip(n): return [wip() for _ in range(n)]


def ms(n): return [ws() for _ in range(n)]


def num_grid(n): return [[int(i) for i in sys.stdin.readline().split()[
    0]] for _ in range(n)]  # NumberGrid


def grid(n): return [s_list() for _ in range(n)]


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def main():
    N = si()
    S = ss()
    ans = 0
    for i in range(N-2):
        for j in range(i+1, N-1):
            if S[i] == S[j]:
                continue
            else:
                s = S[j+1:N]
                clist = Counter(s)
                if S[i] == 'R' and S[j] == 'B':
                    ans += clist['G']
                    if j-i+j <= N-1:
                        if S[j-i+j] == 'G':
                            ans -= 1
                elif S[i] == 'R' and S[j] == 'G':
                    ans += clist['B']
                    if j-i+j <= N-1:
                        if S[j-i+j] == 'B':
                            ans -= 1
                elif S[i] == 'B' and S[j] == 'R':
                    ans += clist['G']
                    if j-i+j <= N-1:
                        if S[j-i+j] == 'G':
                            ans -= 1
                elif S[i] == 'B' and S[j] == 'G':
                    ans += clist['R']
                    if j-i+j <= N-1:
                        if S[j-i+j] == 'R':
                            ans -= 1
                elif S[i] == 'G' and S[j] == 'B':
                    ans += clist['R']
                    if j-i+j <= N-1:
                        if S[j-i+j] == 'R':
                            ans -= 1
                elif S[i] == 'G' and S[j] == 'R':
                    ans += clist['B']
                    if j-i+j <= N-1:
                        if S[j-i+j] == 'B':
                            ans -= 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
X = int(input())
S = str(input())
a = 0

for i in range(X):
   for j in range(i+1,X):
      if S[j] != S[i]:
        for k in range(j+1,X):
          Sk = S[k]
          if (j - i) != (k - j) and Sk != S[i] and Sk != S[j]:
            a = a + 1
        
print(a)

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)

#print(B)
count = len(R)*len(G)*len(B)
d={}
for i in R:
    for j in G:
        big = max(i,j)
        small = min(i,j)
        
        x = 2*big-small
        y = 2*small-big
        z = (big+small)/2
        
        if x in d:
            d[x] += 1
        else:
            d[x] = 1
        if y in d:
            d[y] += 1
        else:
            d[y] = 1
        if z%1==0:
            if z in d:
                d[z] += 1
            else:
                d[z] = 1
        
        #if 2*big-small in B:
            #count -= 1
            #print(i,j)
            #print("hoi")
        #if 2*small-big in B:
            #count -= 1
            #print(i,j)
        #if (big+small)/2 in B:
            #print((big+small)/2)
            #count -= 1
            #print(i,j)
    
for i in B:
    if i in d:
        count -= d[i]
    
print(count)

Submission status: Time Limit Exceeded

Solution code:

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

for i in range(len(s)-2):
    for j in range(i+1,len(s)-1):
        for k in range(j+1,len(s)):
            if s[i] != s[j] and s[i] != s[k] and s[j] != s[k] and j-i != k-j:
                count += 1
print(count)

Submission status: Time Limit Exceeded

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

after_cumsum_RGB = [[0]*(N+1), [0]*(N+1), [0]*(N+1)]

def rgb_hash(a):
  if S[i] == 'R':
    return 0
  elif S[i] == 'G':
    return 1
  elif S[i] == 'B':
    return 2
  
def ci_hash(a):
  if 'R' not in a:
    return 0
  elif 'G' not in a:
    return 1
  elif 'B' not in a:
    return 2

def c_hash(a):
  if 'R' not in a:
    return 'R'
  elif 'G' not in a:
    return 'G'
  elif 'B' not in a:
    return 'B'
  
for i in range(N-1, -1, -1):
  if S[i] == 'R':
    after_cumsum_RGB[0][i] = 1
  if S[i] == 'G':
    after_cumsum_RGB[1][i] = 1
  if S[i] == 'B':
    after_cumsum_RGB[2][i] = 1
  for j in range(3):
    after_cumsum_RGB[j][i] += after_cumsum_RGB[j][i+1]

ans = 0
for i in range(0, N-2):
  for j in range(i+1, N-1):
    if S[i] != S[j]:
      target = ci_hash([S[i], S[j]])
      ans += after_cumsum_RGB[target][j+1]
      if 2*j-i <= N-1:
        if S[2*j-i] == c_hash([S[i], S[j]]):
          ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(input())
r = []
g = []
b = []

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


ans = 0
for i in range(n):
    for j in range(i + 1, n):
        if s[i] == s[j]:
            continue
        if "R" not in [s[i], s[j]]:
            a = list(map(lambda x: x > j, r))
            ans += sum(a)
            if 2 * j - i in r:
                ans -= 1
        elif "G" not in [s[i], s[j]]:
            a = list(map(lambda x: x > j, g))
            ans += sum(a)
            if 2 * j - i in g:
                ans -= 1
        elif "B" not in [s[i], s[j]]:
            a = list(map(lambda x: x > j, b))
            ans += sum(a)
            if 2 * j - i in b:
                ans -= 1


print(ans)

Submission status: Time Limit Exceeded

Solution code:
import copy


def main():
    N = int(input())
    S = input()
    count = 0
    RGB = ["R", "G", "B"]

    for i in range(1, N-1):
        RGB = ["R", "G", "B"]
        RGB.remove(S[i - 1])

        for j in range(i+1, N):
            RGB_2 = copy.deepcopy(RGB)
            if S[i-1] == S[j-1]:
                continue
            #print(f"before_remove {RGB_2}, remove{S[j-1]}")
            RGB_2.remove(S[j-1])
            target = RGB_2[0]
            count += S.count(target, j, N)
            #print(S.count(RGB[0], j, N))
            if 2 * j - i > N:
                pass
            else:
                if S[2 * j - i - 1] == target:
                    count -= 1

    print(count)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
ans = 0
for i in range(0,N-2):
    if S[i] == 'R':
        for j in range(i+1,N-1):
            if S[i] == S[j]:
                continue
            else:
                for k in range(j+1,N):
                    if j-i == k-j:
                        continue
                    else:
                        if S[i] != S[k] and S[j] != S[k]:
                            ans = ans + 1
for i in range(0,N-2):
    if S[i] == 'G':
        for j in range(i+1,N-1):
            if S[i] == S[j]:
                continue
            else:
                for k in range(j+1,N):
                    if j-i == k-j:
                        continue
                    else:
                        if S[i] != S[k] and S[j] != S[k]:
                            ans = ans + 1
for i in range(0,N-2):
    if S[i] == 'B':
        for j in range(i+1,N-1):
            if S[i] == S[j]:
                continue
            else:
                for k in range(j+1,N):
                    if j-i == k-j:
                        continue
                    else:
                        if S[i] != S[k] and S[j] != S[k]:
                            ans = ans + 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def count_section_by_zero(data):
  count = 0
  flg = False
  start = 0
  for i, d in enumerate(data):
    if flg is False and d != 0:
      count += 1
      flg = True
      
    if d == 0:
      flg = False
  return count

def input_list():
  return list(map(int, input().split()))

def input_list_str():
  return list(map(str, input().split()))

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

# 2で割り切れる回数
def divide_two(arg):
  c = 0
  while True:
    if c >= 2:
      break
    if arg % 2 != 0:
      break
    arg //= 2
    c += 1
  return c

# 素因数分解
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

def main():
  n = int(input())
  if n < 4:
    print(0)
    exit()
  ans = 0
  s = list(input())
  for i, a in enumerate(s):
    j = i+1
    k = i+2
    for bi, b in enumerate(s[j:]):
      bb = bi+j
      for ci, c in enumerate(s[k:]):
        cc = ci+k
        if i < bb < cc:
          if a == b or b == c or a == c:
            continue
          if bb - i == cc - bb:
            continue
          ans += 1
  print(ans)          
import math
import fractions
import collections
import itertools
from functools import reduce
main()

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']

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

out = 0

def rgb (tmpstr, j, i, s):
    if s[j] != s[i]:
        tmpstr = s[j + 1 :  n]
        tmpset = set(["R","G","B"])
        tmpset.remove(s[j])
        tmpset.remove(s[i])
        remain = list(tmpset)[0]
        num = tmpstr.count(remain) 
        if (j + (j - i)) < len(s):
            if  s[j + (j - i)]  == remain:
                num = num - 1
        return num
    else:
        return 0
    
    
out = sum([sum([rgb(s[j + 1 :  n], j, i, s) for j in range(i+1, n-1)]) for i in range(n-2)])
#for i in range(n-2):
#    out += sum([rgb(s[j + 1 :  n], j, i, s) for j in range(i+1, n-1)])
print(out)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**6)

def rgb(n,s):

    
    a=s[0]
    if len(s)==3:
        SUM=0

    else:
        SUM=rgb(n-1,s[1:])

        for i in range(1,n-1):
            l=["R","G","B"]
            l.remove(a)
            if s[i] != a:
                b=s[i]

                l.remove(b)
                if l!=[]:
                    c=l[0]
                    p=s.count(c,i+1,2*i)
                    q=s.count(c,2*i+1,n+1)
                    SUM+=p+q
                
    return SUM

n=int(input())
s=input()
print(rgb(n,s))

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

a = s_c["R"] * s_c["B"] * s_c["G"]

for i in range(N - 2):
    for j in range(i + 1, N - 1):
        for _ in range(j + 1, N):
            if (S[i] != S[j] and S[i] != S[_] and S[j] != S[_]) and \
                not (j - i != _ - j):
                a -= 1
print(a)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
from functools import reduce

# 入力を整数に変換して受け取る
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def MI1(): return map(int1, sys.stdin.readline().split())
# 入力全てを整数に変換したものの配列を受け取る
def LI(): return list(map(int, sys.stdin.readline().split()))
# 入力全てを整数に変換して1引いたものの配列を受け取る
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SR(): return sys.stdin.readline().rstrip()

ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
ascii_uppercase2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ'

# リストを改行区切りで出力する
p2D = lambda x: print(*x, sep="\n")
p2E = lambda x: print(''.join(x))
p2S = lambda x: print(*x, sep=" ")

def gcd_list(numbers):
    return reduce(math.gcd, numbers)

N = II()
S = input()

ans = 0
for i in range(1, N-2):
    for j in range(1+i, N):
        if j <= i:
            continue
        for k in range(1+j, N+1):
            if k <= j:
                continue
            if not(j-i == k-j or S[i-1] == S[j-1] or S[i-1] == S[k-1] or S[k-1] == S[j-1]):
                ans+=1
print(ans)

Submission status: Time Limit Exceeded

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

    d = 0
    for k in range(2,N):
        sk = S[k]
        for j in range(k):
            sj = S[j]
            if sk != sj:
                for i in range(j):
                    si = S[i]
                    if (si != sj) & (si != sk) & (j-i != k-j):
        
                        d += 1

    print(d)


main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = [0 if i=="R" else 1 if i=="G" else 2 for i in 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 j-i == k-j or s[i] == s[k] or s[j] == s[k]:
        continue
      ans += 1

      
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect
import itertools

def binary_search(left, right, search, List):
    while right-left > 1:
        mid = (left+right)//2
        if List[mid] == search:
            return mid
        else:
            if search > List[mid]:
                left = mid
            else:
                right = mid
    return -1


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

d = {"R": [], "G": [], "B": []}

for i in range(n):
    d[s[i]].append(i)

p=list(itertools.permutations(["R","G","B"]))

ans = 0
for a,b,c in p:
    for r in range(len(d[a])):
        second = bisect.bisect_left(d[b], d[a][r])
        for g in range(second, len(d[b])):
            third = bisect.bisect_left(d[c], d[b][g])
            ans += len(d[c])-third
            if binary_search(-1,len(d[c]),2*d[b][g]-d[a][r],d[c])!=-1:
                ans-=1

print(ans)

Submission status: Time Limit Exceeded

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

sum=0
x1=""
x2=""
y1=0

for i in range(n-2):
  x1=S[i]
  for j in range(i+1,n-1):
    if(S[j]!=x1):
      x2=S[j]
      x3=list({"R","G","B"}-{x1,x2})
      #print(x3)
      sum=sum+S[j+1:n].count(x3[0])
      if(2*j-i<n):
        sum=sum-S[2*j-i].count(x3[0])
          
          
print(int(sum))

Submission status: Time Limit Exceeded

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

c=0
i1=0
j1=0
for i in range(n):
    for j in range(i+1,n):
        if s[i]==s[j]:
            i1+=1
            continue
        for k in range(j+1,n):
            if s[i]==s[k]:
                continue
            elif s[j]==s[k]:
                j1+=1
                continue
            if (j-i)!=(k-j):
                c+=1
        j=j1
        j1=0
    i=i1
    i1=0
print(c)

Submission status: Time Limit Exceeded

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

ans=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(k==j+j-i):
                    continue
                elif(s[i]!=s[k] and s[j]!=s[k]):
                    ans+=1
        else:
            continue

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
R_i = [i for i, x in enumerate(S) if x == 'R']
G_i = [i for i, x in enumerate(S) if x == 'G']
B_i = [i for i, x in enumerate(S) if x == 'B']
ans = 0
for r_i in R_i:
    for g_i in G_i:
        ans += len(B_i)
        min_ = min(r_i, g_i)
        max_ = max(r_i, g_i)
        if 2 * min_ - max_ in B_i:
            ans -= 1
        if 2 * max_ - min_ in B_i:
            ans -= 1
        if (r_i + g_i) / 2 in B_i:
            ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    n = int(input())
    s = input()
    total = 0
    for i in range(n): # 基準を順にかえる
        for j in range(n):
            if i < j and s[i] != s[j]:
                # print(s[i], s[j], s[j+1:n+1])
                #print("RGB".replace(s[i], "").replace(s[j], ""))
                target_str = s[j+1:j+(j-i)] + s[j+(j-i)+1:n+1]
                total = total + target_str.count("RGB".replace(s[i], "").replace(s[j], ""))

    print(total)

Submission status: Time Limit Exceeded

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

t = 0
for i in range(len(s)-2):
    for j in range(1,len(s)-1):
        if j > i and s[i] != s[j]:
            for k in range(2,len(s)):
                if k > j and j-i != k-j:
                    if s[i] != s[k] and s[j] != s[k]:
                            t += 1
print(t)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
tmp_sum = 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:
      if a == 'R' and b =='G':
        c = 'B'
      elif a == 'R' and b =='B':
        c = 'G'
      elif a =='G' and b =='B':
        c = 'R'
      elif a == 'G' and b == 'R':
        c ='B'
      elif a =='B' and b == 'R':
        c = 'G'
      else:
        c = 'R'
      #print(c)
      tmp = S[j:].count(c)
      tmp_sum += tmp
      if j*2-i <= N:
        if S[j*2-i-1] == c:
          tmp_sum -= 1
print(tmp_sum)

Submission status: Time Limit Exceeded

Solution code:
import itertools

N = int(input())
S = list(input())
nums = [i for i in range(N)]
combinations = itertools.combinations(nums, 3)
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:
N = int(input())
s = str(input())
S = [''] * (10**6 + 7)
for i in range(N):
  S[i] = s[i]


r, g, b = 0, 0, 0
for i in range(N):
    if S[i] == 'R':
        r += 1
    elif S[i] == 'G':
        g += 1
    else:
        b += 1

ans = N * (N - 1) * (N - 2) // 6
for i in range(N):
  for j in range(i+1, N):
    if S[j] == S[i]:
      ans -= N - 2
    else:
      if len(set([S[2*j-i], S[i], S[j]])) == 3 and S[2*j-i] != '':
        ans -= 1
          
ans += r * (r - 1) * (r - 2) // 3
ans += g * (g - 1) * (g - 2) // 3
ans += b * (b - 1) * (b - 2) // 3

print(ans)

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 abs(i-j)!=abs(j-k) and abs(j-k)!=abs(k-i) and abs(j-i)!=abs(i-k):
                    cnt+=1
    print(cnt)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
a = 0
b = S.count("R")*S.count("B")*S.count("G")
for i in range(1,len(S)+1):
    for j in range(i,len(S)+1):
        if S[i-1] == S[j-1]:
            continue
        else:
            k = 2*j-i
            if k >= len(S)+1:
                continue
            else:
                if not S[i-1] == S[k-1] and not S[k-1] == S[j-1]:
                    a = a+1
print(b-a)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
n = int(input())
s = input()

count = 0

for i in range(0, n-2):
    for j in range(i+1, n-1):
        j_i = j - i

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

print(count)

Submission status: Time Limit Exceeded

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

result = 0

if n <= 2:
    print(0)
    
for i in range(n-2):
    if s[i] == "R":
        for j in range(i+1, n-1):
            if s[j] == "G":
                a = s[j+1:].count("B")
                if 2*j - i <= n-1:
                    if s[2*j-i] == "B":
                        a -= 1
                result += a
            elif s[j] == "B":
                a = s[j+1:].count("G")
                if 2*j - i <= n-1:
                    if s[2*j-i] == "G":
                        a -= 1
                result += a
    if s[i] == "G":
        for j in range(i+1, n-1):
            if s[j] == "R":
                a = s[j+1:].count("B")
                if 2*j - i <= n-1:
                    if s[2*j-i] == "B":
                        a -= 1
                result += a
            elif s[j] == "B":
                a = s[j+1:].count("R")
                if 2*j - i <= n-1:
                    if s[2*j-i] == "R":
                        a -= 1
                result += a
    if s[i] == "B":
        for j in range(i+1, n-1):
            if s[j] == "G":
                a = s[j+1:].count("R")
                if 2*j - i <= n-1:
                    if s[2*j-i] == "R":
                        a -= 1
                result += a
            elif s[j] == "R":
                a = s[j+1:].count("G")
                if 2*j - i <= n-1:
                    if s[2*j-i] == "G":
                        a -= 1
                result += a
print(result)

Submission status: Time Limit Exceeded

Solution code:
#
from collections import deque


def main():
    n = int(input())
    # これだとTLE
    # s = [s for s in input()]
    # count = 0
    # ps = ""
    # pc = 0
    # for j in range(1, n):
    #     if ps == s[j]:
    #         count += pc
    #         continue
    #     pc = 0
    #     for i in range(j - 1, -1, -1):
    #         if s[i] != s[j]:
    #             offset = j - i
    #             for k in range(j + 1, n):
    #                 if s[k] != s[j] and s[k] != s[i]:
    #                     if k - j != offset:
    #                         pc += 1
    #     count += pc
    # print(count)
    # これもTLEだけどちょっと効率いい
    s = deque()
    r = deque()
    g = deque()
    b = deque()
    for i, c in enumerate(input()):
        s.append(c)
        if c == "R":
            r.append(i)
        elif c == "G":
            g.append(i)
        else:
            b.append(i)
    if len(r) == 0 or len(g) == 0 or len(b) == 0:
        print(0)
        return
    count = 0
    t = []
    x = r
    for w in range(3):
        if w == 0:
            x = r
            t = [g, b]
        elif w == 1:
            x = g
            t = [r, b]
        else:
            x = b
            t = [r, g]
        for j in x:
            if s[j] == "R":
                t = [g, b]
            elif s[j] == "G":
                t = [r, b]
            else:
                t = [r, g]
            for z in range(2):
                for i in t[z]:
                    if i >= j:
                        break
                    offset = j - i
                    for k in t[1 - z]:
                        if k <= j:
                            continue
                        if offset != k - j:
                            count += 1
    print(count)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys
input = sys.stdin.readline


def main():
    n = int(input())
    s = list(input())[:n]

    # memory = [[0 for j in range(n)] for i in range(n)]
    # for i in range(n):
    #     for j in range(i, n):
    #         memory[i][j] = s[i] == s[j]
    #         memory[j][i] = s[i] == s[j]
    # print(memory)

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


if __name__ == '__main__':
    main()

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):
        if S[i]!=S[j]:
            k1=S[j+1:].count(S[i])
            k2=S[j+1:].count(S[j])
            ans=ans+len(S[j+1:])-k1-k2
        if j+(j-i)<N and S[i]!=S[j]:
            if S[j+(j-i)]!=S[i] and S[j+(j-i)]!=S[j]:
                ans-=1
            
print(ans)