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:
# -*- coding: utf-8 -*-
import numpy as np
import itertools
N = int(input())
S = (input())

r_index_lis, g_index_lis, b_index_lis = [], [], []
for i, s in enumerate(S):
    if s == 'R':
        r_index_lis.append(i)
    elif s == 'G':
        g_index_lis.append(i)
    else:
        b_index_lis.append(i)
count = 0

p = (itertools.product(r_index_lis, g_index_lis, b_index_lis))
for _p in p:
    _p = sorted(_p)
    if(_p[0] - _p[1] != _p[1] - _p[2]):
        count += 1
print(count)
# for r in r_index_lis:
#     for g in g_index_lis:
#         diff = r - g
#         if(b_index_lis)
#         count += len(b_index_lis)

# for b in b_index_lis:
#     rgb = sorted([r, g, b])
#     if (rgb[0] - rgb[1] != rgb[1] - rgb[2]):
#         count += 1

Submission status: Time Limit Exceeded

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

R = []
G = []
B = []
result = 0

for i in range(N):
    if S[i] == 'R':
        R.append(1)
        G.append(0)
        B.append(0)
    elif S[i] == 'G':
        R.append(0)
        G.append(1)
        B.append(0)
    if S[i] == 'B':
        R.append(0)
        G.append(0)
        B.append(1)
for i in range(N):
    R.append(0)
    G.append(0)
    B.append(0)

for i in range(N - 2):
    for j in range(i + 1, N - 1):
        if S[i] == 'R':
            if S[j] == 'G':
                tmp_list = B[j + 1:]
                result += sum(tmp_list)
                if B[j * 2 - i] == 1:
                    result -= 1
            if S[j] == 'B':
                tmp_list = G[j + 1:]
                result += sum(tmp_list)
                if G[j * 2 - i] == 1:
                    result -= 1
        elif S[i] == 'G':
            if S[j] == 'R':
                tmp_list = B[j + 1:]
                result += sum(tmp_list)
                if B[j * 2 - i] == 1:
                    result -= 1
            if S[j] == 'B':
                tmp_list = R[j + 1:]
                result += sum(tmp_list)
                if R[j * 2 - i] == 1:
                    result -= 1
        elif S[i] == 'B':
            if S[j] == 'R':
                tmp_list = G[j + 1:]
                result += sum(tmp_list)
                if G[j * 2 - i] == 1:
                    result -= 1
            if S[j] == 'G':
                tmp_list = R[j + 1:]
                result += sum(tmp_list)
                if R[j * 2 - i] == 1:
                    result -= 1
print(result)

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

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    s = input()
    r = 0
    s = [ord(l) for l in s]
    for k in range(n):
        sk = s[k]
        for j in range(k):
            sj = s[j]
            for i in range(j):
                si = s[i]
                if j - i != k - j and si != sj and si != sk and sj != sk:
                    r += 1
    print(r)


main()

Submission status: Time Limit Exceeded

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

l_r = [i for i, s in enumerate(S) if s=='R']
l_g = [i for i, s in enumerate(S) if s=='G']
l_b = [i for i, s in enumerate(S) if s=='B']

ans = len(l_r)*len(l_g)*len(l_b)

for r in l_r:
  for g in l_g:
    li = np.sort(np.array([r, g]))
    tmp = li[1]*2 -li[0]
    if tmp in l_b:
      ans -= 1
    
    tmp = (li[0]+li[1])/2
    if tmp in l_b:
   	  ans -= 1
      
    tmp = 2*li[0] - li[1]
    if tmp in l_b:
   	  ans -= 1

print(ans)

Submission status: Time Limit Exceeded

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

r_list = []
g_list = []
b_list = []

res = 0

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

for r_index in r_list:
    for g_index in g_list:
        for b_index in b_list:
            tmp = sorted([r_index, g_index, b_index])
            if tmp[2]-tmp[1] != tmp[1]-tmp[0]:
                # print(r_index, g_index, b_index)
                res += 1
print(res)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

l = list(S)
ans = 0
for a in range(0, N-2):
  for b in range(a+1, N-1):
    for c in range(b+1, N):
      if (l[a]!=l[b]) and (l[b]!=l[c]) and (l[c]!=l[a]) and (b-a!=c-b):
        ans += 1
        
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import permutations, product

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

R_indices = [i for i in range(N) if S[i] == "R"]
G_indices = [i for i in range(N) if S[i] == "G"]
B_indices = [i for i in range(N) if S[i] == "B"]

count = 0
for indices_1, indices_2, indices_3 in permutations([R_indices, G_indices, B_indices]):
    for i in indices_1:
        j_idx = len(indices_2)
        ng = -1
        while j_idx - ng > 1:
            m = (j_idx + ng) // 2
            if i < indices_2[m]:
                j_idx = m
            else:
                ng = m
        for j in indices_2[j_idx:]:
            k_idx = len(indices_3)
            ng = -1
            while k_idx - ng > 1:
                m = (k_idx + ng) // 2
                if j < indices_3[m]:
                    k_idx = m
                else:
                    ng = m
            for k in indices_3[k_idx:]:
                if j - i != k - j:
                    count += 1

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)

t=0
for i in range(len(r)):
    for j in range(len(g)):
        k=0
        if 2*r[i]-g[j] in b:
            k+=1
        if 2*g[j]-r[i] in b:
            k+=1
        if (r[i]+g[j])/2 in b:
            k+=1
        t+=(len(b)-k)
print(t)

Submission status: Time Limit Exceeded

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

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

print(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):
        for i in range(1,j):
            if(s[i-1] == "R"):
                if(s[j-1] == "G"):
                    if(s[k-1] == "B" and j-i != k -j):
                        ans += 1
                elif(s[j-1] == "B"):
                    if(s[k-1] == "G" and j-i != k -j):
                        ans += 1
            elif(s[i-1] == "G"):
                if(s[j-1] == "R"):
                    if(s[k-1] == "B" and j-i != k -j):
                        ans += 1
                elif(s[j-1] == "B"):
                    if(s[k-1] == "R" and j-i != k -j):
                        ans += 1
            elif(s[i-1] == "B"):
                if(s[j-1] == "G"):
                    if(s[k-1] == "R" and j-i != k -j):
                        ans += 1
                elif(s[j-1] == "R"):
                    if(s[k-1] == "G" and j-i != k -j):
                        ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict, deque
import sys
import heapq
import bisect
import math
import itertools
import string
import queue
import copy
import time
# import numpy as np
from fractions import gcd

sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7

def inp(): return int(sys.stdin.readline())
 
def inp_list(): return list(map(int, sys.stdin.readline().split()))

def lcm(x, y): return (x * y) // gcd(x, y)


N = inp()
S = input()

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

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
r = [0]*n
g = [0]*n
b = [0]*n
ans = 0

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

for i in range(n):
    for j in range(i,n):
        s1 = s[i]
        s2 = s[j]
        t = (s1,s2)
        if s1 != s2:
            if "R" not in t:
                if 2*j-i < n:
                    ans += sum(r[j+1:]) - r[2*j-i]
                else:
                    ans += sum(r[j+1:])
            elif "G" not in t:
                if 2*j-i < n:
                    ans += sum(g[j+1:]) - g[2*j-i]
                else:
                    ans += sum(g[j+1:])
            else:
                if 2*j-i < n:
                    ans += sum(b[j+1:]) - b[2*j-i]
                else:
                    ans += sum(b[j+1:])

print(ans)

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(l,3))

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

	print(sum)

Submission status: Time Limit Exceeded

Solution code:
import sys
from io import StringIO
import unittest

def resolve():
    n = int(input())
    s = input()
    ans = 0

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

    for i in range(n):
        for j in range(1, n - i, 1):
            if s[i] != s[i+j]:
                for x in ('R', 'G', 'B'):
                    if x not in (s[i], s[i+j]):
                        tmp = x
                ans += s[i+2*j+1:].count(tmp) + s[i+j+1:i+2*j].count(tmp)

    print(ans)


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """4
RRGB"""
        output = """1"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB"""
        output = """1800"""
        self.assertIO(input, output)

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

Submission status: Time Limit Exceeded

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

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

    # for i, si in enumerate(S):
    #     for j, sj in enumerate(S[i+1:]):
    #         if si == sj:
    #             continue
    #         for k, sk in enumerate(S[i+j+1:]):
    #             if j == k:
    #                 continue
    #             if si


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
from collections import defaultdict

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

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

for i in range(N):
    if S[i] == 'R':
        R.append(i)
    if S[i] == 'G':
        G.append(i)
    if S[i] == 'B':
        B.append(i)

base = len(R)*len(G)*len(B)

cnt = 0
for r in R:
    for g in G:
        for b in B:
            if abs(b-g) == abs(g-r) or abs(b-r) == abs(r-g) or abs(g-b) == abs(b-r) or abs(g-r) == abs(r-b) or abs(r-b) == abs(b-g) or abs(r-g) == abs(g-b):
                cnt += 1
print(base-cnt)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
s_list = list(s)#文字列をリストに
ans = 0
nainai = 'N'

for i in range(n-2):
    for j in range(i+1,n-1):
        if(not s[i] == s[j]):
            new_list = [s[i], s[j]]
            if('R' in new_list and 'G'in new_list):
                nainai='B'
            elif('G' in new_list and 'B'in new_list):
                nainai='R'
            else:
                nainai = 'G'
            for k in range(j+1,n):
                if (not j-i == k-j):
                    if(s[k] == nainai):
                        ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

for i in itertools.combinations(range(N),3):
  if (i[0]+i[2])/2 ==i[1]:
    continue
  L=[S[i[0]],S[i[1]],S[i[2]]]
  if len(set(L))!=3:
    continue
  ans+=1
  
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=list(input())
a=0
for i in range(0,N-2):
    for j in range(i+1,N-1):
        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:
                a+=1
            else:
                pass
print(a)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
Sl = list(map(str, input()))
cnt = 0
for i in range(N-1):
    for m in range(N-i):
        for n in range(N-i-m):
            if (m!=n):
                if (Sl[i]!=Sl[i+m] and Sl[i]!=Sl[i+m+n] and Sl[i+m]!=Sl[i+m+n]):
                    cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
from collections import Counter

N = int(input())
S = input()
ans = 0
for i, j in combinations(range(N-1), 2):
    if S[i] == S[j]: continue
    l = j - i
    temp = set([S[i], S[j]])
    if 'R' not in temp: key = 'R'
    elif 'G' not in temp: key = 'G'
    else: key = 'B'
    counter = Counter(S[j+1:j+l] + S[j + l + 1:])
    ans += counter[key]
print(ans)

Submission status: Time Limit Exceeded

Solution code:

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

ans = 0

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

for i in range(1, len(s)):
    if s[i] == "R":
        r.append(r[-1] + 1)
    else:
        r.append(r[-1])

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

for i in range(1, len(s)):
    if s[i] == "G":
        g.append(g[-1] + 1)
    else:
        g.append(g[-1])

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

for i in range(1, len(s)):
    if s[i] == "B":
        b.append(b[-1] + 1)
    else:
        b.append(b[-1])

for i in range(n):
    for j in range(i+1, n):
        if s[i] == s[j]:
            continue
        else:
            tmp = [s[i]] + [s[j]]
            target_idx = j + (j - i)

            if "R" not in tmp:
                ans += r[-1] - r[j]
                if target_idx >= len(s):
                    continue
                if s[target_idx] == "R":
                    ans -= 1
            elif "G" not in tmp:
                ans += g[-1] - g[j]
                if target_idx >= len(s):
                    continue
                if s[target_idx] == "G":
                    ans -= 1
            else:
                ans += b[-1] - b[j]
                if target_idx >= len(s):
                    continue
                if s[target_idx] == "B":
                    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+1)
    elif S[i] == 'G':
        G.append(i+1)
    else:
        B.append(i+1)

#print(R, G, B)
res = 0

for i in R:
    for j in G:
        for k in B:
            l = [i, j, k]
            l.sort()
            if l[1] - l[0] != l[2] - l[1]:
                res += 1

print(res)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
S = [s for s in S]
cnt = 0

def my_index_multi(l, x):
    return [i for i, _x in enumerate(l) if _x == x]
R = my_index_multi(S, 'R')
G = my_index_multi(S, 'G')
B = my_index_multi(S, 'B')
for r in R:
    for g in G:
        for b in B:
            l = [r, g, b]
            l.sort()
            if (l[1]-l[0])!=(l[2]-l[1]):
                cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

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+1)
    elif s[i]=="G":
        g.append(i+1)
    elif s[i]=="B":
        b.append(i+1)
for i in r:
    for j in g:
        for k in b:
            if max(i,j,k)+min(i,j,k)!=(i+j+k)*2/3:
                ans+=1

print(ans)

Submission status: Time Limit Exceeded

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


for i in range(N-2):
    a = S[i]
    for j in range(i+1,N-1):
        b = S[j]
        if a==b:
            continue
        for k in range(j+1,N):
            c = S[k]
            if (b==c)or(a==c):
                continue
            if (j-i)!=(k-j):
                cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(str(input()))
num=0
for k in range(N):
    for j in range(k):
        for i in range(j):
            if (A[i]!=A[j]) and (A[j]!=A[k]) and (A[i]!=A[k]) and ((i+k+2)!=2*(j+1)):
                num=num+1
print(num)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

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)
    


for i in range(n):
  if i in r:
    for j in g:
      if i > j:
        continue
      for k in b:
        if k < j:
          continue
        if 2*j != i+k:
          count += 1
    for j in b:
      if i > j:
        continue
      for k in g:
        if k < j:
          continue
        if 2*j != i+k:
          count += 1      
  if i in g:
    for j in r:
      if i > j:
        continue
      for k in b:
        if k < j:
          continue
        if 2*j != i+k:
          count += 1
    for j in b:
      if i > j:
        continue
      for k in r:
        if k < j:
          continue
        if 2*j != i+k:
          count += 1
  if i in b:
    for j in g:
      if i > j:
        continue
      for k in r:
        if k < j:
          continue
        if 2*j != i+k:
          count += 1
    for j in r:
      if i > j:
        continue
      for k in g:
        if k < j:
          continue
        if 2*j != i+k:
          count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
 
R = 0
G = 0
B = 0
RR = []
GG = []
BB = []
for i, s in enumerate(S):
  if s=='R':
    RR.append(i)
    R +=1
  elif s=='G':
    GG.append(i)
    G+=1
  else:
    BB.append(i*2)
    B+=1
ans_Full = R*G*B
jkn = []
for i in range(R):
  for j in range(G):
    for k in range(B):
      jkn.append(4*GG[j]-2*RR[i]-BB[k])
      jkn.append(4*RR[i]-2*GG[j]-BB[k])
      jkn.append(RR[i]+GG[j]-BB[k])
print(ans_Full - jkn.count(0))

Submission status: Time Limit Exceeded

Solution code:
import time

N = int(input())
S = input()
start = time.time()
kv = {'R':[],'G':[],'B':[]}
for i in range(N):
    kv[S[i]].append(i)
a = sorted([[len(kv['R']),'R'],[len(kv['G']),'G'],[len(kv['B']),'B']], key=lambda x:-x[0])
a1, a2, a3 = a[0][1],a[1][1],a[2][1]

ans = 0
for one in kv[a1]:
    for t in kv[a2]:
        tmp = len(kv[a3])
        if 2*t-one in kv[a3]:
            tmp-=1
        if 2*one-t in kv[a3]:
            tmp-=1
        if (t+one)/2 in kv[a3]:
            tmp-=1
        ans+=tmp
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
n = int(input())
s = list(input())
a_li = []
b_li = []
c_li = []
for i,ss in enumerate(s):
    if ss == 'R':
        a_li.append(i)
    elif ss == 'G':
        b_li.append(i)
    else:
        c_li.append(i)
if len(a_li) == 0 or len(b_li)== 0 or len(c_li) == 0:
    print(0)
    sys.exit()
all_count = 0
for a in a_li:
    for b in b_li:
        tmp = len(c_li)
        if (2*b - a)  in c_li:
            #lili.append(2*b - a)
            tmp -= 1
        if (2*a - b)  in c_li:
            #lili.append(2*a - b)
            tmp -= 1
        if (a + b)% 2 == 0 and (a + b)//2   in c_li:
            #lili.append((a + b)//2)
            tmp -= 1
        #tmp -= len(set(lili))
        all_count += tmp
print(all_count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
bc = n*[0]
gc = n*[0]
rc = n*[0]
for i in range(1, n+1):
    mi = n - i
    if s[n - i] == 'B':
        if i == 1:
            bc[mi] = 1
        else:
            bc[mi] = bc[mi+1] + 1
            rc[mi] = rc[mi+1]
            gc[mi] = gc[mi+1]
    elif s[mi] == 'R':
        if i == 1:
            rc[mi] = 1
        else:
            rc[mi] = rc[mi+1] + 1
            bc[mi] = bc[mi+1]
            gc[mi] = gc[mi+1]
    elif s[mi] == 'G':
        if i ==1:
            gc[mi] = 1
        else:
            gc[mi] = gc[mi+1] + 1
            rc[mi] = rc[mi+1]
            bc[mi] = bc[mi+1]
si = 0
for i in range(0, n - 1):
    
    for j in range(i+1, n-1):
        if s[j] != s[i]:
            d = j-i 
            if j+d < n:
                nc = s[j+d]
                if nc in [s[i], s[j]]:
                    l = list(set(['R', 'G', 'B']) - set([s[i], s[j]]))
                    if l[0] == 'R':
                        si+= rc[j+1]
                    elif l[0] == 'G':
                        si += gc[j+1] 
                    else:
                        si += bc[j+1]
                else:
                    l = list(set(['R', 'G', 'B']) - set([s[i], s[j]]))
                    if l[0] == 'R':
                        si+= rc[j+1]-1
                    elif l[0] == 'G':
                        si += gc[j+1] -1
                    else:
                        si += bc[j+1] - 1
            else:
                l = list(set(['R', 'G', 'B']) - set([s[i], s[j]]))
                if l[0] == 'R':
                    si+= rc[j+1]
                elif l[0] == 'G':
                    si += gc[j+1] 
                else:
                    si += bc[j+1]
print(si)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = tuple([s for s in input()])
B = {'R','G','B'}
ans = 0
d = {}
d['R'] = 0
d['G'] = 1
d['B'] = 2
num = [[0,0,0]for _ in range(N+1)]
for i in range(N):
    num[i+1][d[S[i]]] = num[i][d[S[i]]]+1
    for a in list(B-{S[i]}):
        num[i+1][d[a]] = num[i][d[a]]
num = num[1:]
for i in range(N-2):
    s = S[i]
    for j in range(i+1,N-1):
        if S[j] != s:
            l = j-i
            t = (B-{s,S[j]}).pop()
            L = 0
            R = 0
            if l > N-j-1:
                ans += num[N-1][d[t]]-num[j][d[t]]
            else:
                L = num[j+l-1][d[t]]-num[j][d[t]]
                R = num[N-1][d[t]]-num[j+l][d[t]]
                ans += L+R
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
def count(s):
  ans = 0
  for i in range(n-3):
    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 k-j != j-i:
          ans += 1
  print(ans)
count(s)

Submission status: Time Limit Exceeded

Solution code:
import itertools
n=input()
s=input()

R = [i for i, x in enumerate(s) if x == "R"]
G = [i for i, x in enumerate(s) if x == "G"]
B = [i for i, x in enumerate(s) if x == "B"]
a1 = [R,G,B]
l1=list(itertools.product(*a1))
a2 = [R,B,G]
l2=list(itertools.product(*a2))
a3 = [G,R,B]
l3=list(itertools.product(*a3))
a4 = [G,B,R]
l4=list(itertools.product(*a4))
a5 = [B,R,G]
l5=list(itertools.product(*a5))
a6 = [B,G,R]
l6=list(itertools.product(*a6))

l=l1+l2+l3+l4+l5+l6
#print (len(l))
ans=[]
for i in l:
    if i[1]-i[0]!=i[2]-i[1] and i[0]<i[1]<i[2]:
        ans.append(i)
#print (len(l))
print (len(ans))

Submission status: Time Limit Exceeded

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

R_inds = []
G_inds = []
B_inds = []

for i in range(n):
    if s[i] == 'R':
        R_inds.append(i)
    elif s[i] == 'G':
        G_inds.append(i)
    else:
        B_inds.append(i)
    
ans = 0
for r in R_inds:
    for g in G_inds:
        big = max(r,g)
        small = min(r,g)
        bigger = big + (big - small)
        smaller = small - (big - small)
        if (big - small) % 2 == 0:
            middle = (big + small) // 2
        else:
            middle = -1
        
        subans = 0
        subans += len(B_inds)
        if smaller in B_inds:
            subans -= 1
        if bigger in B_inds:
            subans -= 1
        if middle in B_inds:
            subans -= 1
        if subans > 0:
            ans += subans

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
sentense = input()
ans = 0
for i, s_i in enumerate(sentense):
    for j, s_j in enumerate(sentense[i+1:]):
        if s_j == s_i:
            continue
        else:
            for k, s_k in enumerate(sentense[i+j+2:]):
                if s_k == s_i or s_k == s_j:
                    continue
                else:
                    if j != k:
                        ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env pypy3
# n,m = map(int,sys.stdin.readline().split())
# a = list(map(int,sys.stdin.readline().split()))
# a = [sys.stdin.readline() for s in range(n)]
# s = sys.stdin.readline().rstrip()
# n = int(sys.stdin.readline())
# d = collections.defaultdict(list)
# import math
# from functools import reduce
import sys
sys.setrecursionlimit(15000)

n = int(sys.stdin.readline())
s = sys.stdin.readline().rstrip()
d = {"R":[],"G":[],"B":[]}
for i in range(n):
    d[s[i]].append(i)
s = 0
s = sum(1 for a in d["R"] for b in d["G"] for c in d["B"] if abs(a-b) != abs(b-c) and abs(a-c) != abs(c-b) and abs(b-a) != abs(a-c))
print(s)

Submission status: Time Limit Exceeded

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

rli = []
gli = []
bli = []
for i in range(N):
    if S[i] == "R":
        rli.append(i+1)
    if S[i] == "G":
        gli.append(i+1)
    if S[i] == "B":
        bli.append(i+1)
ans = 0
mx = max(len(rli), len(gli), len(bli))

if mx == len(rli):
    for i in gli:
        for j in bli:
            sa = max(i,j)-min(i,j)
            scr = len(rli)
            if min(i,j)-sa in rli:
                scr -= 1
            if (i+j)/2 in rli:
                scr -= 1
            if max(i,j)+sa in rli:
                scr -= 1
            ans += scr
elif mx == len(gli):
    for i in rli:
        for j in bli:
            sa = max(i,j)-min(i,j)
            scr = len(gli)
            if min(i,j)-sa in gli:
                scr -= 1
            if (i+j)/2 in gli:
                scr -= 1
            if max(i,j)+sa in gli:
                scr -= 1
            ans += scr
else:
    for i in rli:
        for j in gli:
            sa = max(i,j)-min(i,j)
            scr = len(bli)
            if min(i,j)-sa in bli:
                scr -= 1
            if (i+j)/2 in bli:
                scr -= 1
            if max(i,j)+sa in bli:
                scr -= 1
            ans += scr

print(ans)

Submission status: Time Limit Exceeded

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

r = [i for i, x in enumerate(S) if x == "R"]
g = [i for i, x in enumerate(S) if x == "G"]
b = [i for i, x in enumerate(S) if x == "B"]
kotae = 0
# print(r,g,b)
rgb = [r,g,b]
len1 = [len(r),len(g),len(b)]
n3 = len1.index(max(len1))
if n3 == 0:
    n1,n2 = 1,2
elif n3 == 1:
    n1,n2 = 0,2
else:
    n1,n2 = 0,1
# print(len1)
# print(n1,n2,n3)

for i in rgb[n1]:
    for k in rgb[n2]:
        y = len(rgb[n3])
        d1 = max([abs(i-k)+i,abs(i-k)+k])
        d2 = min([i-abs(i-k),k-abs(i-k)])
        if (i+k)/2 in rgb[n3]:
            y -=1
        if d1 in rgb[n3]:
            y-=1
        if d2 in rgb[n3]:
            y-=1
        kotae += y
print(kotae)

Submission status: Time Limit Exceeded

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

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

print(count)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
 
x1 = int(input())
x2 = input()
RGB = {"R", "G", "B"}
 
print(sum([1 for i, rgb in zip(combinations(range(x1), 3), combinations(x2, 3)) if set(rgb) == RGB and (i[1] - i[0]) != (i[2] - i[1])]))