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

#BGR, BRG, GBR, GRB, RBG, RGB
ans = 0
for j in range(N):
  for i in range(j):
    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):
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

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 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:

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

ans = 0
k = 'RGB'
for i in range(N):
	for j in range(i+1,N):
		if not S[i]==S[j]:
			tmp =k.replace(S[i],'')
			tmp = tmp.replace(S[j],'')
			ans += S[j+1:].count(tmp)
			if 2*j-i<N and S[2*j-i]==tmp:
				ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
SS=[a for a in S]
SS.insert(0,0)
answer=0
for i in range(1,N+1):
    for j in range(i+1,N+1):
        for k in range(j+1,N+1):
            if i+k==j*2:
                continue
            else:
                if SS[i]!=SS[j] and SS[j]!=SS[k] and SS[k]!=SS[i]:
                    answer+=1
print(answer)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3

import sys
import pprint

sys.setrecursionlimit(10 ** 6)

class Logger:
	def __init__(self, debug):
		self.debug = debug
	def print(self, *args):
		if self.debug:
			pprint.pprint(args)


def main():
	log = Logger(0)
	n = int(sys.stdin.readline())
	s = input()
	sumc = []
	sumr = 0
	sumg = 0
	sumb = 0
	for i in range(n):
		if s[i] == "R":
			sumr += 1
		if s[i] == "G":
			sumg += 1
		if s[i] == "B":
			sumb += 1
		sumc.append({
			"R" : sumr,
			"G": sumg,
			"B": sumb
			})
	a = {"R": 0, "G": 1, "B": 2}
	case = 0
	log.print(sumc)
	for i in range(n):
		for j in range(i+1, n-1):
			l = j - i
			if s[i] == s[j]:
				continue
			rgb = list("RGB")
			rgb.pop(rgb.index(s[i]))
			rgb.pop(rgb.index(s[j]))
			case += sumc[n-1][rgb[0]] - sumc[j][rgb[0]]
			if j + l < n and s[j+l] == rgb[0]:
				case -= 1
			log.print(i, j, rgb, case)
	print(case)




	


main()

Submission status: Time Limit Exceeded

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

ans = 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 j - i != k - j:
               x = set(s[i-1] + s[j-1] + s[k-1])
               if len(x) == 3:
                  ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)  # 再帰関数の再帰の深さを設定
to_index = lambda x: int(x) - 1  # 入力した数字に1を引いたものを返す
print_list_in_2D = lambda x: print(*x, sep="\n")  # リストの要素を改行を挟んで表示する関数

# 入力を整数に変換して受け取る
def input_int():
    return int(input())

def map_int_input():
    return map(int, input())

MII = map_int_input

def MII_split():
    return map(int, input().split())

def MII_to_index():
    return map(to_index, input())

def MII_split_to_index():
    return map(to_index, input().split())

# 入力全てを整数に変換したものの配列を受け取る
def list_int_inputs():
    return list(map(int, input()))

LII = list_int_inputs

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

# 2次元リスト化
def LII_2D(rows_number):
    return [LII() for _ in range(rows_number)]

def LII_split_2D(rows_number):
    return [LII_split() for _ in range(rows_number)]

N = input_int()
S = input()

if N < 3:
    print(0)
    exit()
count = 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] or S[j] == S[k] or S[i] == S[k] or j - i == k - j:
                continue
            else:
                count += 1
print(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+1,N-1):
        if S[i] == S[j]:
            continue
        else:
            for k in range(j+1,N):
                if S[i] == S[k] or S[j] == S[k]:
                    continue
                else:
                    if j-i != k-j:
                        ans = ans + 1
                    else:
                        continue
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def main():
    n=int(input())
    s=input()
    l1=[0]*(n)
    l2=[0]*(n)
    l3=[0]*(n)
    for i in range(1,n):
        if s[i]=="R":
            l1[i-1]+=1
        elif s[i]=="G":
            l2[i-1]+=1
        else:
            l3[i-1]+=1
    for i in range(n-1,0,-1):
        l1[i-1]+=l1[i]
        l2[i-1]+=l2[i]
        l3[i-1]+=l3[i]
    ans=0
    for i in range(n):
        for j in range(i+1,n):
            if set([s[i],s[j]])=={"G","B"}:
                ans+=l1[j]
                if 2*j-i<n and s[2*j-i]=="R":
                    ans-=1
            elif set([s[i],s[j]])=={"R","B"}:
                ans+=l2[j]
                if 2*j-i<n and s[2*j-i]=="G":
                    ans-=1
            elif set([s[i],s[j]])=={"R","G"}:
                ans+=l3[j]
                if 2*j-i<n and s[2*j-i]=="B":
                    ans-=1
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def split(word):
    return [char for char in word]
N=int(input())
S=input()
Slist=split(S)
Rcounter=0
Bcounter=0
Gcounter=0
for i in range(0,N):
    if Slist[i]=='R':
        Rcounter=Rcounter+1
    if Slist[i]=='B':
        Bcounter=Bcounter+1
    if Slist[i]=='G':
        Gcounter=Gcounter+1
ans=Rcounter*Gcounter*Bcounter
for i in range(0,N):
    for j in range(i+1,N):
        if 2*j-i>N-1:
            break
        flag=0
        if Slist[i]==Slist[j]:
            flag=1
        if Slist[i]==Slist[2*j-i]:
            flag=1
        if Slist[j]==Slist[2*j-i]:
            flag=1
        if flag==0:
            ans=ans-1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n=I()
  s=S()

  r=[0]*(n+1)
  g=[0]*(n+1)
  b=[0]*(n+1)

  for i,x in enumerate(s):
    if x=='R':
      r[i+1]+=1
    elif x=='G':
      g[i+1]+=1
    else:
      b[i+1]+=1
    r[i+1]+=r[i]
    g[i+1]+=g[i]
    b[i+1]+=b[i]

  # print(r)
  # print(g)
  # print(b)

  ans=0
  for i,x in enumerate(s):
    for j in range(i+1,n):
      y=s[j]

      k=j+(j-i)
      if k<n:
        # print(x,y,i,j,k,s[k])
        if x=='R' and y=='G':
          ans+=b[k]-b[j]+b[n]-b[k]
        elif x=='R' and y=='B':
          ans+=g[k]-g[j]+g[n]-g[k]
        elif x=='G' and y=='B':
          ans+=r[k]-r[j]+r[n]-r[k]
        elif x=='G' and y=='R':
          ans+=b[k]-b[j]+b[n]-b[k]
        elif x=='B' and y=='R':
          ans+=g[k]-g[j]+g[n]-g[k]
        elif x=='B' and y=='G':
          ans+=r[k]-r[j]+r[n]-r[k]
        
        z=s[k]
        if j-i==k-j and len(set([x,y,z]))==3:
          ans-=1

      else:
        if x=='R' and y=='G':
          ans+=b[n]-b[j]
        elif x=='R' and y=='B':
          ans+=g[n]-g[j]
        elif x=='G' and y=='B':
          ans+=r[n]-r[j]
        elif x=='G' and y=='R':
          ans+=b[n]-b[j]
        elif x=='B' and y=='R':
          ans+=g[n]-g[j]
        elif x=='B' and y=='G':
          ans+=r[n]-r[j]

  return ans

# main()
print(main())

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

N = int(input())
S = list(input())
R = []
G = []
B = []
for i in range(N):
    if S[i] =="R":
        R.append(i)
    if S[i] =="G":
        G.append(i)
    if S[i] =="B":
        B.append(i)
cnt = 0
for r in R:
    for g in G:
        for b in B:
            L = [r,g,b]
            if (r+g+b)/3 not in L:
                cnt +=1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n= int(input())
s = input()
R = []
G = []
B = []
import bisect
def func(l1,l2,l3):
  res =0
  for i in l1:
    for j in l2:
      if i>j:
        continue
      a = l3[bisect.bisect_left(l3,j):]
      res += len(a)
      if j*2-i in a:
        res -=1
  return res

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)
    
ans = func(R,G,B)+func(R,B,G)+func(G,R,B)+func(G,B,R) + func(B,R,G)+func(B,G,R)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
from sys import stdin
import math
#from functools import reduce
    
#A, B, C = [int(x) for x in stdin.readline().rstrip().split()]
N = int(input())
S = input()

counter = 0

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

print(counter)

Submission status: Time Limit Exceeded

Solution code:

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

    if n <= 3:
        print(0)
        exit()
    r = 0
    for ia in range(0,n-3):
        for ib in range(ia+1, n-1):
            for ic in range(ib+1,n):
                if (ib - ia) != (ic - ib) and s[ia] != s[ib] and s[ia] != s[ic] and s[ib] != s[ic]:
                    r += 1

    print(r)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

r = list()
g = list()
b = list()
for idx, si in enumerate(s):
    if si == 'R':
        r.append(idx)
    elif si == 'G':
        g.append(idx)
    else:
        b.append(idx)

ret = 0
for ri in r:
    for gi in g:
        for bi in b:
            comp = list(sorted([ri, gi, bi]))
            if (comp[1] - comp[0]) != (comp[2] - comp[1]):
                ret += 1

print(ret)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
#h  = list(map(int, input().split()))
#h, w = list(map(int, input().split()))


s = input()
count = 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[i]!=s[k] and s[j]!=s[k]:
                    count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
R = set()
B = set()
G = set()
for i in range(N):
    if S[i] == "R":
        R.add(i)
    elif S[i] == "B":
        B.add(i)
    elif S[i] == "G":
        G.add(i)

c = 0
for r in R:
    for b in B:
        p = set()
        if r < b:
            p.add(r - (b-r))
            if ((r + b)/2).is_integer():
                p.add((r + b) // 2)
            p.add(b + (b-r))
        elif r > b:
            p.add(b - (r-b))
            if ((r + b)/2).is_integer():
                p.add((r + b) // 2)
            p.add(r + (r-b))

        c += len(G - p)
print(c)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
N=int(input())
S=list(input())
Pattern=list(combinations(range(N),3))
res = 0
for i in Pattern:
    if i[2]-i[1] != i[1]-i[0]:
        if S[i[0]] != S[i[1]] and S[i[1]] != S[i[2]] and S[i[2]] != S[i[0]]:
            res += 1
print(res)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
Rs,Gs,Bs=[],[],[]

for i in range(n):
    if s[i]=='R':
        Rs.append(i)
    elif s[i]=='G':
        Gs.append(i)
    else:
        Bs.append(i)

ans=len(Rs)*len(Gs)*len(Bs)
for i in Rs:
    for j in Gs:
        for k in Bs:
            l=[i,j,k]
            l.sort()
            a,b,c=l[0],l[1],l[2]
            if b-a==c-b:
                ans-=1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left, bisect_right
from collections import defaultdict

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

pos = defaultdict(lambda: [])
for i, s in enumerate(S) :
  pos[s].append(i)

def color(a, b) :
  for c in 'RGB' :
    if c != S[a] and c != S[b] :
      return c

ret = 0
for a in range(N) :
  for b in range(a + 2, N) :
    if S[a] == S[b] : continue
    c = color(a, b)
    ret += bisect_right(pos[c], b) - 1
    ret -= bisect_right(pos[c], a) - 1
    if (a + b) % 2 == 0 and S[(a + b) // 2] == c :
      ret -= 1
      
print(ret)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

from collections import defaultdict
from bisect import bisect_left

input()
s = input()

d = defaultdict(list)


def get_missing(a, b):
    if a == 'R' or b == 'R':
        if a == 'G' or b == 'G':
            return 'B'
        else:
            return 'G'
    else:
        return 'R'


for idx in range(len(s)):
    d[s[idx]].append(idx)

ans = 0

for i in range(len(s)):
    for j in range(i + 1, len(s)):
        if s[i] == s[j]:
            continue
        missing = get_missing(s[i], s[j])
        bis = bisect_left(d[missing], j + 1)
        ans += len(d[missing]) - bis
        dist = j - i

        dist_bis = bisect_left(d[missing], j + dist)

        if dist_bis < len(d[missing]) and d[missing][dist_bis] == j + dist:
            ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from math import gcd
from functools import reduce

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

ret=0

for i,x in enumerate(s):
	for j,y in enumerate(s[i+1:],start=i+1):
		if x==y:
			continue
		if i+(j-i)*2 < n :
			z=s[i+(j-i)*2]
			if y!=z!=x:
				ret-=1

dic={}
for x in s:
	if not x in dic: 
		dic[x]=0
	dic[x]+=1

for i,x in dic.items():
	for j,y in dic.items():
		for k,z in dic.items():
			if i<j<k:
				ret+=x*y*z
print(ret)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
def  bfs(i, j):
  cnt = 0
  bf = [s[i], s[j]]
  for k in range(j+1, n):
    if k-j == j-i:
      continue
    if s[k] not in bf:
      cnt += 1
if ('R' not in s) and ('G' not in s) and ('B' not in s):
  print(0)
else:
  cnt = 0
  for i in range(n-3):
    for j in range(i+1, n-1):
      si = s[i]
      sj = s[j]
      if si == sj:
          continue
      bf = [si,sj]
      for k in range(j+1, n):
        if k-j == j-i:
          continue
        if s[k] not in bf:
          cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations

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

ans = 0

for i in range(1,N-1):
    for j in range(i+1,N):
        for k in range(j+1,N+1):
            if (2*j != k+i)&(len(set([S[i-1],S[j-1],S[k-1]]))==3):
                ans +=1

print(ans)

Submission status: Time Limit Exceeded

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

r = [i for i, x in enumerate(S) if x == "R"]
g = [i for i, x in enumerate(S) if x == "G"]
b = [i for i, x in enumerate(S) if x == "B"]
kotae = 0

for i in range(len(r)):
    for k in range(len(g)):
        for l in range(len(b)):
            a = [r[i] + 1 ,g[k] + 1 ,b[l] + 1]
            a.sort()
            if a[2]-a[1] != a[1] - a[0]:
                kotae += 1
# print(r,g,b)
print(kotae)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=list(input())
rc,bc,gc=[0]*N,[0]*N,[0]*N
if S[0]=="R":
  rc[0]+=1
elif S[0]=="B":
  bc[0]+=1
if S[0]=="G":
  gc[0]+=1
for i in range(1,N):
  if S[i]=="R":
    rc[i]=rc[i-1]+1
    bc[i]=bc[i-1]
    gc[i]=gc[i-1]
  elif S[i]=="B":
    bc[i]=bc[i-1]+1
    gc[i]=gc[i-1]
    rc[i]=rc[i-1]
  elif S[i]=="G":
    gc[i]=gc[i-1]+1    
    rc[i]=rc[i-1]
    bc[i]=bc[i-1]

ans=0
for i in range(N-2):
  for j in range(i+1,N-1):
    if S[i]==S[j]:
      pass
    else:
      if [S[i],S[j]]==["R","B"] or [S[i],S[j]]==["B","R"]:
        ans+=gc[-1]-gc[j]
        if 2*j-i<N:
          if S[2*j-i]=="G":
            ans-=1
      elif [S[i],S[j]]==["B","G"] or [S[i],S[j]]==["G","B"]:
        ans+=rc[-1]-rc[j]
        if 2*j-i<N:
          if S[2*j-i]=="R":
            ans-=1
      elif [S[i],S[j]]==["G","R"] or [S[i],S[j]]==["R","G"]:
        ans+=bc[-1]-bc[j]
        if 2*j-i<N:
          if S[2*j-i]=="B":
            ans-=1
            
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

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

total = 0
for r, g, b in itertools.product(R, G, B):
    i = min([r, g, b])
    k = max([r, g, b])
    j = sum([r, g, b]) - i - k
    if j - i != k - j:
        total += 1

print(str(total))

Submission status: Time Limit Exceeded

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

ans=0

for i in range(N-2):
    for j in range(i+1,N-1):
        for k in range(j+1,N):
            if j-i != k-j 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:
class SegmentTree(object):
    """
    セグメントツリー (0-indexed)

    1. 値の更新 O(logN)
    2. 区間クエリ O(logN)
    """
    def __BinOp(self, x, y):
        """ セグ木で使用する二項演算 """
        return x + y

    def __init__(self, init_ele, N:int):
        """
        セグ木を構築する

        init_ele: 単位元 
        N: 要素数
        """
        self.__init_ele = init_ele
        self.__n = 1
        while self.__n < N:
            self.__n <<= 1

        self.__dat = [init_ele] * (2 * self.__n)
    
    def update(self, k:int, x):
        """ k番目の値をxに変更する """
        k += self.__n - 1
        self.__dat[k] = x
        while k:
            k = (k - 1) // 2
            self.__dat[k] = self.__BinOp(
                self.__dat[k * 2 + 1], self.__dat[k * 2 + 2]
                )
    
    def query(self, p:int, q:int):
        """ 区間クエリ [p,q) """
        if q <= p: return self.__init_ele
        p += self.__n - 1
        q += self.__n - 2
        res = self.__init_ele
        while q - p > 1:
            if not (p & 1):
                res = self.__BinOp(res, self.__dat[p])
            if q & 1:
                res = self.__BinOp(res, self.__dat[q])
                q -= 1
            p = p // 2
            q = (q - 1) // 2
        res = self.__BinOp(res, self.__dat[p])
        if p != q: res = self.__BinOp(res, self.__dat[q])
        return res
    
    def get_num(self, k:int):
        """ k番目の値を取得する """
        k += self.__n - 1
        return self.__dat[k]

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

R = SegmentTree(0, N)
G = SegmentTree(0, N)
B = SegmentTree(0, N)

for i in range(N):
    if S[i] == "R":
        R.update(i, 1)
    elif S[i] == "G":
        G.update(i, 1)
    else:
        B.update(i, 1)

ans = 0
for i in range(N):
    for j in range(i + 1, N - 1):
        if S[i] == S[j]: continue
        tmp = set(["R","G","B"])
        tmp.remove(S[i])
        tmp.remove(S[j])
        Sk = tmp.pop()
        no = j + (j - i)
        if Sk == "R":
            ans += R.query(j + 1, min(N, no)) + R.query(min(N, no + 1), N)
        elif Sk == "G":
            ans += G.query(j + 1, min(N, no)) + G.query(min(N, no + 1), N)
        else:
            ans += B.query(j + 1, min(N, no)) + B.query(min(N, no + 1), N)

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import bisect
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)

ans = 0
for i in range(n):
    for j in range(i+1, n):
        ci, cj = s[i], s[j]
        if ci==cj:
            continue
        ng = 2*j-i

        if (ci=='R' and cj=='B') or (ci=='B' and cj=='R'):
            ans += len(G)-bisect.bisect_left(G, j)
            if ng in G:
                ans -= 1
        elif (ci=='R' and cj=='G') or (ci=='G' and cj=='R'):
            ans += len(B)-bisect.bisect_left(B, j)
            if ng in B:
                ans -= 1
        else:
            ans += len(R)-bisect.bisect_left(R, j)
            if ng in R:
                ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:

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



print(count)

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

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

print(total)

Submission status: Time Limit Exceeded

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

rcs = [0] * N
gcs = [0] * N
bcs  = [0] * N

for i in range(N):
    if S[i] == 'R':
        rcs[i] = 1
    elif S[i] == 'G':
        gcs[i] = 1
    elif S[i] == 'B':
        bcs[i] = 1

for i in range(1, N):
    rcs[i] += rcs[i - 1]
    gcs[i] += gcs[i - 1]
    bcs[i] += bcs[i - 1]

result = 0
for i in range(N):
    if S[i] == 'R':
        for j in range(i + 1, N):
            if S[j] == 'R':
                continue
            elif S[j] == 'G':
                result += bcs[N - 1] - bcs[j]
                t = 2 * j - i
                if t < N and S[t] == 'B':
                    result -= 1
            elif S[j] == 'B':
                result += gcs[N - 1] - gcs[j]
                t = 2 * j - i
                if t < N and S[t] == 'G':
                    result -= 1
    elif S[i] == 'G':
        for j in range(i + 1, N):
            if S[j] == 'G':
                continue
            elif S[j] == 'R':
                result += bcs[N - 1] - bcs[j]
                t = 2 * j - i
                if t < N and S[t] == 'B':
                    result -= 1
            elif S[j] == 'B':
                result += rcs[N - 1] - rcs[j]
                t = 2 * j - i
                if t < N and S[t] == 'R':
                    result -= 1
    elif S[i] == 'B':
        for j in range(i + 1, N):
            if S[j] == 'B':
                continue
            elif S[j] == 'R':
                result += gcs[N - 1] - gcs[j]
                t = 2 * j - i
                if t < N and S[t] == 'G':
                    result -= 1
            elif S[j] == 'G':
                result += rcs[N - 1] - rcs[j]
                t = 2 * j - i
                if t < N and S[t] == 'R':
                    result -= 1
print(result)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=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 = input()
cnt = 0

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

print(cnt)

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)
    elif S[i] == 'B':
        B.append(i+1)

ans = 0
for i in range(len(R)):
    for j in range(len(G)):
        for k in range(len(B)):
            tmpi = min(R[i], G[j], B[k])
            tmpk = max(R[i], G[j], B[k])
            tmpj = (R[i] + G[j] + B[k])-(tmpi+tmpk)

            if (tmpj-tmpi) != (tmpk-tmpj):
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

c = 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):
#                print("{}:{}:{}".format(i,j,k))
                c+=1
print(c)

Submission status: Time Limit Exceeded

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

c = 0

for k in range(n):
    for j in range(k):
        for i in range(j):
            if (k-j) != (j-i):
                if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
                    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")

ans = R*G*B
for i in range(N-2):
    for j in range(i,N-1):
        k = 2*j - i
        if k < N:
            tmp = []
            tmp.append(S[i])
            tmp.append(S[j])
            tmp.append(S[k])
            if len(set(tmp)) == 3:
                ans -= 1

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

ans = 0
for i in range(N):
    for j in range(i+1, N):
        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:
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = str(input())
a=0
for i in range(N):
    for j in range(N):
        for k in range(N):
            if S[i]==S[j]:
                continue
            if S[j]==S[k]:
                continue
            if S[k]==S[i]:
                continue
            if k+i==2*j:
                continue
            if i<=j and j<=k: 
                a+=1
print(a)