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()
c = 0
for i in range(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]:
                continue
            if S[j] == S[k]:
                continue
            if j-i == k-j:
                continue
            c += 1
print(c)

Submission status: Time Limit Exceeded

Solution code:
def abc162d_rgb_triplets():
    n = int(input())
    s = input()
    r_cnt = [0] * n
    g_cnt = [0] * n
    b_cnt = [0] * n
    for i in range(n - 1, -1, -1):
        if i != n - 1:
            r_cnt[i] = r_cnt[i + 1]
            g_cnt[i] = g_cnt[i + 1]
            b_cnt[i] = b_cnt[i + 1]
        if s[i] == 'R':
            r_cnt[i] += 1
        elif s[i] == 'G':
            g_cnt[i] += 1
        else:
            b_cnt[i] += 1

    result = 0
    for i in range(n - 2):
        for j in range(i + 1, n - 1):
            if s[i] == s[j]: continue
            total = 0
            if 'R' not in (s[i] + s[j]):
                total = r_cnt[j + 1]
                if j+(j - i) < n and s[j+(j - i)] == 'R':
                    total -= 1
            if 'G' not in (s[i] + s[j]):
                total = g_cnt[j + 1]
                if j + (j - i) < n and s[j + (j - i)] == 'G':
                    total -= 1
            if 'B' not in (s[i] + s[j]):
                total = b_cnt[j + 1]
                if j + (j - i) < n and s[j + (j - i)] == 'B':
                    total -= 1
            result += total
    print(result)
abc162d_rgb_triplets()

Submission status: Time Limit Exceeded

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

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

c = 0
for i in range(1,(n+1)//2):
    for j in range(1,n-2*i+1):
        if j in r and j+i in g and j+2*i in b:
            c += 1
        if j in r and j+i in b and j+2*i in g:
            c += 1
        if j in g and j+i in b and j+2*i in r:
            c += 1
        if j in g and j+i in r and j+2*i in b:
            c += 1
        if j in b and j+i in r and j+2*i in g:
            c += 1
        if j in b and j+i in g and j+2*i in r:
            c += 1

ans = (len(r)*len(g)*len(b) - c)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
from math import gcd,pi
INF = float("inf")

import sys
sys.setrecursionlimit(10**6)
import itertools
from collections import Counter,deque
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]


def main():
    n = i_input()
    s = s_input()
    l = list(itertools.combinations(list(range(n)), 3))
    ans = 0
    for i in l:
        z = i[0]
        o = i[1]
        t = i[2]
        if o - z != t - o:
            if s[z] != s[o] and s[o] != s[t] and s[t] != s[z]:
                ans += 1
    print(ans)


if __name__=="__main__":
    main()

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

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import sys
input = sys.stdin.readline

def main():
    N = int(input())
    S = input()
    ans = 0
    RGB = []
    for i in range(N):
        RGB.append(Counter(S[:i+1]))
        
    for i in range(N-1):
        for j in range(i+1, N):
            if S[i] == S[j]:
                continue
            c = 'RGB'.replace(S[i], '')
            c = c.replace(S[j], '')
            ans += RGB[-1][c] - RGB[j][c]
            if 2*j - i < N and S[2*j-i] == c:
                ans -= 1
    print(ans)

main()

Submission status: Time Limit Exceeded

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

def main():
    N = int(input())
    S = input()
    ans = 0
    for i in range(N-1):
        for j in range(i+1, N):
            if S[i] == S[j]:
                continue
            c = 'RGB'.replace(S[i], '')
            c = c.replace(S[j], '')
            ans += S[j+1:].count(c)
            if 2*j - i < N and S[2*j-i] == c:
                ans -= 1
    print(ans)

main()

Submission status: Time Limit Exceeded

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

def main():
    N = int(input())
    S = input()
    ans = 0
    for i in range(N-1):
        for j in range(i, N):
            if S[i] == S[j]:
                continue
            c = 'RGB'.replace(S[i], '')
            c = c.replace(S[j], '')
            ans += S[j+1:].count(c)
            if 2*j - i < N and S[2*j-i] == c:
                ans -= 1
    print(ans)

main()

Submission status: Time Limit Exceeded

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

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

main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = [("R", "G", "B").index(c) for c in input()]

sums = [[0] * (N + 1) for _ in range(3)]
for i, c in enumerate(S):
    for x in sums:
        x[i+1] = x[i]
    sums[c][i+1] += 1

ans = 0
for i in range(N - 2):
    si = S[i]
    for j in range(i + 1, N - 1):
        sj = S[j]
        if si == sj:
            continue
        sk = (set(range(3)) - {si, sj}).pop()
        count = sums[sk][N] - sums[sk][j+1]
        if j - i <= N - (j + 1):
            count -= (sums[sk][j+1+j-i] - sums[sk][j+1+j-i-1])
        ans += count
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from numba import jit

n=int(input())
s=input()
R=[]
G=[]
B=[]

@jit
def f():
  for i in range(n):
    if s[i]=="R":
      R.append(i)
    elif s[i]=="G":
      G.append(i)
    else:
      B.append(i)
  r=len(R)
  g=len(G)
  b=len(B)
  if r==0 or g==0 or b==0:
    return 0
  else:
    ans=r*g*b
    m=0
    for j in range(1,n-1):
      for k in range(1,min(j,n-1-j)+1):
        lt=s[j-k]
        md=s[j]
        rt=s[j+k]
        if lt!=md and md!=rt and rt!=lt:
          m+=1
    return ans-m
print(f())

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import itertools as it
n = int(input())
s = input()
seed = list(range(0,n))
cmt = list(it.combinations(seed, 3))
ans = 0
for i in range(len(cmt)):
  a = cmt[i]
  if s[a[0]] != s[a[1]] and s[a[1]] != s[a[2]] and s[a[2]] != s[a[0]] and a[1] - a[0] != a[2] - a[1]:
    ans += 1
print(ans)

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
        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()
ans = 0 
for i in range(0,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 s[i] == s[j] or s[j] == s[k] or s[i] == s[k]:
                continue
            elif j - i == k - j:
                continue
            else:
                ans += 1
               
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
# coding: utf-8
# 自分の得意な言語で
# Let's チャレンジ！！
import math
N=int(input())
S=list(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 len({S[i],S[j],S[k]})==3 and 2*j!=i+k:
                    count+=1
            else:
                continue
print(count)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
# coding: utf-8
# 自分の得意な言語で
# Let's チャレンジ！！
import math
N=int(input())
S=list(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 2*j==i+k:
                continue
            else:
                if len({S[i],S[j],S[k]})==3:
                    count+=1
print(count)

Submission status: Time Limit Exceeded

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

cnt = 0
jk = {}
for j in range(1, N-1):
    jk[j] = []
    for k in range(j+1, N):
        if S[j] != S[k]:
            jk[j].append(k)

for i in range(N-2):
    for j in range(i+1, N-1):
        if S[i] == S[j]:
            continue
        ks = jk[j]
        for k in ks:
            if S[i] == S[k]:
                continue
            if j-i != k-j:
                cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

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

ans=r*g*b

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

                    else:
                        break
                        

      
print(ans)

Submission status: Time Limit Exceeded

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

ans=r*g*b

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

      
print(ans)

Submission status: Time Limit Exceeded

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

ans=r*g*b

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

            else:
                break        
print(ans)

Submission status: Time Limit Exceeded

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

ans=r*g*b

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

            else:
                break        
print(ans)

Submission status: Time Limit Exceeded

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

ans=r*g*b

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

Submission status: Time Limit Exceeded

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

ans=r*g*b

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
r_cnt = s.count("R")
g_cnt = s.count("G")
b_cnt = s.count("B")
ans = r_cnt * g_cnt * b_cnt

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

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

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

c=defaultdict(int)
for i in range(N):
  c[S[i]] += 1
ans = c['R']*c['G']*c['B']

max_d = (N-3)//2+1

for d in range(1,max_d+1):
  for i in range(N-2):
    j,k = i+d,i+2*d
    if k > N-1:break
    if len(set([S[i],S[j],S[k]]))==3:
      ans -= 1
      
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

c=Counter(S+'RGB')
ans = (c['R']-1)*(c['G']-1)*(c['B']-1)

max_d = (N-3)//2+1

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

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

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

def main():
    N = int(input())
    S = input()
    RGB = ('R', 'G', 'B')
    Cumsum = [[0] * 3 for i in range(N + 1)]
    for i in range(N):
        Cumsum[i + 1][0] = Cumsum[i][0] + (1 if S[i] == 'R' else 0)
        Cumsum[i + 1][1] = Cumsum[i][1] + (1 if S[i] == 'G' else 0)
        Cumsum[i + 1][2] = Cumsum[i][2] + (1 if S[i] == 'B' else 0)

    ans = 0
    for i in range(N - 2):
        for j in range(i + 1, N - 1):
            if S[i] != S[j]:
                cnt = Cumsum[N][3 - RGB.index(S[i]) - RGB.index(S[j])] - Cumsum[j][3 - RGB.index(S[i]) - RGB.index(S[j])]
                if 2 * j - i < N:
                    if S[2 * j - i] == RGB[3 - RGB.index(S[i]) - RGB.index(S[j])]:
                        cnt -= 1
                ans += cnt
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    S = input()
    RGB = ('R', 'G', 'B')
    ans = 0
    for i in range(N - 2):
        for j in range(i + 1, N - 1):
            if S[i] != S[j]:
                cnt = S[j + 1:].count(RGB[3 - RGB.index(S[i]) - RGB.index(S[j])])
                if 2 * j - i < N:
                    if S[2 * j - i] == RGB[3 - RGB.index(S[i]) - RGB.index(S[j])]:
                        cnt -= 1
                ans += cnt
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

ans = r*g*b

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

print(ans)

Submission status: Time Limit Exceeded

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

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

ans = r*g*b

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

print(ans)

Submission status: Time Limit Exceeded

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

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

ans = r*g*b

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

print(ans)

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

ans = r*g*b

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

print(ans)

Submission status: Time Limit Exceeded

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

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

ans = r*g*b

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

print(ans)

Submission status: Time Limit Exceeded

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

ans = 0

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

print(ans)

Submission status: Time Limit Exceeded

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

ans = 0

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

print(ans)

Submission status: Time Limit Exceeded

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

ans = 0

for i in range(0,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:
from statistics import median
N = int(input())
S = input()
R_lst = []
G_lst = []
B_lst = []
cnt = 0
for i,s in enumerate(S):
    if s == "R":
        R_lst.append(i)
    elif s == "G":
        G_lst.append(i)
    else:
        B_lst.append(i)
for r in R_lst:
    for g in G_lst:
        for b in B_lst:
            mid = median([r,g,b])
            if max(r,g,b) - mid != mid - min(r,g,b):
                cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

N=int(input().rstrip('\n'))
As = list(input().rstrip('\n'))
rs = [i for i, x in enumerate(As) if x == 'R']
bs = [i for i, x in enumerate(As) if x == 'B']
gs = [i for i, x in enumerate(As) if x == 'G']
res = len(rs)*len(gs)*len(bs)
sub = [2*y-x for x in rs for y in bs]
sub1 = [(x+y)/2 for x in rs for y in bs]
sub2 = [2*x-y for x in rs for y in bs]
sub.extend(sub1)
sub.extend(sub2)
sub = [1 for i in sub if i in gs]
print(res-sum(sub))

Submission status: Time Limit Exceeded

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

N=int(input().rstrip('\n'))
As = input().rstrip('\n')
rs = []
bs = []
gs = []
for i in range(N):
  if As[i] =='R':
    rs.append(i)
  elif As[i] == 'G':
    gs.append(i)
  else:
    bs.append(i)
res = len(rs)*len(gs)*len(bs)
for x in rs:
  for y in bs:
    if (x+y)%2==0:
      if (x+y)//2 in gs:
        res -= 1
    if 2*x-y in gs:
      res -= 1
    if 2*y-x in gs:
      res -= 1
print(res)

Submission status: Time Limit Exceeded

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

N=int(input().rstrip('\n'))
As = input().rstrip('\n')
rs = []
bs = []
gs = []
for i in range(N):
  a = As[i]
  if a =='R':
    rs.append(i)
  elif a == 'G':
    gs.append(i)
  else:
    bs.append(i)
res = len(rs)*len(gs)*len(bs)
for x in rs:
  for y in bs:
    if (x+y)%2==0:
      if (x+y)//2 in gs:
        res -= 1
    if 2*x-y in gs:
      res -= 1
    if 2*y-x in gs:
      res -= 1
print(res)

Submission status: Time Limit Exceeded

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

from itertools import combinations
from math import floor
N=int(input().rstrip('\n'))
As = input().rstrip('\n')
rs = []
bs = []
gs = []
for i in range(N):
  a = As[i]
  if a =='R':
    rs.append(i)
  elif a == 'G':
    gs.append(i)
  else:
    bs.append(i)
res = len(rs)*len(gs)*len(bs)
for x in rs:
  for y in bs:
    if (x+y)%2==0:
      if (x+y)//2 in gs:
        res -= 1
    if 2*x-y in gs:
      res -= 1
    if 2*y-x in gs:
      res -= 1
print(res)

Submission status: Time Limit Exceeded

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

from itertools import combinations
from math import floor
N=int(input().rstrip('\n'))
As = list(input().rstrip('\n'))
rs = [i for i, x in enumerate(As) if x == 'R']
bs = [i for i, x in enumerate(As) if x == 'B']
gs = [i for i, x in enumerate(As) if x == 'G']
res = len(rs)*len(gs)*len(bs)
for x in rs:
  for y in bs:
    if (x+y)/2 in gs:
      res -= 1
    if 2*x-y in gs:
      res -= 1
    if 2*y-x in gs:
      res -= 1
print(res)

Submission status: Time Limit Exceeded

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

from itertools import combinations
from math import floor
N=int(input().rstrip('\n'))
As = list(input().rstrip('\n'))
rs = [i for i, x in enumerate(As) if x == 'R']
bs = [i for i, x in enumerate(As) if x == 'B']
gs = [i for i, x in enumerate(As) if x == 'G']
res = len(rs)*len(gs)*len(bs)
for x in rs:
  for y in bs:
    if (x+y)%2==0:
      if (x+y)//2 in gs:
        res -= 1
    if 2*x-y in gs:
      res -= 1
    if 2*y-x in gs:
      res -= 1
print(res)

Submission status: Time Limit Exceeded

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

from itertools import combinations
from math import floor
N=int(input().rstrip('\n'))
As = list(input().rstrip('\n'))
rs = []
bs = []
gs = []
for i in range(N):
  a = As[i]
  if a =='R':
    rs.append(i)
  elif a == 'G':
    gs.append(i)
  else:
    bs.append(i)
res = len(rs)*len(gs)*len(bs)
for x in rs:
  for y in bs:
    if (x+y)%2==0:
      if (x+y)//2 in gs:
        res -= 1
    if 2*x-y in gs:
      res -= 1
    if 2*y-x in gs:
      res -= 1
print(res)

Submission status: Time Limit Exceeded

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

from itertools import combinations
from math import floor
N=int(input().rstrip('\n'))
As = list(input().rstrip('\n'))
rs = []
bs = []
gs = []
for i in range(N):
  a = As[i]
  if a =='R':
    rs.append(i)
  elif a == 'G':
    gs.append(i)
  else:
    bs.append(i)
res = len(rs)*len(gs)*len(bs)
for x in rs:
  for y in bs:
    for z in gs:
      a = abs(x-y)
      b = abs(y-z)
      c = abs(z-x)
      if len(set([abs(x-y),abs(y-z),abs(x-z)]))<3:
        res -= 1
print(res)

Submission status: Time Limit Exceeded

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

from itertools import combinations
from math import floor
N=int(input().rstrip('\n'))
As = list(input().rstrip('\n'))
rs = []
bs = []
gs = []
for i in range(N):
  a = As[i]
  if a =='R':
    rs.append(i)
  elif a == 'G':
    gs.append(i)
  else:
    bs.append(i)
res = 0
for x in rs:
  for y in bs:
    for z in gs:
      if x > y:
        if y > z:
          if x-y != y-z:
            res += 1
        else:
          if x > z:
            if x-z != z-y:
              res += 1
          else:
            if z-x != x-y:
              res += 1
      else:
        if z>y:
          if z-y != y-x:
            res += 1
        else:
          if z>x:
            if y-z != z-x:
              res += 1
          else:
            if y-x != x-z:
              res += 1
print(res)

Submission status: Time Limit Exceeded

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

from itertools import combinations
from math import floor
N=int(input().rstrip('\n'))
As = list(input().rstrip('\n'))
rs = []
bs = []
gs = []
for i in range(N):
  a = As[i]
  if a =='R':
    rs.append(i)
  elif a == 'G':
    gs.append(i)
  else:
    bs.append(i)
res = 0
for x in rs:
  for y in bs:
    for z in gs:
      if x > y:
        if y > z:
          if x-y != y-z:
            res += 1
        else:
          if x > z:
            if x-z != z-y:
              res += 1
          else:
            if z-x != x-y:
              res += 1
      else:
        if z>y:
          if z-y != y-x:
            res += 1
        else:
          if z>x:
            if y-z != z-x:
              res += 1
          else:
            if y-x != x-z:
              res += 1
print(res)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
ini = lambda : int(input())
inm = lambda : map(int,input().split())
inl = lambda : list(map(int,input().split()))
gcd = lambda x,y : gcd(y,x%y) if x%y else y
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])
    if arr==[]:
        arr.append([n, 1])
    return arr
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    divisors.sort()
    return divisors

#maincode-------------------------------------------------
n = ini()
s = input()
ans = 0
for k in range(n):
    for j in range(k):
        if s[k] == s[j]:
            continue
        for i in range(j):
            if j-i == k-j:
                continue
            if s[k] == s[i]:
                continue
            if s[i] == s[j]:
                continue
            ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf,comb
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
from functools import reduce
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n = INT()
s = input()

r = s.count('R')
g = s.count('G')
b = n - r - g
ans = comb(n, 3) - ( comb(r, 2) + comb(g, 2) + comb(b, 2) ) * (n-2) + ( comb(r, 3) + comb(g, 3) + comb(b, 3) ) * 2
for i in range(n):
    d = 1
    j = i + d
    k = j + d
    while k < n:
        t = s[i] + s[j] + s[k]
        if t.count('R') == t.count('G') == 1:
            ans -= 1
        d += 1
        j = i + d
        k = j + d
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
S = input()
rs = [s=="R" for s in S]
gs = [s=="G" for s in S]
bs = [s=="B" for s in S]

ind_r = [i for i,s in enumerate(rs) if s == 1]
ind_g = [i for i,s in enumerate(gs) if s == 1]
ind_b = [i for i,s in enumerate(bs) if s == 1]

ans = 0
ng = sum(gs)
for i in ind_r:
    for j in ind_b:
        # print(i,j)
        kM = max(i,j) + abs(i-j)
        km = min(i,j) - abs(i-j)
        ans += ng
        if (max(i,j) - min(i,j))%2==0:
            # print("aaa",(max(i,j)+min(i,j))//2)
            ans -= gs[(max(i,j)+min(i,j))//2]
        if kM < n:
            ans -= gs[kM]
        if 0 <= km:
            ans -= gs[km]
print(ans)

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):
        if S[i] == S[j]:
            continue
        k = j + (j-i)
        a = list("RGB")
        a.remove(S[i])
        a.remove(S[j])
        x = a[0]
        if k < N and S[k] == x:
            ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
def main():
    N = int(input())
    S = input()

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


if __name__ == '__main__':
    main()