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

from collections import Counter
c = Counter(S)
count_all =c["R"] * c["G"] * c["B"]
count =0
for i,v in enumerate(S):
    for j in range(1,(N+1)//2):
        if  i+j*2 <N: 
            if S[i]=="R" and S[i+j] =="G" and S[i+j*2] =="B":
                #print(i-j,i,i+j)
                count +=1
            if S[i]=="R" and S[i+j] =="B" and S[i+j*2] =="G":
                #print(i-j,i,i+j)
                count +=1
            if S[i]=="G" and S[i+j] =="R" and S[i+j*2] =="B":
                #print(i-j,i,i+j)
                count +=1
            if S[i]=="G" and S[i+j] =="B" and S[i+j*2] =="R":
                #print(i-j,i,i+j)
                count +=1
            if S[i]=="B" and S[i+j] =="G" and S[i+j*2] =="R":
                #print(i-j,i,i+j)
                count +=1
            if S[i]=="B" and S[i+j] =="R" and S[i+j*2] =="G":
                count +=1
                #print(i-j,i,i+j)

print(count_all - count)

Submission status: Time Limit Exceeded

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

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

ans = len(R)*len(G)*len(B)
for r in R:
    for g in G:
        i, j = r, g
        if i > j:
            i, j = j, i
        
        s = j-i
        t1 = i-s
        t2 = -1
        if s%2 == 0:
            t2 = i+s//2
        t3 = j+s
        
        k = 0
        while k < len(B) and B[k] < t1:
            k += 1
        if k == len(B):
            continue
        if B[k] == t1:
            ans -= 1
        
        if t2 != -1:
            while k < len(B) and B[k] < t2:
                k += 1
            if k == len(B):
                continue
            if B[k] == t2:
                ans -= 1

        while k < len(B) and B[k] < t3:
            k += 1
        if k == len(B):
            continue
        if B[k] == t3:
            ans -= 1

# output
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# 初期入力
N= int(input())
S =input()

from collections import Counter
c = Counter(S)
count_all =c["R"] * c["G"] * c["B"]
count =0
for i,v in enumerate(S):
    for j in range(1,(N+1)//2):
        if 0 <= i-j and i+j <N: 
            if S[i-j]=="R" and S[i] =="G" and S[i+j] =="B":
                #print(i-j,i,i+j)
                count +=1
            if S[i-j]=="R" and S[i] =="B" and S[i+j] =="G":
                #print(i-j,i,i+j)
                count +=1
            if S[i-j]=="G" and S[i] =="R" and S[i+j] =="B":
                #print(i-j,i,i+j)
                count +=1
            if S[i-j]=="G" and S[i] =="B" and S[i+j] =="R":
                #print(i-j,i,i+j)
                count +=1
            if S[i-j]=="B" and S[i] =="G" and S[i+j] =="R":
                #print(i-j,i,i+j)
                count +=1
            if S[i-j]=="B" and S[i] =="R" and S[i+j] =="G":
                count +=1
                #print(i-j,i,i+j)

print(count_all - count)

Submission status: Time Limit Exceeded

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

answer = 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[j-1] != S[k-1] and S[k-1] != S[i-1]:
                if j - i != k - j:
                    answer += 1
                else:
                    pass
            else:
                pass
print(answer)

Submission status: Time Limit Exceeded

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



n=S.count("R")*S.count("G")*S.count("B")
count=0
for l in range(N-2):
    for r in range(l+2,N):
        m=(l+r)/2
        if m%1.0!=0:
            continue
        m=int(m)
        tmp =[S[l], S[r], S[m]]
        if len(set(tmp))==3:
            count+=1
            
            

print(n-count)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

def D_RGB_Triplets(N, S):
    answer = 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[j-1] != S[k-1] and S[k-1] != S[i-1]:
                    if j - i != k - j:
                        answer += 1
                    else:
                        pass
                else:
                    pass
    return answer
print(D_RGB_Triplets(n, s))

Submission status: Time Limit Exceeded

Solution code:
from itertools import product
 
x1 = int(input())
x2 = input()
 
R = [i for i, x in enumerate(x2) if x == "R"]
G = [i for i, x in enumerate(x2) if x == "G"]
B = [i for i, x in enumerate(x2) if x == "B"]
 
group = map(sorted, product(R, G, B))
voter = [(i[1] - i[0]) != (i[2] - i[1]) for i in group]
print(sum(voter))

Submission status: Time Limit Exceeded

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

N = INT()
S = input()

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(R, G, B)

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

count = 0

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

Submission status: Time Limit Exceeded

Solution code:
# 初期入力
N= int(input())
S =input()


#
R_index=[]
G_index=[]
B_index=[]
for i,v in enumerate(S):
    if v =="R":
        R_index.append(i)
    if v =="G":
        G_index.append(i)
    if v =="B":
        B_index.append(i)

count_3jurupe =0
for i in R_index:
    for j in G_index:
        for k in B_index:
            if abs(i-j) == abs(j-k) or abs(i-k) == abs(j-k) or abs(i-j) == abs(i-k):
                count_3jurupe +=1
                #print(i,j,k,end=" ,")

#選んだ文字が３つとも違う＝RGB（全体）
a = len(R_index) * len(G_index) * len(B_index) 

kotae =a -count_3jurupe
#print()
print(kotae)

Submission status: Time Limit Exceeded

Solution code:
# D - RGB Triplets

def get_rgb():
	i = 1
	
	for s in S:
		if s == 'R':
			r_list.append(i)
		elif s == 'G':
			g_list.append(i)
		else:
			b_list.append(i)
		
		i += 1
# -----------------------------


def fix_i_j():
	i = min(r, g)
	j = max(r, g)
	k = j * 2 - i
	
	global ans
	
	if k in b_list:
		ans -= 1


def fix_i_k():
	i = min(r, g)
	k = max(r, g)
	j = (i + k) / 2
	
	global ans
	
	if j in b_list:
		ans -= 1


def fix_j_k():
	j = min(r, g)
	k = max(r, g)
	i = j * 2 - k
	
	global ans
	
	if i in b_list:
		ans -= 1


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

r_list = []
g_list = []
b_list = []
get_rgb()

ans = len(r_list) * len(g_list) * len(b_list)

for r in r_list:
	for g in g_list:
		fix_i_j()
		fix_i_k()
		fix_j_k()
		 
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
# D

import sys
# 再起回数上限変更
# sys.setrecursionlimit(1000000)
from bisect import bisect_left

N = int(input())
S = input()
R = []
G = []
B = []


for idx, s in enumerate(S):
    if s == 'R':
        R.append(idx+1)
    if s == 'G':
        G.append(idx+1)
    if s == 'B':
        B.append(idx+1)
# print(R, G, B)

if (len(R) > 0) and (len(G) > 0) and (len(B) > 0):
    pass
else:
    print(0)
    sys.exit()
    
R0, G0, B0 = R, G, B

sum = 0

for i in range(6):
    if i == 0:
        R, G, B, temp = R0, G0, B0, 'B'
    elif i == 1:
        R, G, B, temp = R0, B0, G0, 'G'
    elif i == 2:
        R, G, B, temp = B0, R0, G0, 'G'
    elif i == 3:
        R, G, B, temp = B0, G0, R0, 'R'
    elif i == 4:
        R, G, B, temp = G0, R0, B0, 'B'
    elif i == 5:
        R, G, B, temp = G0, B0, R0, 'R'

    for s in R:
        if s > G[-1]:
            break
        idx1 = bisect_left(G, s)
        for g in G[idx1:]:
            if g > B[-1]:
                break
            idx2 = bisect_left(B, g)
            sum += len(B[idx2:])
            if 2 * g - s <= N:
                if S[2 * g - s - 1] == temp:
                    sum -= 1

print(sum)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s =input()
s = "0" + s
ans = 0
 
for i in range(1, n - 1):
    for j in range(i + 1 , n):
        if s[i] == s[j]:
            continue
        elif (s[i] == "R" and s[j] == "G") or (s[i] == "G" and s[j] == "R"):
            ans += s.count("B", j , n + 1)
            if 2 * j - i <= n:
                if s[2 *j - i] == "B":
                    ans -=1
        elif (s[i] == "R" and s[j] == "B") or (s[i] == "B" and s[j] == "R"):
            ans += s.count("G", j , n + 1)
            if 2 * j - i <= n:
                if s[2 *j - i] == "G":
                    ans -=1
        elif (s[i] == "B" and s[j] == "G") or (s[i] == "G" and s[j] == "B"):
            ans += s.count("R", j , n + 1)
            if 2 * j - i <= n:
                if s[2 *j - i] == "R":
                    ans -=1
print(ans)

Submission status: Time Limit Exceeded

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

r = []
g = []
b = []
counter = 0

for x in range(n):
  if s[x] == 'R':
    r.append(x)
  elif s[x] == 'G':
    g.append(x)
  else:
    b.append(x)
    
for i in range(len(r)):
  for j in range(len(g)):
    for k in range(len(b)):
      if r[i]-g[j] != g[j]-b[k] and g[j]-b[k] != b[k]-r[i] and b[k]-r[i] != r[i]-g[j]:
        counter += 1
print(counter)

Submission status: Time Limit Exceeded

Solution code:
from itertools import product
 
x1 = int(input())
x2 = input()
 
R = [i for i, x in enumerate(x2) if x == "R"]
G = [i for i, x in enumerate(x2) if x == "G"]
B = [i for i, x in enumerate(x2) if x == "B"]
 
group = map(sorted, product(R, G, B))
voter = [1 for i in group if (i[1] - i[0]) != (i[2] - i[1])]
print(sum(voter))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N = int(input())
S = str(input())
S += "x"*4001
c = Counter(S)
after_happen = {}
after_happen[0] = c.copy()
after_happen[0][S[0]] -= 1

for i in range(1, N):
    after_happen[i] = after_happen[i-1].copy()
    after_happen[i][S[i]] -= 1
# print(after_happen)
ans = 0
rgb = set(["R", "G", "B"])
for i in range(N-2):
    for j in range(i+1, N-1):
        # 同じ文字ならcontinue
        if S[i] == S[j]:
            continue
        # jより後ろにtargetがいくつ存在するか確認する, after_happen[j] := jより後ろの情報
        target = rgb - set([S[i], S[j]])
        target = list(target)[0]
        ans += after_happen[j][target]
        if S[j+(j-i)] == target:
            ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
from itertools import combinations
nums = list(range(1,n+1))
p_list = list(combinations(nums, 3))

def remove_ele(tmp):
    final = []
    for i in tmp:
        if (i[1] - i[0]) != (i[2] - i[1]):
            final.append(i)
    return final
        
def spell_check(a,b,c):
    if a != b and b != c and c != a:
        return True
    else:
        return False
    
final_nums = remove_ele(p_list)
count = 0
for ele in final_nums:
    count += spell_check(s[ele[0]-1], s[ele[1]-1], s[ele[2]-1])
print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from itertools import product

x1 = int(input())
x2 = input()

R = [i for i, x in enumerate(x2) if x == "R"]
G = [i for i, x in enumerate(x2) if x == "G"]
B = [i for i, x in enumerate(x2) if x == "B"]

group = [sorted(i) for i in product(R, G, B)]
voter = [1 for i in group if (i[1] - i[0]) != (i[2] - i[1])]
print(sum(voter))

Submission status: Time Limit Exceeded

Solution code:
from itertools import product

x1 = int(input())
x2 = input()

R = [i for i, x in enumerate(x2) if x == "R"]
G = [i for i, x in enumerate(x2) if x == "G"]
B = [i for i, x in enumerate(x2) if x == "B"]

_sum = 0
for i in product(R, G, B):
    group = sorted(i)
    if (group[1] - group[0]) != (group[2] - group[1]):
        _sum += 1
print(_sum)

Submission status: Time Limit Exceeded

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

N = INT()
S = input()

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(R, G, B)

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

count = 0
for i in R:
    for j in G:
        num = max(i, j) - min(i, j)
        num = num + max(i, j)
        if num in B:
            count += 1

for i in R:
    for j in B:
        num = max(i, j) - min(i, j)
        num = num + max(i, j)
        if num in G:
            count += 1

for i in G:
    for j in B:
        num = max(i, j) - min(i, j)
        num = num + max(i, j)
        if num in R:
            count += 1


print(ans - count)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

counter = Counter(S)
r_count = counter['R']
g_count = counter['G']
b_count = counter['B']

ans = r_count * g_count * b_count
for i in range(N):
    for j in range(N):
        k = 2 * j - i
        if k > N-1:
            break
        if i < j < k and 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()

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(r,g,b)
ans = 0

for rr in r:
    for gg in g:
        for bb in b:
            if abs(gg-bb) != abs(rr-gg) and abs(rr-bb) != abs(gg-bb) and abs(rr-gg) != abs(rr-bb):
                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)
    elif S[i] == 'B':
        B.append(i)
ans = len(R)*len(G)*len(B)
for i in R:
    for j in G:
        if min(i,j)-abs(i-j) in B:
            ans -= 1
        if max(i,j)+abs(i-j) in B:
            ans -= 1
        if (i-j) % 2 == 0:
            if min(i,j)+abs(i-j)//2 in B:
                ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from statistics import median
length = int(input())
text = list(input())

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

count = 0
for i, c in enumerate(text):
    if c == "R":
        r_list.append(i)
    elif c == "G":
        g_list.append(i)
    else:
        b_list.append(i)

num_list = [0, 0, 0]
for i in r_list:
    num_list[0] = i
    for j in g_list:
        num_list[1] = j
        for k in b_list:
            num_list[2] = k
            d1 = max(num_list) - median(num_list)
            d2 = median(num_list) - min(num_list)
            if d1 != d2:
                count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
x = [0,0,0]
for i in range(n):
    x[ord(s[i])%3] += 1
 
ans = x[0]*x[1]*x[2]

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

if __name__ == '__main__':
    input = sys.stdin.readline
    n = int(input())
    s = list(input())
    s.remove('\n')
    r = 0
    g = 0
    b = 0
    for i in s:
        if i == "R":
            r += 1
        elif i == "G":
            g += 1
        else:
            b += 1
    ans = r * g * b
    for i in range(n):
        for j in range(i, n):
            out = j * 2 - i
            if out >= n:
                continue
            if len({s[i], s[j], s[out]}) == 3:
                ans -= 1
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
#template
def inputlist(): return [int(k) for k in input().split()]
#template
N = int(input())
S = list(input())
from collections import Counter
c = Counter(S)
ans = c['R']*c['B']*c['G']
for i in range(N-1):
    for j in range(i+1,N):
        if 2*j - i >= N:
            continue
        se = [S[i],S[j],S[2*j-i]]
        se = set(se)
        if len(se) == 3:
            ans -=1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect
import sys
if __name__ == '__main__':
    n = int(input())
    s = list(input())
    r = 0
    g = 0
    b = 0
    for i in s:
        if i == "R":
            r += 1
        elif i == "G":
            g += 1
        else:
            b += 1
    ans = r * g * b
    for i in range(n):
        for j in range(i, n):
            out = j * 2 - i
            if out >= n:
                continue
            if len({s[i], s[j], s[out]}) == 3:
                ans -= 1
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
count=0
n=int(input())
l=list(map(str,input()))
i=0
while i<=n-3:
    j=i+1
    while i+1<=j and j<=n-2:
        if l[i]==l[j]:
            j+=1
        else:
            k=j+1
            while j+1<=k and k<=n-1:
                if l[i]!=l[j] and l[j]!=l[k] and l[i]!=l[k] and k-j!=j-i:
                    count+=1
                    k+=1
                else:
                    k+=1
            j+=1
    i+=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)
r=len(R)
g=len(G)
b=len(B)

c=0
for i in R:
	for j in G:
		d=abs(i-j)
		min_n=min(i,j)
		max_n=max(i,j)
		if max_n+d in B:
			c+=1
		if (i+j)%2==0 and (i+j)//2 in B:
			c+=1
		if min_n-d in B:
			c+=1

print(r*g*b-c)

Submission status: Time Limit Exceeded

Solution code:
# D - RGB Triplets

def get_rgb():
	i = 1
	
	for s in S:
		if s == 'R':
			r_list.append(i)
		elif s == 'G':
			g_list.append(i)
		else:
			b_list.append(i)
		
		i += 1
# -----------------------------


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

r_list = []
g_list = []
b_list = []
get_rgb()

ans = 0

for r in r_list:
	for g in g_list:
		for b in b_list:
			list = [r, g, b]
			list.sort()
			i, j, k = list
			
			if j - i != k -j:
				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)
r=len(R)
g=len(G)
b=len(B)

c=0
for i in R:
	for j in B:
		d=abs(i-j)
		min_n=min(i,j)
		max_n=max(i,j)
		if max_n+d in G:
			c+=1
		if (i+j)%2==0 and (i+j)//2 in G:
			c+=1
		if min_n-d in G:
			c+=1

print(r*g*b-c)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = str(input())
Answer = 0
rList,gList,bList = list(),list(),list()
for n in range(N):
    if S[n] == "R":
        rList.append(n)
    if S[n] == "G":
        gList.append(n)
    if S[n] == "B":
        bList.append(n)

for r in rList:
    for g in gList:
        for b in bList:
            if ((b-g)**2 == (g-r)**2) or ((b-r)**2 == (g-b)**2) or ((b-r)**2 == (g-r)**2):
                pass
            else:
                Answer += 1



print(Answer)

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)
r=len(R)
g=len(G)
b=len(B)

c=0
for i in R:
	for j in G:
		d=abs(i-j)
		min_n=min(i,j)
		max_n=max(i,j)
		if max_n+d in B:
			c+=1
		if (i+j)%2==0 and (i+j)//2 in B:
			c+=1
		if min_n-d in B:
			c+=1

print(r*g*b-c)

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)
r=len(R)
g=len(G)
b=len(B)

c=0
for i in R:
	for j in G:
		d=abs(i-j)
		min_n=min(i,j)
		max_n=max(i,j)
		if max_n+d in B:
			c+=1
		if (i+j)/2 in B:
			c+=1
		if min_n-d in B:
			c+=1

print(r*g*b-c)

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)
r=len(R)
g=len(G)
b=len(B)

c=0
for i in R:
	for j in G:
		d=abs(i-j)
		min_n=min(i,j)-d
		max_n=max(i,j)+d
		if max_n in B:
			c+=1
		if (i+j)/2 in B:
			c+=1
		if min_n in B:
			c+=1

print(r*g*b-c)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: print('Yes' if b else 'No')
YESNO=lambda b: print('YES' if b else 'NO')

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = str(input())
Answer = 0
for r in range(N):
    if S[r] == "R":
        for g in range(N):
            if S[g] == "G":
                for b in range(N):
                    if S[b] != "B" or ((b-g)**2 == (g-r)**2) or ((b-r)**2 == (g-b)**2) or ((b-r)**2 == (g-r)**2):
                        pass
                    else:
                        Answer += 1

print(Answer)

Submission status: Time Limit Exceeded

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

R=[0]*n
G=[0]*n
B=[0]*n

for i in range(n):
    if s[i]=='R':
        R[i]=1
    elif s[i]=='G':
        G[i]=1
    elif s[i]=='B':
        B[i]=1

ans=0
for i in range(n):
    if R[i]==1:
        lG=sum(G[:i])
        lB=sum(B[:i])
        rG=sum(G[i:])
        rB=sum(B[i:])
        ans+=(lG+rG)*(lB+rB)
        for j in range(1,n//2+1):
            if R[i]==1:
                if 0<=i-j<n and 0<=i+j<n:
                    if G[i-j]==1 and B[i+j]==1:
                        ans-=1
                    if B[i-j]==1 and G[i+j]==1:
                        ans-=1
                if 0<=i-2*j<n:
                    if G[i-j]==1 and B[i-2*j]==1:
                        ans-=1
                    if B[i-j]==1 and G[i-2*j]==1:
                        ans-=1
                if 0<=i+2*j<n:
                    if G[i+j]==1 and B[i+2*j]==1:
                        ans-=1
                    if B[i+j]==1 and G[i+2*j]==1:
                        ans-=1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
#D
#入力
N=int(input())
S=input()


#デバッグ用
def pront(*n):
    if debug==1:
        print(n)
        
debug=0#0:本番,1:デバッグ

#処理
R=[]
G=[]
B=[]
for i in range(N):
    if S[i]=="R":
        R.append(i)
    elif S[i]=="G":
        G.append(i)
    elif S[i]=="B":
        B.append(i)
        
pront(len(R),len(G),len(B))
r=len(R)
g=len(G)
b=len(B)

if min(r,g,b)==r:
    P=R
    if g<b:
        Q=G
        S=B
    else:
        Q=B
        S=G
elif min(r,g,b)==g:
    P=G
    if r<b:
        Q=R
        S=B
    else:
        Q=B
        S=R
else:
    P=B
    if r<g:
        Q=R
        S=G
    else:
        Q=G
        S=R
        
y=0
for i in P:
    for j in Q:
        t=0
        l=abs(i-j)-1
        if min(i,j)-l-1 in S:
            t-=1
        if max(i,j)+l+1 in S:
            t-=1
        if l%2==1:
            if (i+j)//2 in S:
                t-=1
        y+=len(S)+t
print(y)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=str(input())
t=N*(N-1)*(N-2)//6
for i in range(N):
    for j in range(i+1,N):
        if S[i]==S[j]:
            t=t-(N-j-1)
        else:
            for k in range(j+1,N):
                if S[j]!=S[k] and S[i]!=S[k] and (i-j)!=(j-k):
                    continue
                else:
                    t=t-1
print (t)

Submission status: Time Limit Exceeded

Solution code:
#D
#入力
N=int(input())
S=input()


#デバッグ用
def pront(*n):
    if debug==0:
        print(n)
        
debug=1#0:本番,1:デバッグ

#処理
R=[]
G=[]
B=[]
for i in range(N):
    if S[i]=="R":
        R.append(i)
    elif S[i]=="G":
        G.append(i)
    elif S[i]=="B":
        B.append(i)
        
pront(R,G,B)
y=0
for i in R:
    for j in G:
        for k in B:
            if abs(i-j)==abs(i-k) or abs(j-k)==abs(j-i) or abs(i-k)==abs(j-k):
                continue
            y+=1
print(y)

Submission status: Time Limit Exceeded

Solution code:
#D
#入力
N=int(input())
S=input()


#デバッグ用
def pront(*n):
    if debug==1:
        print(n)
        
debug=0#0:本番,1:デバッグ

#処理
R=[]
G=[]
B=[]
for i in range(N):
    if S[i]=="R":
        R.append(i)
    elif S[i]=="G":
        G.append(i)
    elif S[i]=="B":
        B.append(i)
        
pront(R,G,B)
y=0
for i in R:
    for j in G:
        for k in B:
            x=[i,j,k]
            x.sort()
            if not(x[1]-x[0]==x[2]-x[1]):
                y+=1
print(y)

Submission status: Time Limit Exceeded

Solution code:
d = input()
s = input()

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

res = r*g*b
for i in range(len(s)-2):
    for j in range(i,len(s)-1):
        for k in range(j, len(s)):
            if (j - i) == (k - j):
                if s[i] != s[k] and s[i] != s[j] and s[j] != s[k]:
                    res -= 1
print(res)

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)

lenr=len(r)
leng=len(g)
lenb=len(b)

isum=0
for ir in r:
    for ig in g:
        if (ir+ig)/2 in b: 
            isum=isum+1
for ir in r:
    for ib in b:
        if (ir+ib)/2  in g: 
            isum=isum+1
for ig in g:
    for ib in b:
        if (ig+ib)/2  in r: 
            isum=isum+1

isum=lenr*leng*lenb-isum
                
print(isum)

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)

lenr=len(r)
leng=len(g)
lenb=len(b)

isum=0
for ir in r:
    for ig in g:
        if (ir+ig)/2 in b: 
            isum=isum+1
for ir in r:
    for ib in b:
        if (ir+ib)/2  in g: 
            isum=isum+1
for ig in g:
    for ib in b:
        if (ig+ib)/2  in r: 
            isum=isum+1

isum=lenr*leng*lenb-isum
                
print(isum)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
a=S.count('R')
b=S.count('G')
c=S.count('B')
ans=a*b*c
for i in range(N-2):
  for j in range(i+1,N-1):
    if S[i]!=S[j]:
      if (2*j-i)<=N-1:
        if S[i]!=S[2*j-i]!=S[j]:
          ans-=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
a=S.count('R')
b=S.count('G')
c=S.count('B')
ans=a*b*c
for i in range(N):
  for j in range(i+1,N-1):
    if S[i]!=S[j]:
      if (2*j-i)<=N-1:
        if S[i]!=S[2*j-i]!=S[j]:
          ans-=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
a=S.count('R')
b=S.count('G')
c=S.count('B')
ans=a*b*c
for i in range(N):
  for j in range(i+1,N):
    if S[i]!=S[j]:
      if (2*j-i)<=N-1:
        if S[i]!=S[2*j-i] and S[j]!=S[2*j-i]:
          ans-=1
print(ans)

Submission status: Time Limit Exceeded

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

print(c)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=list(str(input()))
ans=0
for i in range(N):
  for j in range(i+1,N-1):
    if S[i] != S[j]:
      for k in range(j+1,N):
        if 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=list(input())
 
def rgb(now,sec):
    if now=='R' and sec=='G':
        return 'B'
    if now == 'R' and sec == 'B':
        return 'G'
    if now == 'G' and sec == 'R':
        return 'B'
    if now == 'G' and sec == 'B':
        return 'R'
    if now == 'B' and sec == 'G':
        return 'R'
    if now == 'B' and sec == 'R':
        return 'G'
 
ans=0
for i in range(N-2):
    now = S[i]
    for j in range(i+1,N-1):
        if S[j] == now:
            continue
        else:
            sec = S[j]
            thi = rgb(now,sec)
            ans += S[j+1:].count(thi)
            if j+(j-i) < N and S[j+(j-i)]==thi:
                ans-=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:

s = input()
n=int(s)
s = input()
ans=0
r=0
g=0
b=0
for i in s:
    if 'R'==i:
        r+=1
    if 'G' == i:
        g += 1
    if 'B' == i:
        b += 1
for i in range(n):
    for j in range(i,n):
        k=2*j-i
        if k<n:
            o=s[i]+s[j]+s[k]
            if 'R' in o and 'G' in o and 'B' in o:
                ans+=1
print(r*g*b-ans)