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()
R = []
nsum = 0
for i in range(N):
    if S[i] == "R":
        R.append(i)
B = []
for i in range(N):
    if S[i] == "B":
        B.append(i)
g = 0
for i in range(N):
    if S[i] == "G":
        g += 1
        for r in R:
            for b in B:
                if abs(i - b) - abs(b - r) == 0 or abs(b - i) - abs(r - i) == 0 or abs(b - r) - abs(i - r) == 0:
                    nsum += 1
                    if max(i,b,r) == b: break
print(len(R)*len(B)*g - nsum)

Submission status: Time Limit Exceeded

Solution code:
from itertools import permutations

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

r = [0] * (2 * n)
g = [0] * (2 * n)
b = [0] * (2 * 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 = sum(r) * sum(g) * sum(b)

lst = [r, g, b]
for i in range(n - 1):
    for j in range(i + 1, n):
        k = 2 * j - i
        for x, y, z in permutations(lst, 3):
            if x[i] and y[j] and z[k]:
                ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:


N = int(input())
S = input()
S = S[::-1]

cnt = S.count('B')*S.count('G')*S.count('R')

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


N = int(input())
S = input()
S = S[::-1]

cnt = S.count('B')*S.count('G')*S.count('R')
# for i in range(1,N-1):
#     for j in range(i+1,N):
#         if S[i-1] != S[j-1]:
#             for k in range(j+1,N+1):
#                 if S[i-1]!=S[k-1] and S[j-1]!=S[k-1] and j-i!=k-j:
#                     cnt += 1
for i in range(N-2):
    for j in range(i,N-1):
        k = 2*j-i
        if k>j and k<=N-1:
            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:
n = int(input())
s = list(input())

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

a = r * g * b

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

print(a)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
ans=S.count("R")*S.count("G")*S.count("B")
 
for i in range(N):
  for j in range(i+1,N):
    for k in range(j+1,N):
      if j-i==k-j and S[j]!=S[i] 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 = str(input())

count = 0
for i in range(1,n-1):
    for j in range(i+1,n):
        for k in range(j+1,n+1):
            #print("{},{},{}".format(s[i-1],s[j-1],s[k-1]))
            if s[i-1] != s[j-1] and s[j-1] != s[k-1] and s[i-1] != s[k-1] and j-i != k-j:
                #print("{},{},{}".format(s[i-1],s[j-1],s[k-1]))
                count += 1
    
print(count)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
R=S.count("R")
G=S.count("G")

ans=R*G*(N-(R+G))

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

Submission status: Time Limit Exceeded

Solution code:
import bisect

def bs(x, key_func, arr):

	if len(arr) == 0 or arr == None:
		return -1

	n = len(arr)
	ini = 0
	fin = n - 1

	while ini < fin:
		m = (ini + fin) // 2
		if key_func(arr[m]) < x:
			ini = m + 1
		else:
			fin = m
	if fin == n - 1 and key_func(arr[fin]) < x:
		return n
	if key_func(arr[fin]) >= x:
		return max(0, fin - 1)
	return fin	

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

	tb_i = {'BG': [], 'BR': [], 'GR': []}
	for i in range(N):
		for j in range(i + 1, N):
			if S[i] == S[j]:
				continue
			word = ''.join(list(sorted([S[i], S[j]])))
			tb_i[word].append((j, j - i))
	
	for key in tb_i:
		tb_i[key].sort(key=lambda x: x[0])
	
	rs = 0	
	for i in range(2, N):	
		word = None
		if S[i] == 'R':
			word = 'BG'
		if S[i] == 'G':
			word = 'BR'
		if S[i] == 'B':
			word = 'GR'

		idx = bs(i, lambda x: x[0], tb_i[word])
		if idx == len(tb_i[word]):
			idx -= 1	
		if idx == 0 and tb_i[word][0][0] >= i:
			idx -= 1
		for j in range(idx, -1, -1):
			if tb_i[word][j][1] == i - tb_i[word][j][0]:
				continue
			rs += 1
	print(rs)
	return 0 

main()

Submission status: Time Limit Exceeded

Solution code:
import bisect

def bs(x, key_func, arr):

	if len(arr) == 0 or arr == None:
		return -1

	n = len(arr)
	ini = 0
	fin = n - 1

	while ini < fin:
		m = (ini + fin) // 2
		if key_func(arr[m]) < x:
			ini = m + 1
		else:
			fin = m
	if fin == n - 1 and key_func(arr[fin]) < x:
		return n
	if key_func(arr[fin]) >= x:
		return max(0, fin - 1)
	return fin	

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

	tb_i = {'BG': [], 'BR': [], 'GR': []}
	for i in range(N):
		for j in range(i + 1, N):
			if S[i] == S[j]:
				continue
			word = ''.join(list(sorted([S[i], S[j]])))
			tb_i[word].append((j, j - i))
	
	for key in tb_i:
		tb_i[key] = list(sorted(tb_i[key], key=lambda x: x[0]))
	
	rs = 0	
	for i in range(2, N):	
		word = None
		if S[i] == 'R':
			word = 'BG'
		if S[i] == 'G':
			word = 'BR'
		if S[i] == 'B':
			word = 'GR'

		idx = bs(i, lambda x: x[0], tb_i[word])
		if idx == len(tb_i[word]):
			idx -= 1	
		if idx == 0 and tb_i[word][0][0] >= i:
			idx -= 1
		for j in range(idx, -1, -1):
			if tb_i[word][j][1] == i - tb_i[word][j][0]:
				continue
			rs += 1
	print(rs)
	return 0 

main()

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
ans=S.count("R")*S.count("G")*S.count("B")

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

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

print(cnt)

Submission status: Time Limit Exceeded

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


nr = 0
ng = 0
nb = 0

for i in range(0, n):
    if s[i] == 'R':
        nr += 1
    elif s[i] == 'G':
        ng += 1
    else:
        nb += 1

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

print(nr*ng*nb - c)

Submission status: Time Limit Exceeded

Solution code:
from itertools import product

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

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

R = len(R_i)
G = len(G_i)
B = len(B_i)

xs = map(sorted, product(R_i, G_i))

s = 0
for a in xs:
    k = a[1]*2 - a[0]  # 2j-i
    if k in B_i:
        s +=1
    i = a[0]*2 - a[1]  # 2j-k
    if i in B_i:
        s +=1
    j = (a[0] + a[1]) / 2
    if j.is_integer():
        j = int(j)
        if j in B_i:
            s +=1
print(R*G*B - s)

Submission status: Time Limit Exceeded

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


nr = 0
ng = 0
nb = 0

for i in range(0, n):
    if s[i] == 'R':
        nr += 1
    elif s[i] == 'G':
        ng += 1
    else:
        nb += 1

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

print(nr*ng*nb - c)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=list(input())
ans=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:
            a=r+g+b
            e=max(r,g,b)
            d=min(r,g,b)
            c=a-e-d
            if e-c!=c-d:
                ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=list(input())
ans=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)
lenb=len(B)
for r in R:
    for g in G:
        flag=0
        for b in B:
            a=r+g+b
            if a%3==0:
                a=a//3
                if a==r or a==g or a==b:
                    flag=flag+1
        ans=ans+lenb-flag
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):
    for d in range(n):
        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()

c = 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:
                c += 1

print(c)

Submission status: Time Limit Exceeded

Solution code:
#I, K, N  = map(int, input().split())
#L = list(map(int, input().split()))

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

#print(N, S) 

def rgbcnt():
  cnt = 0
  for i in range(0, N-2):
    r = S[i]
    for j in range(i+1, N-1):
      g = S[j]
      if g == r:
        continue
      if r=="R" and g=="G":
        b = "B"
      elif r=="R" and g=="B":
        b = "G"
      elif r=="G" and g=="R":
        b = "B"
      elif r=="G" and g=="B":
        b = "R"
      elif r=="B" and g=="R":
        b = "G"
      else: # r=="B" and g=="G":
        b = "R"
      w=S[j:]
      cnt += w.count(b)
      x=j-i +j
      if x < N and S[x] ==b:
        cnt -= 1
#      for k in range(j+1, N):
#        if S[k] == b and j-i != k-j:
#          cnt += 1
  return cnt
#

ans = rgbcnt()
print(ans)

Submission status: Time Limit Exceeded

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

ans = 0

def first(a,b,c):
    if S[a] != S[b] and S[a] != S[c] and S[b] != S[c]:
        return 0
    else:
        return 1

def second(i,j,k):
    if j-i != k-j:
        return 0
    else:
        return 1

# def third(i,j,k):
#     if i < j and j < k:
#         return 0
#     else:
#         return 1

for i in range(N):
    for j in range(i+1,N):
        for k in range(j+1,N):
            check = 0
            check += first(i,j,k)#index型の時
            check += second(i+1,j+1,k+1)#数値として１から使い始めたいとき
            # check += third(i+1,j+1,k+1)
            if check == 0:
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:

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

Rn=S.count('R')
Gn=S.count('G')
Bn=S.count('B')

ans=0

for k in range(N//2-2+1 if N&1==0 else N//2-1+1):#k個飛ばし
    i=0
    while i+2*(k+1)<=N-1:
        ls=[S[i],S[i+k+1],S[i+2*(k+1)]]
        if "R" in ls and "G" in ls and "B" in ls:
            ans+=1
        i+=1

print(Rn*Gn*Bn-ans)

Submission status: Time Limit Exceeded

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

ans = 0

def first(a,b,c):
    if S[a] != S[b] and S[a] != S[c] and S[b] != S[c]:
        return 0
    else:
        return 1

def second(i,j,k):
    if j-i != k-j:
        return 0
    else:
        return 1

def third(i,j,k):
    if i < j and j < k:
        return 0
    else:
        return 1

for i in range(N):
    for j in range(N):
        for k in range(N):
            check = 0
            check += first(i,j,k)#index型の時
            check += second(i+1,j+1,k+1)#数値として１から使い始めたいとき
            check += third(i+1,j+1,k+1)
            if check == 0:
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:


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

Rcnt = 0
Gcnt = 0

excep = 0

for i in range(len(S)):
    if S[i] == 'R':
        Rcnt += 1
    elif S[i] == 'G':
        Gcnt += 1
    for j in range(1, len(S)):
        if i+j+j < len(S) and S[i] != S[i+j]:
            if S[i] != S[i+j+j] and S[i+j] != S[i+j+j]:
                excep += 1
        elif i+j+j >= len(S):
            break

print(Rcnt * Gcnt * (N - Rcnt - Gcnt) - excep)

Submission status: Time Limit Exceeded

Solution code:

# -*- coding: utf-8 -*-

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

count=0

rs= gs= bs=0
for i,s in enumerate(S):
    if s =='R':
        rs+=1
    elif s=='G':
        gs+=1
    else: bs+=1

tot=rs*gs*bs


x0=set(['R','G','B'])
for i in range(1,N-1):
    for j in range(i+1,N):
        k=2*j-i

        if k<N+1:
            x=set([S[i-1],S[j-1],S[k-1]])
            if x==x0: tot-=1
print(tot)

Submission status: Time Limit Exceeded

Solution code:
#I, K, N  = map(int, input().split())
#L = list(map(int, input().split()))

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

#print(N, S) 

def rgbcnt(r, b, g):
  cnt = 0
  for i in range(0, N-2):
    if S[i] != r:
      continue
    for j in range(i+1, N-1):
      if S[j] != g:
        continue
      w=S[j:]
      cnt += w.count(b)
      x=j-i +j
      if x < N and S[x] ==b:
        cnt -= 1
#      for k in range(j+1, N):
#        if S[k] == b and j-i != k-j:
#          cnt += 1
  return cnt
#

ans = 0
ans += rgbcnt("R", "G", "B")
#print(ans)
ans += rgbcnt("R", "B", "G")
#print(ans)
ans += rgbcnt("G", "R", "B")
#print(ans)
ans += rgbcnt("G", "B", "R")
#print(ans)
ans += rgbcnt("B", "R", "G")
#print(ans)
ans += rgbcnt("B", "G", "R")

print(ans)

Submission status: Time Limit Exceeded

Solution code:


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

Rcnt = 0
Gcnt = 0

excep = 0

for i in range(len(S)):
    if S[i] == 'R':
        Rcnt += 1
    elif S[i] == 'G':
        Gcnt += 1
    for j in range(1, len(S)):
        if i+j+j < len(S) and S[i] != S[i+j]:
            if S[i] != S[i+j+j] and S[i+j] != S[i+j+j]:
                excep += 1
        elif i+j+j >= len(S):
            break

RGBcalc = Rcnt * Gcnt * (N - Rcnt - Gcnt)

print(RGBcalc - excep)

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):
        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:
                        sum += 1
print(sum)

Submission status: Time Limit Exceeded

Solution code:

# -*- coding: utf-8 -*-

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

count=0

rs, gs, bs=[], [], []
for i,s in enumerate(S):
    if s =='R':
        rs.append(i+1)
    elif s=='G':
        gs.append(i+1)
    else: bs.append(i+1)

tot=len(rs)*len(gs)*len(bs)
n=len(bs)

for i in rs:
    for j in gs:
        co=0
        i1=min([i,j])
        i2=max([i,j])
        o1=2*i-j
        o2=2*j-i
        o3=0.5*(i+j)
        if  o1 in bs: co+=1
        if  o2 in bs: co+=1
        if  o3 in bs: co+=1
        tot-=co

            
print(tot)

Submission status: Time Limit Exceeded

Solution code:
#I, K, N  = map(int, input().split())
#L = list(map(int, input().split()))

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

#print(N, S) 

def rgbcnt(r, b, g):
  cnt = 0
  for i in range(0, N-2):
    if S[i] != r:
      continue
    for j in range(i+1, N-1):
      if S[j] != g:
        continue
      for k in range(j+1, N):
        if S[k] == b and j-i != k-j:
          cnt += 1
  return cnt
#

ans = 0
ans += rgbcnt("R", "G", "B")
#print(ans)
ans += rgbcnt("R", "B", "G")
#print(ans)
ans += rgbcnt("G", "R", "B")
#print(ans)
ans += rgbcnt("G", "B", "R")
#print(ans)
ans += rgbcnt("B", "R", "G")
#print(ans)
ans += rgbcnt("B", "G", "R")

print(ans)

Submission status: Time Limit Exceeded

Solution code:


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

Rcnt = 0
Gcnt = 0

excep = 0

for i in range(len(S)):
    if S[i] == 'R':
        Rcnt += 1
    elif S[i] == 'G':
        Gcnt += 1
    for j in range(1, len(S)):
        # if i+j+j < len(S) and S[i] != S[i+j]:
        if i+j+j < len(S) and S[i] != S[i+j] and S[i] != S[i+j+j] and S[i+j] != S[i+j+j]:
            excep += 1

RGBcalc = Rcnt * Gcnt * (N - Rcnt - Gcnt)

print(RGBcalc - excep)

Submission status: Time Limit Exceeded

Solution code:
import itertools
import sys
import math
import numpy as np
import fractions
from collections import deque
from itertools import combinations
from functools import reduce
from functools import lru_cache
sys.setrecursionlimit(10**9)

def main():
    N = int(input())
    S = input()
    r,g,b = "R","G","B"

    def get_other(x,y):
        if x in [r,g] and y in [r,g]:
            return b
        if x in [r,b] and y in [r,b]:
            return g
        if x in [g,b] and y in [g,b]:
            return r

    rs = [0] * (N+1)
    gs = [0] * (N+1)
    bs = [0] * (N+1)

    for i in range(N):
        rs[i+1] = ((S[i] == r) + 0) + rs[i]
        gs[i+1] = ((S[i] == g) + 0) + gs[i]
        bs[i+1] = ((S[i] == b) + 0) + bs[i]

    s_dic = {r:rs,b:bs,g:gs}

    ans = 0
    for i in range(N):
        for j in range(i+1,N):
            if S[i] != S[j]:
                not_k = 2 * j - i
                other = get_other(S[i],S[j])

                counter = s_dic[other]

                ans += counter[N] - counter[j+1]
                if not_k < len(S) and S[not_k] == other:
                    ans -= 1

    print(ans)



if __name__ == "__main__":
  main()

Submission status: Time Limit Exceeded

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

r_ary = []
g_ary = []
b_ary = []

for i in (s):
    if i == 'R':
        r_ary.append(i)
    elif i == 'G':
        g_ary.append(i)
    elif i == 'B':
        b_ary.append(i)

r = len(r_ary)
g = len(g_ary)
b = len(b_ary)
'''
r = s.count('R')
g = s.count('G')
b = s.count('B')
'''
# 1つ目の条件を満たす
first_conditions = r*g*b
# 1つ目の条件は満たすが、2つ目の条件は満たさない
# 2つ目の条件を満たすということは、(i,j,k)のときk=2j-i
for i in range(n-2):
    for j in range(i+1,n-1):
        if 2*j-i<n and s[i]!=s[2*j-i] and s[j]!=s[2*j-i] and s[i]!=s[j]:
            first_conditions -= 1
print(first_conditions)

Submission status: Time Limit Exceeded

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

r_ary = []
g_ary = []
b_ary = []

for i in (s):
    if i == 'R':
        r_ary.append(i)
    elif i == 'G':
        g_ary.append(i)
    elif i == 'B':
        b_ary.append(i)

r = len(r_ary)
g = len(g_ary)
b = len(b_ary)
'''
r = s.count('R')
g = s.count('G')
b = s.count('B')
'''
# 1つ目の条件を満たす
first_conditions = r*g*b
# 1つ目の条件は満たすが、2つ目の条件は満たさない
second_condition = 0
# 2つ目の条件を満たすということは、(i,j,k)のときk=2j-i
for i in range(n-2):
    for j in range(i+1,n-1):
        if 2*j-i<n and s[i]!=s[2*j-i] and s[j]!=s[2*j-i] and s[i]!=s[j]:
            second_condition += 1
print(first_conditions-second_condition)

Submission status: Time Limit Exceeded

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

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

# 1つ目の条件を満たす
first_conditions = r*g*b
# 1つ目の条件は満たすが、2つ目の条件は満たさない
second_condition = 0
# 2つ目の条件を満たすということは、(i,j,k)のときk=2j-i
for i in range(n-2):
    for j in range(i+1,n-1):
        if 2*j-i<n and s[i]!=s[2*j-i] and s[j]!=s[2*j-i] and s[i]!=s[j]:
            second_condition += 1
print(first_conditions-second_condition)

Submission status: Time Limit Exceeded

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

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

# 1つ目の条件を満たす
first_conditions = r*g*b
# 1つ目の条件は満たすが、2つ目の条件は満たさない
second_condition = 0
sum_ = 0
# 2つ目の条件を満たすということは、(i,j,k)のときk=2j-i
for i in range(n):
    for j in range(i,n):
        if 2*j-i< n  and s[i] != s[2*j-i] and s[j] != s[2*j-i] and s[i] != s[j]:
            second_condition += 1
print(first_conditions-second_condition)

Submission status: Time Limit Exceeded

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

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

# 1つ目の条件を満たす
first_conditions = r*g*b
# 一つ目の条件は満たすが、2つ目の条件は満たさない
second_condition = 0
sum_ = 0
for i in range(n):
    r_ = s[i]
    for j in range(i,n):
        g_ = s[j]
        if r_ != g_:
            for k in range(j,n):
                if j-i == k-j  and g_ != s[k] and r_ != s[k]:
                    second_condition += 1
print(first_conditions-second_condition)

Submission status: Time Limit Exceeded

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

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

# 1つ目の条件を満たす
first_conditions = r*g*b
# 一つ目の条件は満たすが、2つ目の条件は満たさない
second_condition = 0
sum_ = 0
for i in range(n):
    r_ = s[i]
    for j in range(i,n):
        g_ = s[j]
        for k in range(j,n):
            if j-i == k-j and r_ != g_ and g_ != s[k] and r_ != s[k]:
                second_condition += 1
print(first_conditions-second_condition)

Submission status: Time Limit Exceeded

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

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

# 1つ目の条件を満たす
first_conditions = r*g*b
# 一つ目の条件は満たすが、2つ目の条件は満たさない
second_condition = 0
sum_ = 0
for i in range(n):
    for j in range(i,n):
        for k in range(j,n):
            if j-i == k-j and s[i] != s[j] and s[j] != s[k] and s[i] != s[k]:
                second_condition += 1
print(first_conditions-second_condition)

Submission status: Time Limit Exceeded

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

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

# 1つ目の条件を満たす
first_conditions = r*g*b
# 一つ目の条件は満たすが、2つ目の条件は満たさない
second_condition = 0
sum_ = 0
for i in range(n):
    for j in range(i,n):
        for k in range(j,n):
                if j-i == k-j and (s[i] != s[j] and s[j] != s[k] and s[i] != s[k]):
                    second_condition += 1
print(first_conditions-second_condition)

Submission status: Time Limit Exceeded

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

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

# 1つ目の条件を満たす
first_conditions = r*g*b
# 一つ目の条件は満たすが、2つ目の条件は満たさない
second_condition = 0
sum_ = 0
for i in range(n):
    for j in range(n):
        for k in range(n):
            if i < j and j < k:
                if j-i == k-j and (s[i] != s[j] and s[j] != s[k] and s[i] != s[k]):
                    second_condition += 1
print(first_conditions-second_condition)

Submission status: Time Limit Exceeded

Solution code:
def main():
  n = int(input())
  s = input()
  s1 = []
  s2 = []
  s3 = []
  ss = [("R",s.count("R")),("G",s.count("G")),("B",s.count("B"))]
  ss = sorted(ss,key=lambda x:x[1])
  ans = ss[0][1]*ss[1][1]*ss[2][1]
  for i in range(n):
    if s[i]==ss[2][0]:
      s3.append(i)
    elif s[i]==ss[1][0]:
      s2.append(i)
    elif s[i]==ss[0][0]:
      s1.append(i)
      
  for i in s1:
    for j in s2:
      if i*2-j in s3:
        ans -= 1
      if j*2-i in s3:
        ans -= 1
      if (i+j)%2==0 and (i+j)/2 in s3:
        ans -= 1
  print(ans)
main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
r, g, b = 0, 0, 0
for i in s:
    if i == 'R':
        r += 1
    elif i == 'G':
        g += 1
    else:
        b += 1
bad = 0
for i in range(n):
    gap = 0
    while i+(gap+1)*2 <= n-1:
        if s[i] != s[i+gap+1] and s[i+gap+1] != s[i+(gap+1)*2]\
                and s[i] != s[i+(gap+1)*2]:
            bad += 1
        gap += 1
print(r*g*b-bad)

Submission status: Time Limit Exceeded

Solution code:
def main():
  n = int(input())
  s = input()
  s1 = []
  s2 = []
  s3 = []
  ss = [("R",s.count("R")),("G",s.count("G")),("B",s.count("B"))]
  ss = sorted(ss,key=lambda x:x[1])
  ans = ss[0][1]*ss[1][1]*ss[2][1]
  for i in range(n):
    if s[i]==ss[0][0]:
      s1.append(i)
    if s[i]==ss[1][0]:
      s2.append(i)
    if s[i]==ss[2][0]:
      s3.append(i)
      
  for i in s1:
    for j in s2:
      if i*2-j in s3:
        ans -= 1
      if j*2-i in s3:
        ans -= 1
      if (i+j)/2 in s3:
        ans -= 1
  print(ans)
main()

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
S=list(map(str,input()))

y=[]
count=0
for i in range(1,n+1):
  for j in range(i,n+1):
    for k in range(j,n+1):
      if not k-j==j-i:
        y.append([i,j,k])
for p in range(0,len(y)):
  if not S[y[p][0]-1]==S[y[p][1]-1] and not S[y[p][0]-1]==S[y[p][2]-1] and not S[y[p][1]-1]==S[y[p][2]-1] :
    count+=1
  else:
    count+=0
print(count)

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

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

rcnt = s.count('R')
gcnt = s.count('G')
bcnt = s.count('B')

sub = 0
for i in range(n):
    for j in range(i+1, n):
        k = j + (j - i)
        if n <= k: break

        alp = set()
        alp.add(s[i])
        alp.add(s[j])
        alp.add(s[k])

        if len(alp) == 3: sub += 1

ans = rcnt * gcnt * bcnt - sub
print(ans)

Submission status: Time Limit Exceeded

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

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

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

    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 in r_list:
        for g in g_list:
            L = g - r
            if g + L in b_list:
                sum_count -= 1
            if r - L in b_list:
                sum_count -= 1
            if L % 2 == 0 and L//2 + r in b_list:
                sum_count -= 1

    print(sum_count)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

rcnt = s.count('R')
gcnt = s.count('G')
bcnt = s.count('B')

sub = 0
for i in range(n):
    for j in range(i+1, n):
        k = j + (j - i)
        if n <= k: continue

        alp = set()
        alp.add(s[i])
        alp.add(s[j])
        alp.add(s[k])

        if len(alp) == 3: sub += 1

ans = rcnt * gcnt * bcnt - sub
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

r = 0
g = 0
b = 0

for i in range(n):
    if s[i] == 'R':
        r += 1
    elif s[i] == 'G':
        g += 1
    else:
        b += 1
ans = r * b * g

for c in range(n):
    center = s[c]
    for dif in range(n):
        if dif + c > n - 1 or c - dif < 0:
            continue
        left = s[c - dif]
        right = s[c + dif]
        if left != center and center != right and right != left:
            ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
sum = 0
for i in range(n):
  for j in range(i+1,n):
    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):
        sum += 1
print(sum)

Submission status: Time Limit Exceeded

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

R = s.count('R')
G = s.count('G')
B = n - R - G

err = 0
for i in range(n - 2):
    span = 1
    while i + 2 * span < n:
        if all([s[i + span * t] for t in range(3)].count(c) for c in ['R', 'G', 'B']):
            err += 1
        span += 1

print(R * G * B - err)

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):
        if s[i] == s[j]:
            continue
        k = 2*j - i
        if j < k < n and s[i] != s[k] and s[j] != s[k]:
            cnt += 1
print(s.count('R') * s.count('G') * s.count('B') - cnt)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

# 標準入力 <int>
N = int(input())

# 標準入力<str>
S = input()

ans = 0

for i in range(N - 2):

  S2 = []
  for j in range(N - i - 2):
    rgb = "RGB"
    if S[i] == S[i + j + 1]:
      continue
    rgb = rgb.replace(S[i], "")
    rgb = rgb.replace(S[i + j + 1], "")
    if i + ((j + 1) * 2) < N:
      S2 = S[i + j + 2:i + (2 * j) + 2] + S[i + (2 * j) + 3:]
    else:
      S2 = S[i + j + 2:]
    #print(rgb)
    ans += S2.count(rgb)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
S=list(map(str,input()))

y=[]
count=0
for i in range(1,n+1):
  for j in range(i,n+1):
    for k in range(j,n+1):
      if not k-j==j-i:
        y.append([i,j,k])
      else:
        y=y
      z=len(y)
for p in range(0,z):
  if not S[y[p][0]-1]==S[y[p][1]-1] and not S[y[p][0]-1]==S[y[p][2]-1] and not S[y[p][1]-1]==S[y[p][2]-1] :
    count+=1
  else:
    count+=0
print(count)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
S=list(map(str,input()))

y=[]
count=0
for i in range(1,n+1):
  for j in range(i,n+1):
    for k in range(j,n+1):
      if not k-j==j-i:
        y.append([i,j,k])
      else:
        y=y
      z=len(y)
for p in range(0,z):
  a=y[p][0]
  b=y[p][1]
  c=y[p][2]
  if not S[a-1]==S[b-1] and not S[a-1]==S[c-1] and not S[b-1]==S[c-1] :
    count+=1
  else:
    count+=0
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
count = 0
def findlet(let,ind):
    add = st[ind:].count(let)
    if add > 0:
        if 2*ind < len(st):
            if st[2*ind] == let:
                add -= 1
        return add
    return 0
for i in range(N-2):
    if i == 0:
        st = S
    else:
        st = st[1:]
    if st[0] == "R":
        for j in [x for x, y in enumerate(st) if y == 'G']:
            count += findlet("B",j)
        for j in [x for x, y in enumerate(st) if y == 'B']:
            count += findlet("G",j)
    elif st[0] == "G":
        for j in [x for x, y in enumerate(st) if y == 'B']:
            count += findlet("R",j)
        for j in [x for x, y in enumerate(st) if y == 'R']:
            count += findlet("B",j)
    else:
        for j in [x for x, y in enumerate(st) if y == 'R']:
            count += findlet("G",j)
        for j in [x for x, y in enumerate(st) if y == 'G']:
            count += findlet("R",j)
print(count)