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:

s = input()
n=int(s)
s = input()
ans=0

for i in range(n):
    for j in range(i,n):
        for k in range(j,n):
            if j-i==k-j:
                continue
            else:
                o=s[i]+s[j]+s[k]
                if 'R' in o and 'G' in o and 'B' in o:
                    ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n = int(input())
s = np.array([i for i in input()])

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

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

print(r*g*b-ct)

Submission status: Time Limit Exceeded

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

# 全ての組み合わせ数
allcnt = S.count("R") * S.count("G") * S.count("B")

# 除外するものチェック
rem = 0
for i in range(N-2):
  for j in range(i+1,N-1):
    k = j + (j - i)
    if k >= N:
      break
    if len(set([S[i],S[j],S[k]])) == 3:
      rem += 1
print(allcnt - rem)

Submission status: Time Limit Exceeded

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

# 全ての組み合わせ数
allcnt = S.count("R") * S.count("G") * S.count("B")

# 除外するものチェック
rem = 0
for i in range(N-2):
  for j in range(i+1,N-1):
    k = j + (j - i)
    if k >= N:
      continue
    if len(set([S[i],S[j],S[k]])) == 3:
      rem += 1
print(allcnt - rem)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

r = 0
g = 0
b = 0
for c in s:
    if c == 'R':
        r += 1
    if c == 'G':
        g += 1
    if c == 'B':
        b += 1

total = r*g*b

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

Submission status: Time Limit Exceeded

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

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

comb = r*g*b

for i in range(1, n - 1):
    for j in range(2, n):
        if i < j:
            if (2*j - i) <= n:
                if s[i - 1] != s[j - 1]:
                    if s[i - 1] != s[2*j - i - 1]:
                        if s[j - 1] != s[2*j - i - 1]:
                            comb -= 1

print(comb)

Submission status: Time Limit Exceeded

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

rc = 0
gc = 0
bc = 0

for si in S:
    if si == 'R':
        rc += 1
    elif si == 'G':
        gc += 1
    else:
        bc += 1


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

ans = rc * gc * bc
# print(ans)
ans -= bad
print(ans)

Submission status: Time Limit Exceeded

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

snum = tuple(s[:])

ret =0
for i in range(1,n-1):
    for j in range(i,n):
        if snum[i-1] == snum[j-1]:
            continue
        eq_flg=False
        # j_i = j-i
        skip_k = j*2-i
        for k in range(j,n+1):
            if eq_flg == False:
                # if j_i == k-j:
                if k == skip_k:
                    eq_flg = True
                    continue
            if snum[i-1] == snum[k-1] or snum[j-1] == snum[k-1]:
                continue
            ret += 1

print(ret)

Submission status: Time Limit Exceeded

Solution code:
import math
import collections
import sys

input = sys.stdin.readline
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]

n=I()
s=input()
r=[]
g=[]
t=[]
for i in range(n):
    if s[i:i+1]=="R":
        r.append(i)
    elif s[i:i+1]=="G":
        g.append(i)
    else:
        t.append(i)
R=len(r)
G=len(g)
T=n-R-G
ans=R*G*T
for i in range(n-1):
    for j in range(i+1,n):
        if s[i]!=s[j]:
            if j<2*i-j<=n-1:
                if s[i]!=s[2*i-j] and s[j]!=s[2*i-j]:
                    ans-=1
            if j<2*j-i<=n-1:
                if s[i]!=s[2*j-i] and s[j]!=s[2*j-i]:
                    ans-=1
            if (i+j)%2==0 and j<(i+j)//2 and s[i]!=s[(i+j)//2] and s[j]!=s[(i+j)//2]:
                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+1)
    elif s=='G': G.append(i+1)
    else: B.append(i+1)

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

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
from collections import deque
from string import ascii_uppercase
import sys, bisect, math, heapq

stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
read_str = lambda : stdin.readline().rstrip()

N = read_int()[0]
S = read_str()

r = []
g = []
b = []

rap = r.append
gap = g.append
bap = b.append
for i, s in enumerate(S):
    if s == 'R': rap(i)
    elif s == 'G': gap(i)
    else: bap(i)
rgb = [r, g, b]

# print(r, g, b)

def bisect_find_lt(a, x):
    'Find rightmost value less than x'
    i = bisect.bisect_left(a, x)
    if i:
        return i-1
    raise ValueError

def bisect_find_gt(a, x):
    'Find leftmost value greater than x'
    i = bisect.bisect_right(a, x)
    if i != len(a):
        return i
    raise ValueError

def solve():
    ans = 0
    lb = len(b)
    lg = len(g)
    lr = len(r)
    if len(r) == 0 or len(g) == 0 or len(b) == 0:
        return ans
    for i, ng in enumerate(g):
        try:
            ri = bisect_find_lt(r, ng)
            bi = bisect_find_gt(b, ng)
            # print(ri, bi)
            for rj in r[0 : ri + 1]:
                for bj in b[bi : lb]:
                    # print(rj, ng, bj)
                    if ng - rj != bj - ng:
                        ans += 1
        except ValueError:
            pass

    for i, ng in enumerate(g):
        try:
            ri = bisect_find_lt(b, ng)
            bi = bisect_find_gt(r, ng)
            # print(ri, bi)
            for rj in b[0 : ri + 1]:
                for bj in r[bi : lr]:
                    # print(rj, ng, bj)
                    if ng - rj != bj - ng:
                        ans += 1
        except ValueError:
            pass

    for i, ng in enumerate(b):
        try:
            ri = bisect_find_lt(g, ng)
            bi = bisect_find_gt(r, ng)
            # print(ri, bi)
            for rj in g[0 : ri + 1]:
                for bj in r[bi: lr]:
                    # print(rj, ng, bi)
                    if ng - rj != bj - ng:
                        ans += 1
        except ValueError:
            pass

    for i, ng in enumerate(b):
        try:
            ri = bisect_find_lt(r, ng)
            bi = bisect_find_gt(g, ng)
            # print(ri, bi)
            for rj in r[0 : ri + 1]:
                for bj in g[bi : lg]:
                    # print(rj, ng, bi)
                    if ng - rj != bj - ng:
                        ans += 1
        except ValueError:
            pass

    for i, ng in enumerate(r):
        try:
            ri = bisect_find_lt(b, ng)
            bi = bisect_find_gt(g, ng)
            # print(ri, bi)
            for rj in b[0 : ri + 1]:
                for bj in g[bi : lg]:
                    # print(rj, ng, bi)
                    if ng - rj != bj - ng:
                        ans += 1
        except ValueError:
            pass

    for i, ng in enumerate(r):
        try:
            ri = bisect_find_lt(g, ng)
            bi = bisect_find_gt(b, ng)
            # print(ri, bi)
            for rj in g[0 : ri + 1]:
                for bj in b[bi : lb]:
                    # print(rj, ng, bi)
                    if ng - rj != bj - ng:
                        ans += 1
        except ValueError:
            pass


    return ans

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

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

Submission status: Time Limit Exceeded

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

snum = tuple(s[:])
ret =0
for i in range(1,n-1):
    for j in range(i,n):
        if snum[i-1] == snum[j-1]:
            continue
        for k in range(j,n+1):
            if j-i == k-j:
                continue
            if snum[i-1] == snum[k-1]:
                continue
            if snum[j-1] == snum[k-1]:
                continue
            ret += 1

print(ret)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

import sys
args = sys.argv

debug_flg = False
def debug(val1,val2="",val3="",val4=""):
    global debug_flg
    if debug_flg:
        print(val1,val2,val3,val4,sep=" , ")

if len(args) != 2:
    n= int(input())
    s= input()
else:
    debug_flg = True
    if args[1] == "2":
        n=39
        s="RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB"
    elif args[1] == "3":
        n=1
        s="R"
    else:
        n=4
        s="RRGB"

snum = []
for i in range(1,n+1):
    if s[i-1] == "R":
        snum.append(0)
    elif s[i-1] == "G":
        snum.append(1)
    elif s[i-1] == "B":
        snum.append(2)
snum = tuple(snum)

ret =0
for i in range(1,n-1):
    for j in range(i,n):
        if snum[i-1] == snum[j-1]:
            continue
        for k in range(j,n+1):
            if j-i == k-j:
                continue
            if snum[i-1] == snum[k-1]:
                continue
            if snum[j-1] == snum[k-1]:
                continue
            ret += 1

print(ret)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

rgb = input()

r = []
g = []
B = []

for i in range(n):
    if rgb[i] == "R":
        r.append(i + 1)
    elif rgb[i] == "G":
        g.append(i + 1)
    elif rgb[i] == "B":
        B.append(i + 1)

ans = 0
for a in r:
    for b in g:
        for c in B:
            aaa = sorted(list([a,b,c]))

            if (aaa[0] + aaa[2]) / 2  == aaa[1]:continue
            ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

if n <= 3:
    print(0)
    exit()

Rs = []
Gs = []
Bs = []

r = 0
g = 0
b = 0

for s in S:
    if s == 'R':
        r += 1
    if s == 'G':
        g += 1
    if s == 'B':
        b += 1

    Rs.append(r)
    Gs.append(g)
    Bs.append(b)

ans = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        if S[i] == S[j]:
            continue
        color = {'R', 'G', 'B'}
        color.discard(S[i])
        color.discard(S[j])
        desire = color.pop()

        pattern = 0
        if desire == 'R':
            pattern = Rs[-1] - Rs[j]
        elif desire == 'G':
            pattern = Gs[-1] - Gs[j]
        elif desire == 'B':
            pattern = Bs[-1] - Bs[j]
        delta = j-i
        if j+delta < n:
            if S[j+delta] == desire:
                pattern -= 1
        ans += pattern

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

import sys
args = sys.argv

debug_flg = False
def debug(val1,val2="",val3="",val4=""):
    global debug_flg
    if debug_flg:
        print(val1,val2,val3,val4,sep=" , ")

if len(args) != 2:
    n= int(input())
    s= input()
else:
    debug_flg = True
    if args[1] == "2":
        n=39
        s="RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB"
    elif args[1] == "3":
        n=1
        s="R"
    else:
        n=4
        s="RRGB"

snum = []
for i in range(1,n+1):
    if s[i-1] == "R":
        snum.append(0)
    elif s[i-1] == "G":
        snum.append(1)
    elif s[i-1] == "B":
        snum.append(2)

ret =0
for i in range(1,n-1):
    for j in range(i,n):
        if snum[i-1] == snum[j-1]:
            continue
        for k in range(j,n+1):
            if j-i == k-j:
                continue
            if snum[i-1] == snum[k-1]:
                continue
            if snum[j-1] == snum[k-1]:
                continue
            ret += 1

print(ret)

Submission status: Time Limit Exceeded

Solution code:
import sys
import fractions
import numpy as np

def in_ctrl():
    _input = [_.rstrip() for _ in sys.stdin.readlines()]
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    N = int(_input[0])
    S = _input[1]
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    return N,S


def out_ctrl(s):
    print(s)


if __name__ == "__main__":
    N,S = in_ctrl()
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    cnt = 0
    #R = list(map(bool,list(map(int,list(S.replace('R','1').replace('G','0').replace('B','0'))))))
    #G = list(map(bool,list(map(int,list(S.replace('R','0').replace('G','1').replace('B','0'))))))
    #B = list(map(bool,list(map(int,list(S.replace('R','0').replace('G','0').replace('B','1'))))))
    R = np.array(list(S.replace('R','1').replace('G','0').replace('B','0'))).astype(bool)
    G = np.array(list(S.replace('R','0').replace('G','1').replace('B','0'))).astype(bool)
    B = np.array(list(S.replace('R','0').replace('G','0').replace('B','1'))).astype(bool)
    for i,r in enumerate(R):
        if r:
            for j,g in enumerate(G[i+1:]):
                if g:
                    if i+2*j+2 < N:
                        cnt += np.sum(B[i+j+2:])-B[i+2*j+2]
                    else:
                        cnt += np.sum(B[i+j+2:])
            for j,g in enumerate(B[i+1:]):
                if g:
                    if i+2*j+2 < N:
                        cnt += np.sum(G[i+j+2:])-G[i+2*j+2]
                    else:
                        cnt += np.sum(G[i+j+2:])
    for i,r in enumerate(G):
        if r:
            for j,g in enumerate(R[i+1:]):
                if g:
                    if i+2*j+2 < N:
                        cnt += np.sum(B[i+j+2:])-B[i+2*j+2]
                    else:
                        cnt += np.sum(B[i+j+2:])
            for j,b in enumerate(B[i+1:]):
                if b:
                    if i+2*j+2 < N:
                        cnt += np.sum(R[i+j+2:])-R[i+2*j+2]
                    else:
                        cnt += np.sum(R[i+j+2:])
    for i,r in enumerate(B):
        if r:
            for j,g in enumerate(R[i+1:]):
                if g:
                    if i+2*j+2 < N:
                        cnt += np.sum(G[i+j+2:])-G[i+2*j+2]
                    else:
                        cnt += np.sum(G[i+j+2:])
            for j,b in enumerate(G[i+1:]):
                if b:
                    if i+2*j+2 < N:
                        cnt += np.sum(R[i+j+2:])-R[i+2*j+2]
                    else:
                        cnt += np.sum(R[i+j+2:])
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    out_ctrl(cnt)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

import sys
args = sys.argv

debug_flg = False
def debug(val1,val2="",val3="",val4=""):
    global debug_flg
    if debug_flg:
        print(val1,val2,val3,val4,sep=" , ")

if len(args) != 2:
    n= int(input())
    s= input()
else:
    debug_flg = True
    if args[1] == "2":
        n=39
        s="RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB"
    elif args[1] == "3":
        n=1
        s="R"
    else:
        n=4
        s="RRGB"

snum = []
for i in range(1,n+1):
    if s[i-1] == "R":
        snum.append(0)
    elif s[i-1] == "G":
        snum.append(1)
    elif s[i-1] == "B":
        snum.append(2)

ret =0
for i in range(1,n-1):
    for j in range(i,n):
        if snum[i-1] == snum[j-1]:
            continue
        for k in range(j,n+1):
            if j-i == k-j:
                continue
            if snum[i-1] == snum[k-1]:
                continue
            if snum[j-1] == snum[k-1]:
                continue
            ret += 1

print(ret)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter


def main():

    N = int(input())
    S = input()
    count = Counter(S)

    # print(count)
    ans = 0
    R = count["R"]
    G = count["G"]
    B = count["B"]
    ans = R * G * B

    # for i in range(N - 2):
    #     if S[i] == "R":
    #         for j in range(i + 1, N - 1):
    #             if S[j] == "G":
    #                 b_count = Counter(S[j:])["B"]
    #                 ans += b_count

    # print(R, G, B, ans)

    for i in range(N):
        for j in range(N):
            if i + j * 2 < N and S[i] == "R" and S[i + j] == "G" and S[i + j * 2] == "B":
                ans -= 1
            if i + j * 2 < N and S[i] == "R" and S[i + j] == "B" and S[i + j * 2] == "G":
                ans -= 1
            if i + j * 2 < N and S[i] == "G" and S[i + j] == "R" and S[i + j * 2] == "B":
                ans -= 1
            if i + j * 2 < N and S[i] == "G" and S[i + j] == "B" and S[i + j * 2] == "R":
                ans -= 1
            if i + j * 2 < N and S[i] == "B" and S[i + j] == "G" and S[i + j * 2] == "R":
                ans -= 1
            if i + j * 2 < N and S[i] == "B" and S[i + j] == "R" and S[i + j * 2] == "G":
                ans -= 1
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
import fractions

def in_ctrl():
    _input = [_.rstrip() for _ in sys.stdin.readlines()]
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    N = int(_input[0])
    S = _input[1]
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    return N,S


def out_ctrl(s):
    print(s)


if __name__ == "__main__":
    N,S = in_ctrl()
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    cnt = 0
    R = list(map(bool,list(map(int,list(S.replace('R','1').replace('G','0').replace('B','0'))))))
    G = list(map(bool,list(map(int,list(S.replace('R','0').replace('G','1').replace('B','0'))))))
    B = list(map(bool,list(map(int,list(S.replace('R','0').replace('G','0').replace('B','1'))))))
    for i,r in enumerate(R):
        if r:
            for j,g in enumerate(G[i+1:]):
                if g:
                    if i+2*j+2 < N:
                        if B[i+2*j+2]:
                            cnt += sum(B[i+j+2:])-1
                        else:
                            cnt += sum(B[i+j+2:])
                    else:
                        cnt += sum(B[i+j+2:])
            for j,g in enumerate(B[i+1:]):
                if g:
                    if i+2*j+2 < N:
                        if G[i+2*j+2]:
                            cnt += sum(G[i+j+2:])-1
                        else:
                            cnt += sum(G[i+j+2:])
                    else:
                        cnt += sum(G[i+j+2:])
    for i,r in enumerate(G):
       if r:
           for j,g in enumerate(R[i+1:]):
               if g:
                    if i+2*j+2 < N:
                        if B[i+2*j+2]:
                            cnt += sum(B[i+j+2:])-1
                        else:
                            cnt += sum(B[i+j+2:])
                    else:
                        cnt += sum(B[i+j+2:])
           for j,b in enumerate(B[i+1:]):
               if b:
                    if i+2*j+2 < N:
                        if R[i+2*j+2]:
                            cnt += sum(R[i+j+2:])-1
                        else:
                            cnt += sum(R[i+j+2:])
                    else:
                        cnt += sum(R[i+j+2:])
    for i,r in enumerate(B):
       if r:
           for j,g in enumerate(R[i+1:]):
               if g:
                    if i+2*j+2 < N:
                        if G[i+2*j+2]:
                            cnt += sum(G[i+j+2:])-1
                        else:
                            cnt += sum(G[i+j+2:])
                    else:
                        cnt += sum(G[i+j+2:])
           for j,b in enumerate(G[i+1:]):
               if b:
                    if i+2*j+2 < N:
                        if R[i+2*j+2]:
                            cnt += sum(R[i+j+2:])-1
                        else:
                            cnt += sum(R[i+j+2:])
                    else:
                        cnt += sum(R[i+j+2:])
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    out_ctrl(cnt)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

import sys
args = sys.argv

debug_flg = False
def debug(val1,val2="",val3="",val4=""):
    global debug_flg
    if debug_flg:
        print(val1,val2,val3,val4,sep=" , ")

if len(args) != 2:
    n= int(input())
    s= input()
else:
    debug_flg = True
    if args[1] == "2":
        n=39
        s="RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB"
    else:
        n=4
        s="RRGB"

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

print(ret)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = 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']
sum = len(r)*len(g)*len(b)

for i in r:
  for j in g:
    for k in b:
      d, e, f = sorted([i, j, k])
      if e-d == f-e:
        sum -= 1
print(sum)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

import sys
args = sys.argv

debug_flg = False
def debug(val1,val2="",val3="",val4=""):
    global debug_flg
    if debug_flg:
        print(val1,val2,val3,val4,sep=" , ")

if len(args) != 2:
    n= int(input())
    s= input()
else:
    debug_flg = True
    if args[1] == "2":
        n=39
        s="RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB"
    else:
        n=4
        s="RRGB"

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

print(ret)

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)
    else:
        B.append(i+1)
r=len(R)
g=len(G)
b=len(B)

c=0
for i in R:
    for j in B:
        min_n=min(i,j)
        max_n=max(i,j)
        d=abs(i-j)
        if max_n+d in G:
            c+=1
        if (i+j)%2==0 and (i+j)//2 in G:
            c+=1
        if min_n-d in G:
            c+=1

print(r*g*b-c)

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)
    else:
        B.append(i+1)
r=len(R)
g=len(G)
b=len(B)

c=0
for i in G:
    for j in B:
        min_n=min(i,j)
        max_n=max(i,j)
        d=abs(i-j)
        if max_n+d in R:
            c+=1
        if (i+j)%2==0 and (i+j)//2 in R:
            c+=1
        if min_n-d in R:
            c+=1

print(r*g*b-c)

Submission status: Time Limit Exceeded

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

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
    for k in range(j+1,n) :
      sk = s[k]
      if si == sk or sj == sk or j-i == k-j :
        continue
      ans += 1
      
print(ans)

Submission status: Time Limit Exceeded

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

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

print(count)

Submission status: Time Limit Exceeded

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

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

print(count)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import math 
n = int(input())
s = input()
a = np.zeros(n)
rgb = [[] for _ in range(3)]
rgb2 = np.zeros((3,n), int)
idx = {'R':0, 'G':1, 'B':2}
for i in range(n):
  rgb[idx[s[i]]].append(i)
  rgb2[idx[s[i]]][i] = 1
#

counts = 0
for i in rgb[0]:
  for j in rgb[1]:
    if(i+j >= 2*n-1):
      break
    if((i+j)%2 == 0 and rgb2[2][((i+j)//2)] == 1):
        counts += 1
for i in rgb[0]:
  for j in rgb[2]:
    if(i+j >= 2*n-1):
      break
    if((i+j)%2 == 0 and rgb2[1][(i+j)//2] == 1):
      counts += 1
for i in rgb[1]:
  for j in rgb[2]:
    if(i+j >= 2*n-1):
      break
    if((i+j)%2 == 0 and rgb2[0][(i+j)//2] == 1):
      counts += 1
print(len(rgb[0])*len(rgb[1])*len(rgb[2]) - counts)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n = int(input())
s = input()
a = np.zeros(n)
rgb = [[] for _ in range(3)]
rgb2 = np.zeros((3,n), int)
idx = {'R':0, 'G':1, 'B':2}
for i in range(n):
  rgb[idx[s[i]]].append(i)
#

middles = [[] for _ in range(3)]
for i in rgb[0]:
  for j in rgb[1]:
    if(i+j >= 2*n-1):
      break
    if((i+j)%2 == 0):
      middles[idx['B']].append((i+j)//2)
for i in rgb[0]:
  for j in rgb[2]:
    if(i+j >= 2*n-1):
      break
    if((i+j)%2 == 0):
      middles[idx['G']].append((i+j)//2)
for i in rgb[1]:
  for j in rgb[2]:
    if(i+j >= 2*n-1):
      break
    if((i+j)%2 == 0):
      middles[idx['R']].append((i+j)//2)
#
counts = 0
for i in range(3):
  tmp,c = np.unique(middles[i], return_counts=True)
  for j,c in zip(tmp,c):
    if(j in rgb[i]):
      counts += c
print(len(rgb[0])*len(rgb[1])*len(rgb[2]) - counts)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import math 
n = int(input())
s = input()
a = np.zeros(n)
rgb = [[] for _ in range(3)]
rgb2 = np.zeros((3,n), int)
idx = {'R':0, 'G':1, 'B':2}
for i in range(n):
  rgb[idx[s[i]]].append(i)
  rgb2[idx[s[i]]][i] = 1
#

middles = [[] for _ in range(3)]
for i in rgb[0]:
  for j in rgb[1]:
    if(i+j >= 2*n-1):
      break
    if((i+j)%2 == 0):
      middles[idx['B']].append((i+j)//2)
for i in rgb[0]:
  for j in rgb[2]:
    if(i+j >= 2*n-1):
      break
    if((i+j)%2 == 0):
      middles[idx['G']].append((i+j)//2)
for i in rgb[1]:
  for j in rgb[2]:
    if(i+j >= 2*n-1):
      break
    if((i+j)%2 == 0):
      middles[idx['R']].append((i+j)//2)
#
counts = 0
for i in range(3):
  tmp,c = np.unique(middles[i], return_counts=True)
  for j,c in zip(tmp,c):
    if(j in rgb[i]):
      counts += c
print(len(rgb[0])*len(rgb[1])*len(rgb[2]) - counts)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import math 
n = int(input())
s = input()
a = np.zeros(n)
rgb = [[] for _ in range(3)]
rgb2 = np.zeros((3,n), int)
idx = {'R':0, 'G':1, 'B':2}
for i in range(n):
  rgb[idx[s[i]]].append(i)
  rgb2[idx[s[i]]][i] = 1
#

counts = 0
for i in rgb[0]:
  for j in rgb[1]:
    if(i+j >= 2*n-1):
      break
    if((i+j)%2 == 0 and rgb2[idx['B']][((i+j)//2)] == 1):
        counts += 1
for i in rgb[0]:
  for j in rgb[2]:
    if(i+j >= 2*n-1):
      break
    if((i+j)%2 == 0 and rgb2[idx['G']][(i+j)//2] == 1):
      counts += 1
for i in rgb[1]:
  for j in rgb[2]:
    if(i+j >= 2*n-1):
      break
    if((i+j)%2 == 0 and rgb2[idx['R']][(i+j)//2] == 1):
      counts += 1
print(len(rgb[0])*len(rgb[1])*len(rgb[2]) - counts)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
c = 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:
                c += 1
print(c)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import collections

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

aaa = collections.Counter(s)
s_r = aaa["R"]
s_g = aaa["G"]
s_b = aaa["B"]

s = s.replace("R", "0").replace("G", "1").replace("B", "2")

def noteq(a, b, c):
  if a != b and b != c and c != a:
    return True
  else:
    return False

ans = s_r * s_g * s_b

for i in range(n):
  for d in range(n):
    j = i + d
    k = j + d
    if k >= n:
      break;
    else:
      a = int(s[i])
      b = int(s[j])
      c = int(s[k])
      if noteq(a, b, c):
        ans -= 1
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections

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

aaa = collections.Counter(s)
s_r = aaa["R"]
s_g = aaa["G"]
s_b = aaa["B"]

s = s.replace("R", "0").replace("G", "1").replace("B", "2")

def noteq(a, b, c):
  if a != b and b != c and c != a:
    return True
  else:
    return False

ans = s_r * s_g * s_b

for i in range(n):
  for j in range(i+1, n):
    if 2*j - i >= n:
      break;
    else:
      a = int(s[i])
      b = int(s[j])
      c = int(s[2*j-i])
      if noteq(a, b, c):
        ans -= 1
    
print(ans)