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

Solution code:
def main():
    input()
    t = input()
    dic = {'R': [], 'G': [], 'B': []}
    for i in range(len(t)):
        dic[t[i]].append(i)
    count = 0
    for i in dic['R']:
        for j in dic['G']:
            for k in dic['B']:
                if j - i != k - j and (i != j and i != k and k != j):
                    count += 1
    print(count)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import itertools

x1 = 39
x2 = "RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB"
RGB = {"R", "G", "B"}
_sum = 0
for i, rgb in zip(itertools.combinations(range(x1), 3), itertools.combinations(x2, 3)):
    if set(rgb) == RGB and (i[1] - i[0]) != (i[2] - i[1]):
        _sum += 1
print(_sum)

Submission status: Wrong Answer

Solution code:
if __name__ == "__main__":
    N = int(input())
    S = input()

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

    except_cnt = 0
    for w in range(N//2):
        for i in range(N):
            j = i + w
            k = j + w
            if i in R and j in G and k in B:
                except_cnt += 1
            if i in R and j in B and k in G:
                except_cnt += 1
            if i in B and j in R and k in G:
                except_cnt += 1
            if i in B and j in G and k in R:
                except_cnt += 1
            if i in G and j in B and k in R:
                except_cnt += 1
            if i in G and j in R and k in B:
                except_cnt += 1

    print(len(R) * len(G) * len(B) - except_cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
R = []
G = []
B = []
s =input()
ans = 0
for i in range(N):
    if s[i] == "R":
        R.append(i)
    elif s[i] == "G":
        G.append(i)
    else:
        B.append(i)
sB = set(B)
print(sB)
ans = len(R) * len(G) * len(B)
for r in R:
    for g in G:
        i = min(r,g)
        j = max(r,g)
        sa = j - i
        if i - sa in sB:
            ans -= 1
        if j + sa in sB:
            ans -= 1
        if (i + j) / 2 in sB:
            ans -= 1
print(ans)

Submission status: Wrong Answer

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

res = 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] and S[j] != S[k] and S[i] != S[k]:
                res += 1

Submission status: Wrong Answer

Solution code:
from collections import Counter
N = int(input())
S = list(input())
c = Counter(S)
# print('c', c)

if len(c) != 3:
    print(0)
    exit()

con1 = 1
for v in c.values():
    con1 *= v

not_con2 = 0
for d in range(1, 2001):
    for i in range(2001):
        j = i + d
        k = j + d
        if N - 1 < k:
            continue
        # print('S', S[i], S[j], S[k], i, j, k)
        if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
            not_con2 += 1

# print('not_con2', not_con2)
ans = con1 - not_con2
print(ans)

Submission status: Wrong Answer

Solution code:
x = int(input())
s = list(input())
di = {'R':0, 'G':0, 'B':0}
for i in s:
    di[i] += 1
c = 0 #重複を数える
p = (len(s) - 3) // 2
for i in range(len(s)-2):
    for j in range(p+1):
        if i + (2*j) <= len(s) -1:
            ss = s[i] + s[i+j] + s[i+(2*j)]
            if ss == 'GBR'or ss ==  'GRB' or ss ==  'RBG' or ss == 'RGB' or  ss == 'BGR' or ss == 'BRG':
                c += 1
print(di['R'] * di['G'] * di['B'] - c)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import collections
import sys
n=int(input())
if n<=2:
  print(0)
  sys.exit()
s=input()
S=list(s)
t=collections.Counter(S)
R=t['R']
G=t['G']
B=t['B']
num=R*G*B
cnt=0
for i in range(1,n//2):
  for j in range(n):
    if j+(2*i)>n-1:
      continue
    if s[j]=='R':
      if s[j+i]=='G':
        if s[j+(2*i)]=='B':
          cnt+=1
      if s[j+i]=='B':
        if s[j+(2*i)]=='G':
          cnt+=1
    if s[j]=='G':
      if s[j+i]=='R':
        if s[j+(2*i)]=='B':
          cnt+=1
      if s[j+i]=='B':
        if s[j+(2*i)]=='R':
          cnt+=1
    if s[j]=='B':
      if s[j+i]=='G':
        if s[j+(2*i)]=='R':
          cnt+=1
      if s[j+i]=='R':
        if s[j+(2*i)]=='G':
          cnt+=1
print(num-cnt)

Submission status: Wrong Answer

Solution code:
import collections
n = int(input())
s = str(input())
rn = collections.Counter(s)['R']
gn = collections.Counter(s)['G']
bn = collections.Counter(s)['B']
ans = rn*gn*bn
if ans == 0:
     print(0)
else:
     for i in range(1,n//2):
          for j in range(n-2*i):
               if s[j] != s[j+i] and s[j+2*i] != s[j+i] and s[j] != s[j+2*i]:
                    ans-=1
     print(ans)

Submission status: Wrong Answer

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(1,n-2):
    for j in range(n):
        if(j+i*2>=n):break
        d1,d2,d3=s[j],s[j+i],s[j+i*2]
        if(not(d1==d2 or d2==d3 or d1==d3)):ans-=1
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
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 S[i-1] != S[j-1] and S[i-1] != S[k-1] and S[j-1] != S[k-1]:
        ans+=1
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=input()
x=(s.count('R'))*(s.count('G'))*(s.count('B'))
for i in range(1,(n-1)//2):
  for j in range(n-i*2):
    if len(set([s[j],s[j+i],s[j+i+i]]))==3:
      x-=1
print(x)

Submission status: Wrong Answer

Solution code:
from collections import Counter

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

c = Counter(S)
ans = c["R"] * c["G"] * c["B"]

if ans == 0:
    print(0)
    exit()

for n_skip in range(1, N//3 + 10):
    for i in range(N):
        try:
            if set(["R", "G", "B"]) == set([S[i], S[i+n_skip], S[i + 2*n_skip]]):
                ans -= 1
        except IndexError:
            break

print(ans)

Submission status: Wrong Answer

Solution code:
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)
    elif s[i]=='B':
        b.append(i)
cnt=0
for _r in r:
    for _g in g:
        for _b in b:
            if _r<_g and _g<_b and (_g-_r!=_b-_g):
                cnt+=1
print(cnt)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# coding: utf-8
from math import gcd
N = int(input())
S = 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)
print(R, G, B)
L = []
for r in R:
    for g in G:
        for b in B:
            if g - r != b - g and r < g < b:
                L.append((r,g,b))
                ans += 1

for r in R:
    for b in B:
        for g in G:
            if b - r != g - b and r < b < g:
                L.append((r,g,b))
                ans += 1
                
for g in G:
    for b in B:
        for r in R:
            if b - g != r - b and g < b < r:
                L.append((r,g,b))
                ans += 1

for g in G:
    for r in R:
        for b in B:
            if r - g != b - r and g < r < b:
                L.append((r,g,b))
                ans += 1

for b in B:
    for g in G:
        for r in R:
            if g - b != r - g and b < g < r:
                L.append((r,g,b))
                ans += 1

for b in B:
    for r in R:
        for g in G:
            if r - b != g - r and b < r < g:
                L.append((r,g,b))
                ans += 1
print(ans)
# print(len(set(L)))
# print(len(R), len(G), len(B))

Submission status: Wrong Answer

Solution code:
from collections import Counter
N = int(input())
S = list(input())
c = Counter(S)
# print('c', c)

con1 = 1
for v in c.values():
    con1 *= v

not_con2 = 0
for d in range(1, 2001):
    for i in range(2001):
        j = i + d
        k = j + d
        if N - 1 < k:
            continue
        # print('S', S[i], S[j], S[k], i, j, k)
        if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
            not_con2 += 1

# print('not_con2', not_con2)
ans = con1 - not_con2
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from collections import Counter
n = int(input())
s = input()
r = [i+1 for i in range(n) if s[i]=='R']
b = [i+1 for i in range(n) if s[i]=='B']
g = [i+1 for i in range(n) if s[i]=='G']
minus = 0
def cnt_cmb(a, b, c):
    cnt = 0
    set_c = set(c)
    for i in a:
        for j in b:
            if (i+j)/2 in set_c:
                cnt += 1
    return cnt
minus +=cnt_cmb(r, b, g)
minus += cnt_cmb(b, g, r)
minus += cnt_cmb(g, r, b)
a = Counter(s)
rslt = 1
for value in a.values():
    rslt *= value
print(rslt-minus)

Submission status: Wrong Answer

Solution code:
import collections
n = int(input())
s = str(input())
rn = collections.Counter(s)['R']
gn = collections.Counter(s)['G']
bn = collections.Counter(s)['B']
ans = rn*gn*bn
for i in range(n//2):
     for j in range(n-2*i):
          if s[j] != s[j+i] and s[j+2*i] != s[j+i] and s[j] != s[j+2*i]:
               ans-=1
print(ans)

Submission status: Wrong Answer

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

rgb_dic = {
    "R": [],
    "G": [],
    "B": []
}
for i, s in enumerate(S):
    rgb_dic[s].append(i)

R_list = rgb_dic["R"]
G_list = rgb_dic["G"]
B_list = rgb_dic["B"]

cnt = 0
idx_set = set()
for i in R_list:
    for j in G_list:
        for k in B_list:
            if j-i != k-j:
                cnt += 1
         
print(cnt)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from collections import Counter
n = int(input())
s = input()
slen = len(s)
d = Counter(s)
res = 1
for v in d.values():
    res *= v
#print(res)
for step in range(1, slen):
    for start in range(slen):
        i = start
        j = i + step
        k = j + step
        if k >= slen:
            break
        ijk = [s[i], s[j], s[k]]
        # print("ijk is", ijk)
        if 'R' in ijk and 'G' in ijk and 'B' in ijk:
            res -= 1

print(res)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
r = []
g = []
b = []
for i in range(n):
  if s[i] == 'R':
    r.append(i)
  elif s[i] == 'G':
    g.append(i)
  else:
    b.append(i)
res = 0
for i in range(len(r)):
  for j in range(len(b)):
    if (i + j) / 2 in g:
      res += len(g) - 1
    else:
      res += len(g)
print(res)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input() #sys.stdin.readlineは最後が改行

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 r:
    for j in g:
        for k in b:
            i_, j_, k_ = sorted([i,j,k])
            if j_-i_ != k_-j_ :
                ans+=1
                print(i_,j_,k_)

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter

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


c = Counter(S)

ans = 1
for v in c.values():
    ans *= v
for i in range(N):
    if N-i <= 1:
        break
    for step in range(1, N-i):
        if i+2*step > N - 1:
            break
        ok = True
        ok &= S[i] != S[i+step]
        ok &= S[i] != S[i+2*step]
        ok &= S[i+step] != S[i+2*step]
        if ok:
            ans -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())

str = input()
sum = 0
for i in range(n-2):
  for j in range(i+1, n-1):
    for k in range(j+1, n):
      if str[i] == str[j]:
        break
      elif str[i] == str[k]:
        break
      elif str[j] == str[k]:
        break
      elif j - i == k - j:
        break
      else:
        sum += 1
print(sum)

Submission status: Wrong Answer

Solution code:
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0
"""
i-j-k間が等しくなる組み合わせを求めてから、
Rの数*G*B - それ
で求まる？
"""
n = int(readline())
s = readline().rstrip().decode('utf-8')

if n == 3:
    print(0)
    exit()
dic1 = dict()
for i in s:
    if i in dic1:
        dic1[i] += 1
    else:
        dic1[i] = 1

if len(dic1) != 3:
    print(0)
    exit()
res = 1
for i in dic1.values():
    res *= i
print(res) #1881あと81ケース
#例外を求める
i = 0
x,y,z = 0,0,0
count = 0
while True:
    if i >= n:
        break
    j = 1
    while True:
        x = i
        y = i+j
        z = i+2*j
        if z >= n:
            break
        else:
            if s[x] != s[y] and s[x] != s[z] and s[y] != s[z]:
                count += 1 
        j += 1
        
    i += 1

print(res - count)

Submission status: Wrong Answer

Solution code:
#coding:utf-8
import sys,os
from collections import defaultdict, deque
from fractions import gcd
from math import ceil, floor
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in os.environ else lambda *x: 0
def main(given=sys.stdin.readline):
    input = lambda: given().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    XLMIIS = lambda x: [LMIIS() for _ in range(x)]
    YN = lambda c : print('Yes') if c else print('No')
    MOD = 10**9+7

    N = II()
    S = input()
    sum_d = 0
    for d in range(1,N//2):
        for i in range(N):
            if i+2*d < N:
                if S[i] != S[i+d] and S[i+d] != S[i+2*d] and S[i+2*d] != S[i]:
                    sum_d += 1
    r = 0
    g = 0
    b = 0
    for c in S:
        if c == 'R':
            r += 1
        elif c == 'G':
            g += 1
        else:
            b += 1
    print(r*g*b-sum_d)



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
read = sys.stdin.read


#p,n,k,b,*a = [int(i) for i in read().split()]

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


MOD = 10**9+7
ans = 0
from itertools import permutations
L = ["R","G","B"]
ans = 0
for p in permutations(L):
    dpr = dpg = dpb = 0
    x = p[0]
    y = p[1]
    for i in s:
        if i == x:
            dpr += 1
            dpr %= MOD
        elif i == y:
            dpg += dpr
            dpg %= MOD
        else:
            dpb += dpg
            dpb %= MOD
    ans += dpb

#print(ans)

for d in range(1,n):
    for i in range(n):
        if i+2*d >= n: break
        if s[i] != s[i+d] and s[i+2*d]!=s[i+d] and s[i]!=s[i+2*d]:
            ans -= 1
        
print(ans%MOD)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
R=[];G=[];B=[]
for i in range(N):
  if S[i]=='R':R.append(i)
  elif S[i]=='G':G.append(i)
  else:B.append(i)
s=len(R)*len(G)*len(B)
RG= [(x+y)/2 for x in R for y in G]
GB= [(x+y)/2 for x in G for y in B]
BR= [(x+y)/2 for x in B for y in R]

for i in RG:
  if i in B:s-=1
for i in GB:
  if i in R:s-=1
for i in BR:
  if i in G:s-=1
print(s);exit()

print(R,G,RG)
exit()
s=len(R)*len(G)*len(B)
for i in R:
  for j in G:
    if (i+j)/2 in B:s-=1
for i in G:
  for j in B:
    if (i+j)/2 in R:s-=1
for i in B:
  for j in R:
    if (i+j)/2 in G:s-=1

print(s)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
R=[];G=[];B=[]
for i in range(N):
  if S[i]=='R':R.append(i)
  elif S[i]=='G':G.append(i)
  else:B.append(i)
#print(len(R),len(G),len(B))
s=len(R)*len(G)*len(B)
for i in R:
  for j in G:
    if (i+j)/2 in B:s-=1
for i in G:
  for j in B:
    if (i+j)/2 in R:s-=1
for i in B:
  for j in R:
    if (i+j)/2 in G:s-=1

print(s)

Submission status: Time Limit Exceeded

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

s=0
for i in range(len(R)):
  for j in range(len(G)):
    for k in range(len(B)):
#      print(i,j,k)
#      print(max(R[i],G[j],B[k])+min(R[i],G[j],B[k]),R[i]+G[j]+B[k])
      if (max(R[i],G[j],B[k])+min(R[i],G[j],B[k]))*3/2==R[i]+G[j]+B[k]:continue
      s+=1

print(s)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
assert len(S) == N
count = 0
for i in range(0, len(S)-2):
  for j in range(i+1, len(S)-1):
    if S[i] == S[j]:
      continue
    SiSj = S[i] + S[j]
    if 'R' not in SiSj:
      Sk = 'R'
    elif 'G' not in SiSj:
      Sk = 'G'
    else:
      Sk = 'B'
    for k in range(j+1, len(S)):
      # RGBのどれかになるはず
      if Sk == S[k]:
        if j - i != k - j:
          count += 1
print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    N = int(input())
    S = input()
    cnt = 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:
                    cnt += 1
    print(cnt)

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    N = int(input())
    S = input()
    cnt = 0
    for i in range(N):
        for j in range(i+1, N):
            for k in range(j+1, N):
                if (S[i] != S[j] and 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:
n = int(input())
s = input()

r = [0] * (n+1)
g = [0] * (n+1)
b = [0] * (n+1)
for i, c in enumerate(reversed(s), 1):
    if c == 'R':
        r[i] += r[i-1] + 1
    else:
        r[i] += r[i-1]
    
    if c == 'G':
        g[i] += g[i-1] + 1
    else:
        g[i] += g[i-1]
    
    if c == 'B':
        b[i] += b[i-1] + 1
    else:
        b[i] += b[i-1]

r = r[:-1][::-1]
g = g[:-1][::-1]
b = b[:-1][::-1]

ans=0
for i in range(n-2):
    for j in range(i+1, n-1):
        if s[i] == s[j]:
            continue
        se = set('RGB') - set(s[i] + s[j])
        c = se.pop()
        if c == 'R':
            ans += r[j]
            if 2*j-i < n and s[2*j-i] == 'R':
                ans -= 1
        elif c == 'G':
            # print('j=',j,'g[j]=',g[j])
            ans += g[j]
            if 2*j-i < n and s[2*j-i] == 'G':
                ans -= 1
        elif c == 'B':
            ans += b[j]
            if 2*j-i < n and s[2*j-i] == 'B':
                ans -= 1
        # print('i=',i,'j=',j,'c=',c, 'ans=',ans)
print(ans)

Submission status: Time Limit Exceeded

Solution code:

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

idxs = [i for i in range(N)]

ris = [i for i, s in enumerate(S) if s == "R"]
gis = [i for i, s in enumerate(S) if s == "G"]
bis = [i for i, s in enumerate(S) if s == "B"]

ans = 0
for ri in ris:
    for gi in gis:
        for bi in bis:
            i, j, k = sorted([ri, gi, bi])
            if k - j != j - i:
                ans += 1



"""
for i in range(N):
    for j in range(i, N):
        for k in range(j, N):
            if j - i != k - j:
                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
from collections import deque

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

idxs = [i for i in range(N)]

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


"""
data = deque()
for c in combinations(idxs, 3):
    i, j, k = c
    if j-i != k-j:
        data.append(c)

ans = 0
for c in data:
    # i, j, k = sorted(c)
    i, j, k = c
    s0 = S[i]
    s1 = S[j]
    s2 = S[k]

    if s0 != s1 and s1 != s2 and s0 != s2:
        ans += 1
"""

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations

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

idxs = [i for i in range(N)]

ans = 0
for c in combinations(idxs, 3):
    # i, j, k = sorted(c)
    i, j, k = c
    s0 = S[i]
    s1 = S[j]
    s2 = S[k]

    if j - i != k - j and s0 != s1 and s1 != s2 and s0 != s2:
        ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations

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

idxs = [i for i in range(N)]

ans = 0
for c in combinations(idxs, 3):
    i, j, k = sorted(c)
    s0 = S[i]
    s1 = S[j]
    s2 = S[k]

    if j - i != k - j and s0 != s1 and s1 != s2 and s0 != s2:
        ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

def inp():
    return int(input())
def inps():
    return input().rstrip()
def inpl():
    return list(map(int, input().split()))
def _debug(obj):
    print(obj, file=sys.stderr)

# import decimal
# from decimal import Decimal
# decimal.getcontext().prec = 10

# from heapq import heappush, heappop, heapify
# import math
# from math import gcd
# import itertools as it
# import collections
# from collections import deque 

# ---------------------------------------

N = inp()
S = inps()

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)

ans = 0
for i in r:
    for j in g:
        for k in b:
            if abs(i - j) != abs (j - k) and abs(i - j) != abs (i - k) and abs(i - k) != abs (j - k):
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

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

result = 0
for r in R:
    for g in G:
        for b in B:
            i = min(r, g, b)
            k = max(r, g, b)
            j = (r + g + b) - i - k
            if j - i != k - j:
                result += 1

print(result)

Submission status: Time Limit Exceeded

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

r = []
g = []
b = []
cnt = -1

for i in s:
	cnt += 1
	if i == 'R':
		r.append(cnt)
	elif i == 'G':
		g.append(cnt)
	else:
		b.append(cnt)

ans = 0		
						
for i in r:
	for j in g:
		for t in b:
			l = (i,j,t)
			total = sum(l)
			m, M = min(l), max(l)
			middle = total - m - M
			if middle - m != M - middle:
				ans += 1
	
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# author:  Taichicchi
# created: 19.09.2020 00:14:21

import sys

N = int(input())

S = input()

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


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]) & (S[j] != S[k]) & (S[k] != S[i]):
                    ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
cntR,cntG,cntB=S.count('R'),S.count('G'),S.count('B')

a=[cntR]*N
b=[cntG]*N
c=[cntB]*N
for i in range(N):
    if i==0:
        if S[i]=='R':
            a[i]-=1
        elif S[i]=='G':
            b[i]-=1
        else:
            c[i]-=1
    else:
        a[i],b[i],c[i]=a[i-1],b[i-1],c[i-1]
        if S[i]=='R':
            a[i]-=1
        elif S[i]=='G':
            b[i]-=1
        else:
            c[i]-=1
ans=0
for i in range(N-2):
    p=0
    t=S[i]
    r=0
    for j in range(i+1,N-1):
        if S[i]!=S[j]:
            t+=S[j]
            k=2*j-i
            if t=='RG' or t=='GR':
                ans+=c[j]
            elif t=='GB' or t=='BG':
                ans+=a[j]
            else:
                ans+=b[j]
            if k<N and S[k] not in t:
                ans-=1
            t=S[i]

print(ans)

Submission status: Time Limit Exceeded

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

Rindex = set(i for i in range(N) if S[i] == 'R')
Gindex = set(i for i in range(N) if S[i] == 'G')
Bindex = set(i for i in range(N) if S[i] == 'B')

ans = 0

for i in Rindex:
    for j in Gindex:
        for k in Bindex:
            if abs(i - j) == abs(j - k) or abs(j - k) == abs(k - i) or abs(k - i) == abs(i - j):continue
            ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

Rindex = set(i for i in range(N) if S[i] == 'R')
Gindex = set(i for i in range(N) if S[i] == 'G')
Bindex = set(i for i in range(N) if S[i] == 'B')

def findijk(l1, l2, l3):
    ret = 0
    for i in l1:
        for j in l2:
            if i > j:continue
            for k in l3:
                if j > k or j - i == k - j:continue
                ret += 1
    return ret

ans = 0

ans += findijk(Rindex, Gindex, Bindex)
ans += findijk(Bindex, Rindex, Gindex)
ans += findijk(Gindex, Bindex, Rindex)
ans += findijk(Rindex, Bindex, Gindex)
ans += findijk(Gindex, Rindex, Bindex)
ans += findijk(Bindex, Gindex, Rindex)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce, lru_cache
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def TUPLE(): return tuple(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 
#mod = 998244353
#from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

N = INT()
S = input()

R = S.count("R")
G = S.count("G")
B = S.count("B")

cnt = 0
for l, r in combinations(range(N), 2):
	if l%2 != r%2:
		continue
	m = (l+r)//2
	if len(set([S[l], S[m], S[r]])) == 3:
		cnt += 1

print(R*G*B - cnt)

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

Submission status: Time Limit Exceeded

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

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

print(ans)

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

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

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import Counter
N = int(input())
S = list(input())
Sc = Counter(S)
R = Sc["R"]
G = Sc["G"]
B = Sc["B"]
if R == 0 or G == 0 or B == 0:
    print(0)
    sys.exit()
else:
    ans = R*G*B
for i in range(1, N//2+2):
    for j in range(N):
        if j+i+i >= N:
            break
        RGB = S[j]+S[j+i]+S[j+i+i]
        if RGB == "RGB" or RGB == "RBG" or RGB == "GRB" or RGB == "GBR" or RGB == "BRG" or RGB == "BGR":
            ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import Counter

input = sys.stdin.readline
N = int(input())
S = input().strip()

counter = Counter(S)
if len(counter) < 3:
    print(0)
    sys.exit()

exclude = 0
max_w = N // 2
for w in range(1, max_w+1):
    for i in range(N):
        if N <= i + 2*w:
            continue
        if S[i] != S[i+w] and S[i] != S[i+2*w] and S[i+w] != S[i+2*w]:
            exclude += 1
# print(exclude)

print(counter["R"] * counter["G"] * counter["B"] - exclude)