Problem:


Score : 400 points

Problem Statement

We have a string S of length N consisting of R, G, and B.

Find the number of triples (i,~j,~k)~(1 \leq i < j < k \leq N) that satisfy both of the following conditions:

S_i \neq S_j, S_i \neq S_k, and S_j \neq S_k.

j - i \neq k - j.

Constraints

1 \leq N \leq 4000

S is a string of length N consisting of R, G, and B.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of triplets in question.

Sample Input 1

4
RRGB

Sample Output 1

1

Only the triplet (1,~3,~4) satisfies both conditions. The triplet (2,~3,~4) satisfies the first condition but not the second, so it does not count.

Sample Input 2

39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB

Sample Output 2

1800



Sample input:
4
RRGB


Sample output:
1

Submission status: Time Limit Exceeded

Solution code:
import math
from itertools import product
from collections import Counter


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def sums(a, N):
    ans = ((N//a)*((N//a)+1)//2)*a
    return ans


colors = set(['R', 'G', 'B'])

    

def solve():
    N = read_int()
    S = input().strip()
    right = Counter(S)
    left = Counter()
    total = 0
    for i in range(N):
        take = S[i]
        right[take] -= 1
        #
        for j in range(i):
            if S[j] != S[i]:
                color = (colors-set([S[j], S[i]])).pop()
                k = 2*i-j
                total += right[color]
                if k < N and S[k] == color:
                    total -= 1
        #
        left[take] += 1
    return total


if __name__ == '__main__':
    print(solve())

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

def binary_search(A,n):
    find=False
    start=0
    end=len(A)
    if len(A)==1 and A[0]==n:
        find=True
    elif A[0]==n:
        find=True
    else:
        while find==False and start<end-1:
            middle=(start+end)//2
            if n>A[middle]:
                start=middle
            elif n<A[middle]:
                end=middle
            else:
                find=True
    return find

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

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

REMOVE=0
for r in R:
    for g in G:
        if (r+g)%2==0:
            candidate=[2*r-g,(r+g)//2,2*g-r]
        else:
            candidate=[2*r-g,2*g-r]
        
        for e in candidate:
            #if binary_search(B,e)==True:
            if e in B:
                REMOVE+=1

print(EVERY-REMOVE)

Submission status: Time Limit Exceeded

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

count=0
for i in range(len(R)):
    for j in range(len(G)):

        if max(R[i],G[j])+abs(R[i]-G[j]) in B:
            count+=1

        if min(R[i],G[j])-abs(R[i]-G[j]) in B:
            count+=1

        if (R[i]+G[j])/2 in B:
            count+=1


print(str((len(R)*len(G)*len(B))-count))

Submission status: Time Limit Exceeded

Solution code:
N=input()
S=input()
Sn=[]
R=0
G=0
B=0
for i in range(len(S)):
    if S[i]=='R':
        R+=1
        Sn.append(1)
    elif S[i]=='G':
        G+=1
        Sn.append(2)
    else:
        B+=1
        Sn.append(3)

count=0
for start in range(len(S)):
        for diff in range(1,int((len(S)-start+1)/2)):
            if (Sn[start]-Sn[start+diff])*(Sn[start+diff]-Sn[start+(2*diff)])*(Sn[start]-Sn[start+(2*diff)])!=0: 
                count+=1
                
print(str((R*G*B)-count))

Submission status: Time Limit Exceeded

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

count=0
for i in range(len(R)):
    for j in range(len(G)):

        if max(R[i],G[j])+abs(R[i]-G[j]) in B:
            count+=1

        if min(R[i],G[j])-abs(R[i]-G[j]) in B:
            count+=1

        if (R[i]+G[j])/2 in B:
            count+=1


print(str((len(R)*len(G)*len(B))-count))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import itertools 
import math
import itertools 
import sys

input = sys.stdin.readline


N = int(input())

S = input()

color =list("RGB")

#色がRGBの３色ないとき
for x in color:
    if x not in S:
        print(0)
        exit()

r = 0
g = 0
b = 0
for i in range(len(S)):
    if S[i] == color[0]:
        r+=1
    elif S[i] == color[1]:
        g+=1
    elif S[i] == color[2]:
        b+=1

all = r*g*b

sub = 0
for i in range(N):
    for j in range(i+1,N):
        k = 2*j - i
        #kがN以上の位置にある
        if k >= N:
            continue

        #同じ色がある
        if S[i] == S[j] or S[j] == S[k] or S[k] == S[i] :
            continue
        sub+=1

print(all - sub)

Submission status: Time Limit Exceeded

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

for x in range(n):
  if s[x]=='R':
    r.append(x)
  if s[x]=='G':
    g.append(x)
  if s[x]=='B':
    b.append(x)

for i in r:
  si = s[i]
  for j in g:
    sj = s[j]
    if i>j or sj==si:
      continue
    for k in b:
      sk = s[k]
      if j>k or sk==si or sk==sj:
        continue
      if j-i != k-j:
        result += 1

print(result)

Submission status: Time Limit Exceeded

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

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

print(result)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
in_list = []
for i in range(2):
    in_list.append(input())
N,S = in_list
N = int(N)

filt = []
for i in range(1,N//2+1):
    a = [0,1*i,2*i] + np.array([np.arange(N-2*i)]).T
    filt.extend(a)

res = 0
for i in filt:
    res += (S[i[0]] != S[i[1]] and S[i[1]] != S[i[2]] and S[i[2]] != S[i[0]])
    
rgb = S.count("R") * S.count("G") * S.count("B")
print(rgb - res)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
in_list = []
for i in range(2):
    in_list.append(input())
N,S = in_list
N = int(N)

find_ind = []
for i in ['R','G','B']:
    find = (np.array(list(S)) == i)
    find_ind.append(find.argsort()[len(find)-sum(find):])

filt = []
for i in range(1,N//2+1):
    a = [0,1*i,2*i] + np.array([np.arange(N-2*i)]).T
    filt.extend(a)

res = 0
for i in filt:
    res += (S[i[0]] != S[i[1]] and S[i[1]] != S[i[2]] and S[i[2]] != S[i[0]])
    
prod_rgb = len(find_ind[0])*len(find_ind[1])*len(find_ind[2])
print(prod_rgb - res)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import math

n = int(input())
s = list(map(str, input()))

r = np.array([0]*n)
g = np.array([0]*n)
b = np.array([0]*n)

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

ans = np.sum(r) * np.sum(g) * np.sum(b)

rgb = np.array([r,g,b])

for i in range(n):
    for j in range(i+1,math.ceil((n+i)/2)):
        if (rgb[0,i]==1)and(rgb[1,j]==1)and(rgb[2,2*j-i]==1):
            ans -= 1
        if (rgb[0,i]==1)and(rgb[2,j]==1)and(rgb[1,2*j-i]==1):
            ans -= 1
        if (rgb[1,i]==1)and(rgb[0,j]==1)and(rgb[2,2*j-i]==1):
            ans -= 1
        if (rgb[1,i]==1)and(rgb[2,j]==1)and(rgb[0,2*j-i]==1):
            ans -= 1
        if (rgb[2,i]==1)and(rgb[0,j]==1)and(rgb[1,2*j-i]==1):
            ans -= 1
        if (rgb[2,i]==1)and(rgb[1,j]==1)and(rgb[0,2*j-i]==1):
            ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import math

n = int(input())
s = list(map(str, input()))

r = np.array([0]*n)
g = np.array([0]*n)
b = np.array([0]*n)

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

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

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

ans = np.sum(r) * np.sum(g) * np.sum(b)

rgb = np.array([r,g,b])

for i in range(n):
    for j in range(i+1,math.ceil((n+i)/2)):
        if (rgb[0,i]==1)and(rgb[1,j]==1)and(rgb[2,2*j-i]==1):
            ans -= 1

for i in range(n):
    for j in range(i+1,math.ceil((n+i)/2)):
        if (rgb[0,i]==1)and(rgb[2,j]==1)and(rgb[1,2*j-i]==1):
            ans -= 1

for i in range(n):
    for j in range(i+1,math.ceil((n+i)/2)):
        if (rgb[1,i]==1)and(rgb[0,j]==1)and(rgb[2,2*j-i]==1):
            ans -= 1

for i in range(n):
    for j in range(i+1,math.ceil((n+i)/2)):
        if (rgb[1,i]==1)and(rgb[2,j]==1)and(rgb[0,2*j-i]==1):
            ans -= 1

for i in range(n):
    for j in range(i+1,math.ceil((n+i)/2)):
        if (rgb[2,i]==1)and(rgb[0,j]==1)and(rgb[1,2*j-i]==1):
            ans -= 1

for i in range(n):
    for j in range(i+1,math.ceil((n+i)/2)):
        if (rgb[2,i]==1)and(rgb[1,j]==1)and(rgb[0,2*j-i]==1):
            ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

n = int(input())
s = str(input())
s_count = Counter(list(s))

ans = 1
count = 0

for i, j in s_count.items():
    count += 1
    ans *= j

if count < 3:
    ans = 0
    

for i in range(n):
    k = 1

    while i + 2 * k < n:
        l = [s[i], s[i+k], s[i+2*k]]
        if l.count('R') == 1 and l.count('G') == 1 and l.count('B') == 1:
            ans -= 1
        
        k += 1
    
print(ans)

Submission status: Time Limit Exceeded

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

s = list(s)

count = 0

for i in range(0,len(s)):
    for j in range(i,len(s)):
        for k in range(j,len(s)):
            if(j-i != k-j) and (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:
import sys
N = int(sys.stdin.readline())
S = list(sys.stdin.readline().rstrip())
R = [i for i, x in enumerate(S) if x == "R"]
G = [j for j, y in enumerate(S) if y == "G"]
B = [k for k, z in enumerate(S) if z == "B"]
B_C = len(B)
ans = 0
for r in R:
    for g in G:
        ans += B_C
        cM = 2 * max(r, g) - min(r, g)
        cm = 2 * min(r, g) - max(r, g)
        if cM in B:
            ans -= 1
        if cm in B:
            ans -= 1
        if (r + g) % 2 == 0:
            cT = int((r + g) / 2)
            if cT in B:
                ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
N = int(sys.stdin.readline())
S = list(sys.stdin.readline().rstrip())
R = [i for i, x in enumerate(S) if x == "R"]
G = [j for j, y in enumerate(S) if y == "G"]
B = [k for k, z in enumerate(S) if z == "B"]
ans = 0
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]:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

c = 0
for i in range(1,n//2 +1):
  for j in range(n):
    if j + 2*i < n:
      if a[j] != a[j+i] and a[j+i] != a[j+2*i] and a[j] != a[j+2*i]:
        c+=1

ans = a.count("R") * a.count("G") * a.count("B") - c
print(ans)

Submission status: Time Limit Exceeded

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

c = 0
for i in range(n):
  for j in range(n):
    if j + 2*i < n:
      if a[j] != a[j+i] and a[j+i] != a[j+2*i] and a[j] != a[j+2*i]:
        c+=1

ans = a.count("R") * a.count("G") * a.count("B") - c
print(ans)

Submission status: Time Limit Exceeded

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

count = 0


def rgb(i, j):
    if i == 'R' and j == 'G':
        return 'B'
    if i == 'R' and j == 'B':
        return 'G'
    if i == 'G' and j == 'R':
        return 'B'
    if i == 'G' and j == 'B':
        return 'R'
    if i == 'B' and j == 'R':
        return 'G'
    if i == 'B' and j == 'G':
        return 'R'


for i in range(N -2):
    rgb_i = S[i]
    for j in range(i, N -1):
        rgb_j = S[j]
        if rgb_i == rgb_j:
            continue
        rgb_k = rgb(rgb_i, rgb_j)
        count += S.count(rgb_k, j + 1)
        if j + j -i < N and S[j + (j-i)] == rgb_k:
            count = count -1

print(count)

Submission status: Time Limit Exceeded

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

count = 0


def rgb(i, j):
    if i == 'R' and j == 'G':
        return 'B'
    if i == 'R' and j == 'B':
        return 'G'
    if i == 'G' and j == 'R':
        return 'B'
    if i == 'G' and j == 'B':
        return 'R'
    if i == 'B' and j == 'R':
        return 'G'
    if i == 'B' and j == 'G':
        return 'R'


for i in range(N):
    rgb_i = S[i]
    for j in range(i, N):
        rgb_j = S[j]
        if rgb_i == rgb_j:
            continue
        rgb_k = rgb(rgb_i, rgb_j)
        count += S.count(rgb_k, j + 1)
        if j + j -i < N and S[j + (j-i)] == rgb_k:
            count = count -1

print(count)

Submission status: Time Limit Exceeded

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

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

print(count)

Submission status: Time Limit Exceeded

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

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

print(c)

Submission status: Time Limit Exceeded

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

r = S.count('R')
g = S.count('G')
b = S.count('B')
sum_rgb = r*g*b

# rgbの組み合わせの総数から距離が等しいものを引く
for i in range(N-2):
    for j in range(i+1, N-1):
        k = j + (j-i)
        if k > N-1:
            break
        if len(set([S[i], S[j], S[k]])) == 3:
            sum_rgb -= 1
print(sum_rgb)

Submission status: Time Limit Exceeded

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

r = S.count('R')
g = S.count('G')
b = S.count('B')
sum_rgb = r*g*b

# rgbの組み合わせの総数から距離が等しいものを引く
for i in range(N-2):
    for j in range(i+1, N-1):
        k = j + (j-i)
        if k > N-1:
            continue
        if len(set([S[i], S[j], S[k]])) == 3:
            sum_rgb -= 1
print(sum_rgb)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
 
r , g, b = S.count("R"), S.count("G"), S.count("B")
ans = r * g * b
 
for i in range(N-2):
    for d in range(N//2+1):
        j = i + d
        k = j + d
        if k < N:
            if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
                ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations

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

perm = list(combinations(range(N), 3))
ans = 0
for i in range(len(perm)):
    a1 = perm[i][0]
    a2 = perm[i][1]
    a3 = perm[i][2]
    if a3-a2 == a2 - a1:
        continue
    if len(set([S[a1], S[a2], S[a3]])) == 3:
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

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

for i in range(N-2):
    for j in range(i+1, N-1):
        k = 2*(j+1) - (i+1) # j > iより k = 2j - i > j
        if k <= N:
            if S[i] != S[j] and S[j] != S[k-1] and S[k-1] != S[i]:
                ans -= 1
print(ans)

Submission status: Time Limit Exceeded

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

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

for i in range(N-2):
    for j in range(i+1, N-1):
        k = 2*(j+1) - (i+1) # j > iより k = 2j - i > j
        if k > N:continue
        if S[i] != S[j] and S[j] != S[k-1] and S[k-1] != S[i]:
        	ans -= 1
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

if __name__=='__main__':
    main(N,S)

Submission status: Time Limit Exceeded

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

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

if __name__=='__main__':
    main(N,S)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

num_r = s.count('R')
num_g = s.count('G')
num_b = s.count('B')

ans = num_r*num_g*num_b

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

Submission status: Time Limit Exceeded

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

num_r = s.count('R')
num_g = s.count('G')
num_b = s.count('B')

ans = num_r*num_g*num_b

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

Submission status: Time Limit Exceeded

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

ans = 0

for i in range(n):
	for j in range(i+1, n):
		for k in range(j+1, n):
			if j - i != k - j:
				if s[i] != s[j] and s[j] != s[k] and s[i] != s[k]:
					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 = []
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]:
				if l not in ans:
					ans.append(l)

print (len(ans))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

s = list(input())

ans = s.count("R")*s.count("G")*s.count("B")

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

in_list = []
for i in range(2):
    in_list.append(input())
N,S = in_list
N = int(N)
res = 0
find_ind = []
for i in ['R','G','B']:
    find = (np.array(list(S)) == i)
    find_ind.append(find.argsort()[::-1][:sum(find)])
    # print(len(find_ind[-1]))

find_ind = np.array(find_ind)
rg_diff = find_ind[0] - find_ind[1].reshape(len(find_ind[1]),1)
rg_diff = np.abs(rg_diff)
# print(rg_diff)
gb_diff = find_ind[1] - find_ind[2].reshape(len(find_ind[2]),1)
gb_diff = np.abs(gb_diff)
gb_diff = gb_diff.T
# print(gb_diff)
br_diff = find_ind[2] - find_ind[0].reshape(len(find_ind[0]),1)
br_diff = np.abs(br_diff)

# print(br_diff)

res = 0
# print(len(gb_diff), len(rg_diff))
for i in range(len(find_ind[1])):
    res += np.sum((rg_diff[i] - gb_diff[i].reshape(len(gb_diff[i]),1)) == 0)
for i in range(len(find_ind[0])):
    res += np.sum((rg_diff[:,i] - br_diff[i].reshape(len(br_diff[i]),1)) == 0)
br_diff = br_diff.T
for i in range(len(find_ind[2])):
    res += np.sum((gb_diff[:,i] - br_diff[i].reshape(len(br_diff[i]),1)) == 0)


print(len(find_ind[0])*len(find_ind[1])*len(find_ind[2])-res)

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
a=1;b=0
for i in s:
	for i3 in range(n-a):
		i3a=a+i3
		if i!=s[i3a]:
			j=s[i3a]
			for i2 in range(n-i3a):
				i2a=i3a+i2
				k=s[i2a]
				if i!=k and j!=k:
					if (i3a+1)*2!=a+i2a+1:
						b+=1
			i2=0
	i3=0
	a+=1	
print(b)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
a=1;b=0
for i in s:#文字列を一個ずつ取り出す一つ目の文字用
	for i3 in range(n-a):#二つ目の文字を探す用ループ
		i3a=a+i3#二文字目のインデックス
		if i!=s[i3a]:#一つ目の文字と二つ目の文字が違う条件式
			j=s[i3a]
			for i2 in range(n-i3a):#三つ目を探すための回転数
				i2a=i3a+i2
				k=s[i2a]
				if i!=k and j!=k:#三文字目の条件式
					if (i3a+1)*2!=a+i2a+1:#j−i≠k−j である
						b+=1
			i2=0
	i3=0
	a+=1#二つ目の文字のインデックスの初期値	
print(b)

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

in_list = []
for i in range(2):
    in_list.append(input())
N,S = in_list
N = int(N)
res = 0
for i in range(N-2):
    str_list = ['R', 'G', 'B']
    str_list.remove(S[i])
    # print("i_str",str_list)
    for j in range(1+i,N-1):
        if S[i] != S[j]:
            a = list(np.copy(str_list))
            a.remove(S[j])
            # print("j_str",a)
            b = (a[0] == np.array(list(S[j+1:])))
            cou_b = sum(b)
            ind_list = b.argsort()[::-1][:cou_b] + j+1

            res += len(ind_list[ind_list-j != j-i])

print(res)

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

in_list = []
for i in range(2):
    in_list.append(input())
N,S = in_list
N = int(N)
res = 0
for i in range(N-2):
    for j in range(1+i,N-1):
        for k in range(1+j,N):
            if S[i] != S[j] and S[j] != S[k] and S[i] != S[k]:
                if j-i != k-j:
                    res += 1

print(res)

Submission status: Time Limit Exceeded

Solution code:

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

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

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

Submission status: Time Limit Exceeded

Solution code:

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

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

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

Submission status: Time Limit Exceeded

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

r=[i for i,x in enumerate(a) if x=='R']
g=[i for i,x in enumerate(a) if x=='G']
b=[i for i,x in enumerate(a) if x=='B']

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

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

print(len(r)*len(g)*len(b)-(s+t+u))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
s=list(S)
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 r in R:
  for g in G:
    for b in B:
      if r+g==2*b or r+b==2*g or g+b==2*r:
        count+=1
ans=len(R)*len(G)*len(B)-count
print(ans)

Submission status: Time Limit Exceeded

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

ans = r*g*b

for i in range(n-2):
  for j in range(i+1, n-1):
    k = j + (j - i)
    if k > n-1:
    	break
    if len(set([s[i], s[j], s[k]])) == 3:
      ans -= 1
print(ans)

Submission status: Time Limit Exceeded

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

ans = r*g*b

for i in range(n):
  diff = 1
  while i + 2 * diff <= n-1:
    if len(set([s[i], s[i+diff], s[i+2*diff]])) == 3:
      ans -= 1
    diff += 1
print(ans)

Submission status: Time Limit Exceeded

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

ans = r*g*b

for i in range(n):
  diff = 1
  while i + 2 * diff <= n-1:
    j = i + diff
    k = j + diff
    if len(set([s[i], s[j], s[k]])) == 3:
      ans -= 1
    diff += 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 += [i]
    elif s[i] == 'G':
        g += [i]
    else:
        b += [i]
ans = 0
for i in range(len(r)):
    for j in range(len(g)):
        for k in range(len(b)):
            tmp = [r[i], g[j], b[k]]
            tmp.sort()
            if(tmp[1] - tmp[0] != tmp[2] - tmp[1]):
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
# 等間隔でなく3つとも異なる文字である組み合わせ数
Ir, Ig, Ib = [], [], []
Sr = Sg = Sb = 0
for i, c in enumerate(S):
    if c == 'R':
        Ir.append(i)
        Sr += 1
    elif c == 'G':
        Ig.append(i)
        Sg += 1
    else:
        Ib.append(i)
        Sb += 1
A = Sr * Sg * Sb
for ir in Ir:
    for ig in Ig:
        if 2*ig-ir in Ib:
            A -= 1
        if 2*ir-ig in Ib:
            A -= 1
        irg = ir+ig
        if irg%2 == 0 and irg//2 in Ib:
            A -= 1
print(A)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
B = []
s = [0, 0, 0]
for c in S[::-1]:
    s[['R', 'G', 'B'].index(c)] += 1
    B.append([s[0], s[1], s[2]])
B.reverse()
A = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        if S[i] == S[j]:
            continue
        D = [0, 1, 2]
        D.remove(['R', 'G', 'B'].index(S[i]))
        D.remove(['R', 'G', 'B'].index(S[j]))
        A += B[j+1][D[0]]
        n = 2*j-i
        if n < N and S[n] == ['R', 'G', 'B'][D[0]]:
            A -= 1
print(A)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
# 等間隔でなく3つとも異なる文字である組み合わせ数
Ir, Ig, Ib = [], [], []
for i, c in enumerate(S):
    if c == 'R':
        Ir.append(i)
    elif c == 'G':
        Ig.append(i)
    else:
        Ib.append(i)
A = 0
for ir in Ir:
    for ig in Ig:
        for ib in Ib:
            B = [ir, ig, ib]
            B.sort()
            if B[2] - B[1] != B[1] - B[0]:
                A += 1
print(A)

Submission status: Time Limit Exceeded

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

for i in range(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]:
                    count+=1
print(count)

Submission status: Time Limit Exceeded

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