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

N = int(input())
S = input()
ans = 0

count = collections.Counter(S)

R_list = [count['R']]
G_list = [count['G']]
B_list = [count['B']]

for i in range(N):
    if S[i] == 'R':
        R_list.append(R_list[i] - 1)
        G_list.append(G_list[i])
        B_list.append(B_list[i])
    elif S[i] == 'G':
        R_list.append(R_list[i])
        G_list.append(G_list[i] - 1)
        B_list.append(B_list[i])
    else:
        R_list.append(R_list[i])
        G_list.append(G_list[i])
        B_list.append(B_list[i] - 1)

for i in range(N - 2):
    for j in range(i, N - 1):
        rest_str = {'R', 'G', 'B'} - {S[i], S[j]}
        
        if len(rest_str) != 1:
            continue
        
        if rest_str == {'B'}:
            ans += B_list[j + 1]
        elif rest_str == {'R'}:
            ans += R_list[j + 1]
        elif rest_str == {'G'}:
            ans += G_list[j + 1]
        
        if j + (j - i) <= N - 1:
            if {S[i], S[j], S[2 * j - i]} == {'R', 'B', 'G'}:
                ans -= 1
                
print(ans)

Submission status: Time Limit Exceeded

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

enum = {"R": 0, "G": 1, "B": 2}
counter = [[0, 0, 0]] * (n + 1)
for i in range(n):
    counter[i+1] = counter[i].copy()
    counter[i+1][enum[s[i]]] += 1
counter.pop(0)

ans = 0
for i in range(n):
    for j in range(i + 1, n):
        if s[i] == s[j]:
            continue
        entries = ["R", "G", "B"]
        entries.remove(s[i])
        entries.remove(s[j])
        other_index = enum[entries[0]]
        other = counter[-1][other_index] - counter[j][other_index]
        if j + abs(j-i) < n  and s[j + abs(j-i)] == entries[0]:
            other -= 1
        ans += other


print(ans)

Submission status: Time Limit Exceeded

Solution code:
# this is a good problem !

import sys
si = sys.stdin.readline

d = ['R', 'G', 'B']
ld = [[], [], []]  # letter distribution


def main():
    count = 0
    n = int(si())
    l = list(si())
    l.pop()
    dc = d[:]

    for i in range(n-1, -1, -1):
        if i == n-1:
            if l[i] == 'R':
                ld[0].append(1)
                ld[1].append(0)
                ld[2].append(0)
            elif l[i] == 'G':
                ld[0].append(0)
                ld[1].append(1)
                ld[2].append(0)
            else:
                ld[0].append(0)
                ld[1].append(0)
                ld[2].append(1)
        else:
            if l[i] == 'R':
                ld[0] = [ld[0][0]+1]+ld[0]
                ld[1] = [ld[1][0]+0]+ld[1]
                ld[2] = [ld[2][0]+0]+ld[2]
            elif l[i] == 'G':
                ld[0] = [ld[0][0]+0]+ld[0]
                ld[1] = [ld[1][0]+1]+ld[1]
                ld[2] = [ld[2][0]+0]+ld[2]
            else:
                ld[0] = [ld[0][0]+0]+ld[0]
                ld[1] = [ld[1][0]+0]+ld[1]
                ld[2] = [ld[2][0]+1]+ld[2]

    for i in range(n-2):
        r1 = l[i]
        dc.remove(r1)
        for j in range(i+1, n-1):
            if l[j] in dc:
                r2 = l[j]
                dc.remove(r2)
                count += ld[d.index(dc[0])][j+1]
                if (j+j-i) < n and l[j+j-i] in dc:
                    count -= 1
                dc += [r2]
        dc += [r1]
    print(count)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

enum = {"R": 0, "G": 1, "B": 2}
counter = [[0, 0, 0]]
for i in range(n):
    counter.append(counter[-1].copy())
    counter[i+1][enum[s[i]]] += 1
counter.pop(0)

ans = 0
for i in range(n):
    for j in range(i + 1, n):
        if s[i] == s[j]:
            continue
        entries = ["R", "G", "B"]
        entries.remove(s[i])
        entries.remove(s[j])
        other_index = enum[entries[0]]
        other = counter[-1][other_index] - counter[j][other_index]
        if j + abs(j-i) < n  and s[j + abs(j-i)] == entries[0]:
            other -= 1
        ans += other


print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect
N=int(input())
S=list(input())
ans=0
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)
B.append(10000)
lenb=len(B)-1
for r in R:
    for g in G:
        flag=0
        left=min(r,g)-abs(r-g)
        right=max(r,g)+abs(r-g)
        if (r+g)%2==0:
            middle=(r+g)//2
        else:
            middle=0
        for i in (left,right,middle):
            if i>0:
                a=bisect.bisect_left(B,i)
                if B[a]==i:
                    flag=flag+1
        ans=ans+lenb-flag
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？

n=ii()
s=list(ss())
rarr = []
garr=[]
barr=[]

for i in range(n):
    if s[i]=="R":
        rarr.append(i)
    elif s[i]=="G":
        garr.append(i)
    else:
        barr.append(i)

#組み合わせ全通り
ans=len(rarr)*len(barr)*len(garr)

#[0, 2, 5, 11, 12, 16, 17, 18, 23, 27, 31] 11
#[4, 7, 8, 13, 20, 21, 25, 29, 35] 9
#[1, 3, 6, 9, 10, 14, 15, 19, 22, 24, 26, 28, 30, 32, 33, 34, 36, 37, 38] 19

for r in rarr:
    for b in barr:
        #GRB
        if r-b >0 and (r + r-b in garr):
            ans-=1

        #GBR
        elif b-r >0 and (b + b-r in garr):
            ans-=1

        #RGB BGR
        if r-b >0 and (r-b)%2==0 and b+((r-b)//2) in garr:
            ans-=1

        elif b-r >0 and (b-r)%2==0 and r+((b-r)//2) in garr:
            ans-=1

        #RBG
        if r-b>0 and b-(r-b) in garr:
            ans-=1

        elif b-r>0 and r-(b-r) in garr:
            ans-=1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections

N = int(input())
S = input()
ans = 0

count = collections.Counter(S)

R_list = [count['R']]
G_list = [count['G']]
B_list = [count['B']]

for i in range(N):
    if S[i] == 'R':
        R_list.append(R_list[i] - 1)
        G_list.append(G_list[i])
        B_list.append(B_list[i])
    elif S[i] == 'G':
        R_list.append(R_list[i])
        G_list.append(G_list[i] - 1)
        B_list.append(B_list[i])
    else:
        R_list.append(R_list[i])
        G_list.append(G_list[i])
        B_list.append(B_list[i] - 1)

for i in range(N - 2):
    for j in range(i, N - 1):
        if {S[i], S[j]} == {'R', 'G'}:
            ans += B_list[j + 1]
        elif {S[i], S[j]} == {'G', 'B'}:
            ans += R_list[j + 1]
        elif {S[i], S[j]} == {'B', 'R'}:
            ans += G_list[j + 1]
        
        if j + (j - i) <= N -1:
            if {S[i], S[j], S[2 * j - i]} == {'R', 'B', 'G'}:
                ans -= 1
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import bisect
N=int(input())
S=list(input())
ans=0
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)
B.append(10000)
lenb=len(B)-1
for r in R:
    for g in G:
        flag=0
        left=max(min(r,g)-abs(r-g),0)
        right=max(r,g)+abs(r-g)
        if (r+g)%2==0:
            middle=(r+g)//2
        else:
            middle=0
        for i in (left,right,middle):
            if i!=0:
                a=bisect.bisect_left(B,i)
                if B[a]==i:
                    flag=flag+1
        ans=ans+lenb-flag
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!

_ = int(input())
RGB= list(input())

count = 0
sub = 0

r_list = []
g_list = []
b_list = []
for val in range(0,len(RGB)):
    if "R" == RGB[val]:
        r_list.append(val)
    elif "G" == RGB[val]:
        g_list.append(val)
    elif "B" == RGB[val]:
        b_list.append(val)

for index, a in enumerate(RGB):
    if a == "R":
        del g_list[0:len([i for i in g_list if i < index])]
        del b_list[0:len([i for i in b_list if i < index])]
        for g in g_list:
            for b in b_list:
                if g-index == b-g or b-index == g-b:
                    sub +=1
        count = count + len(g_list)*len(b_list)
                    
    elif a == "G":
        del b_list[0:len([i for i in b_list if i < index])]
        del r_list[0:len([i for i in r_list if i < index])]
        for b in b_list:
            for r in r_list:
                if b-index == r-b or r-index == b-r:
                    sub +=1
        count = count + len(b_list)*len(r_list)
                
    elif a == "B":
        del r_list[0:len([i for i in r_list if i < index])]
        del g_list[0:len([i for i in g_list if i < index])]
        for r in r_list:
            for g in g_list:
                if r-index == g-r or g-index == r-g:
                    sub +=1
        count = count + len(g_list)*len(r_list) 
        
print(count-sub)

Submission status: Time Limit Exceeded

Solution code:
#D

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

ans = 0

if n < 4:
    print(0)
    exit()

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!

_ = int(input())
RGB= list(input())

count = 0
sub = 0

def index_Multi(List):
    r_list = []
    g_list = []
    b_list = []
    for val in range(0,len(List)):
        if "R" == List[val]:
            r_list.append(val)
        elif "G" == List[val]:
            g_list.append(val)
        elif "B" == List[val]:
            b_list.append(val)
    return r_list, g_list, b_list
    
r_list, g_list ,b_list = index_Multi(RGB)

for index, a in enumerate(RGB):
    if a == "R":
        del g_list[0:len([i for i in g_list if i < index])]
        del b_list[0:len([i for i in b_list if i < index])]
        for g in g_list:
            for b in b_list:
                if g-index == b-g or b-index == g-b:
                    sub +=1
        count = count + len(g_list)*len(b_list)
                    
    elif a == "G":
        del b_list[0:len([i for i in b_list if i < index])]
        del r_list[0:len([i for i in r_list if i < index])]
        for b in b_list:
            for r in r_list:
                if b-index == r-b or r-index == b-r:
                    sub +=1
        count = count + len(b_list)*len(r_list)
                
    elif a == "B":
        del r_list[0:len([i for i in r_list if i < index])]
        del g_list[0:len([i for i in g_list if i < index])]
        for r in r_list:
            for g in g_list:
                if r-index == g-r or g-index == r-g:
                    sub +=1
        count = count + len(g_list)*len(r_list) 
        
print(count-sub)

Submission status: Time Limit Exceeded

Solution code:
import sys
#import copy
import numpy as np
#import itertools
#import collections
#from collections import deque
#from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson
#from scipy.sparse import csr_matrix

sys.setrecursionlimit(10**6)
readline = sys.stdin.buffer.readline
read = sys.stdin.buffer.read

def main():
    N = int(readline())
    S = np.array(list(read().rstrip()), np.int8)

    #S = np.array(list(readline()))
    #print(S)

    R = np.sum(S == ord('R'))
    G = np.sum(S == ord('G'))
    B = np.sum(S == ord('B'))

    ans = R*G*B

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

    print(ans)
    #print('end')


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import math
import sys
import collections
def main():
    N=int(input())
    Tr=tuple(input())
    R=[]
    G=[]
    B=[]
    for i in range(N):
       if Tr[i]=="R":
           R.append(i)
       if Tr[i]=="G":
           G.append(i)
       if Tr[i]=="B":
           B.append(i)
    cnt=len(R)*len(G)*len(B)
    for i in R:
        for j in G:
            if max(i,j)+abs(i-j) in B:
                cnt-=1
            if min(i,j)-abs(i-j) in B:
                cnt-=1
            if (i+j)%2==0 and (i+j)//2 in B:
                cnt-=1
    print(cnt)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
char = ""
count = 0

for i in range(len(s)):
    for j in range(i + 1, len(s)):
        if s[j] != s[i]:

            if s[i] == "R":
               if s[j] == "G": char = "B"
               else: char = "G"
            elif s[i] == "G":
                if s[j] == "R": char = "B"
                else: char = "R"
            else:   # s[i] は B
                if s[j] == "G": char = "R"
                else: char = "G"
            a = j - i

            for k in range(j + 1, len(s)):     
                if s[k] == char and a != k - j:
                    count += 1
print(count)

Submission status: Time Limit Exceeded

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

num = [[0]*N for i in range(3)]
if S[N-1] == 'R':
    num[0][N-1] += 1
elif S[N-1] == 'G':
    num[1][N-1] += 1
else:
    num[2][N-1] += 1
    
for i in range(2,N+1):
    num[0][-i] = num[0][-i+1]
    num[1][-i] = num[1][-i+1]
    num[2][-i] = num[2][-i+1]
    if S[-i] == 'R':
        num[0][-i] += 1
    elif S[-i] == 'G':
        num[1][-i] += 1
    else:
        num[2][-i] += 1

ans = 0
for i in range(N-2):
    for j in range(i+1,N-1):
        if S[i] != S[j]:
            if S[i] != 'R' and S[j] != 'R':
                ans += num[0][j+1]
                if 2*j - i < N:
                    if S[2*j - i] == 'R':
                        ans -= 1
                        
            elif S[i] != 'G' and S[j] != 'G':
                ans += num[1][j+1]
                if 2*j - i < N:
                    if S[2*j - i] == 'G':
                        ans -= 1
                        
            elif S[i] != 'B' and S[j] != 'B':
                ans += num[2][j+1]
                if 2*j - i < N:
                    if S[2*j - i] == 'B':
                        ans -= 1
                    
print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
#D

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

ans = 0

if n < 4:
    print(0)
    exit()

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!

N = int(input())
RGB= list(input())

count = 0
sub = 0

def index_Multi(List):
    r_list = []
    g_list = []
    b_list = []
    for val in range(0,len(List)):
        if "R" == List[val]:
            r_list.append(val)
        elif "G" == List[val]:
            g_list.append(val)
        elif "B" == List[val]:
            b_list.append(val)
    return r_list, g_list, b_list
    
r_list, g_list ,b_list = index_Multi(RGB)

for index, a in enumerate(RGB):
    if a == "R":
        del g_list[0:len([i for i in g_list if i < index])]
        del b_list[0:len([i for i in b_list if i < index])]
        for g in g_list:
            for b in b_list:
                if g-index == b-g or b-index == g-b:
                    sub +=1
        count = count + len(g_list)*len(b_list)
                    
    elif a == "G":
        del b_list[0:len([i for i in b_list if i < index])]
        del r_list[0:len([i for i in r_list if i < index])]
        for b in b_list:
            for r in r_list:
                if b-index == r-b or r-index == b-r:
                    sub +=1
        count = count + len(b_list)*len(r_list)
                
    elif a == "B":
        del r_list[0:len([i for i in r_list if i < index])]
        del g_list[0:len([i for i in g_list if i < index])]
        for r in r_list:
            for g in g_list:
                if r-index == g-r or g-index == r-g:
                    sub +=1
        count = count + len(g_list)*len(r_list) 
        
print(count-sub)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
f=0
ijk = list()
for i in range(N):
    for j in range(N):
        for k in range(N):
            if(k>j>i):
                if(k == j+(j-i)):
                    pass
                elif(i == j-(j-k)):
                    pass
                else:
                    ijk.append([i+1,j+1,k+1])
# print(S)
# print(ijk)

for i in range(len(ijk)):
    if(S[ijk[i][0]-1]== S[ijk[i][1]-1]):pass
    elif(S[ijk[i][0]-1]== S[ijk[i][2]-1]):pass
    elif(S[ijk[i][1]-1]== S[ijk[i][2]-1]):pass
    else:f+=1
print(f)

Submission status: Time Limit Exceeded

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

index_R = [i + 1 for i, x in enumerate(s) if x == "R"]
index_B = [i + 1 for i, x in enumerate(s) if x == "B"]
index_G = [i + 1 for i, x in enumerate(s) if x == "G"]

ans = 0

for i in index_R:
    for j in index_B[bisect.bisect_left(index_B, i):]:
        ans += len(index_G[bisect.bisect_left(index_G, j):])
        if 2 * j - i in index_G:
            ans -= 1
for i in index_R:
    for j in index_G[bisect.bisect_left(index_G, i):]:
        ans += len(index_B[bisect.bisect_left(index_B, j):])
        if 2 * j - i in index_B:
            ans -= 1

for i in index_G:
    for j in index_B[bisect.bisect_left(index_B, i):]:
        ans += len(index_R[bisect.bisect_left(index_R, j):])
        if 2 * j - i in index_R:
            ans -= 1

for i in index_G:
    for j in index_R[bisect.bisect_left(index_R, i):]:
        ans += len(index_B[bisect.bisect_left(index_B, j):])
        if 2 * j - i in index_B:
            ans -= 1

for i in index_B:
    for j in index_R[bisect.bisect_left(index_R, i):]:
        ans += len(index_G[bisect.bisect_left(index_G, j):])
        if 2 * j - i in index_G:
            ans -= 1
for i in index_B:
    for j in index_G[bisect.bisect_left(index_G, i):]:
        ans += len(index_R[bisect.bisect_left(index_R, j):])
        if 2 * j - i in index_R:
            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
        else:
            for k in range(j+1, n):
                if s[i] == s[k] or s[j] == s[k] or j - i == k - j:
                    continue
                else:
                    ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections

N = int(input())
S = input()
result = 0

resdict = collections.defaultdict(dict)

target = set('RGB')
for i in range(N - 2):
    # print(resdict)
    for j in range(i + 1, N):
        if S[i] != S[j]:
            tmp = (target - set(S[i]+S[j])).pop()
            if j in resdict[tmp]:
                # print('used')
                result += resdict[tmp][j]
            else:
                res = S.count(tmp, j + 1, N)
                resdict[tmp][j] = res
                result += res
            if 2*j-i < N and S[2 * j - i] == tmp:
                result -= 1
print(result)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=list(map(ord,list(input())))
R=[0]
G=[0]
B=[0]
for i in s:
  R.append(R[-1])
  G.append(G[-1])
  B.append(B[-1])
  if i==82:R[-1]+=1
  elif i==71:G[-1]+=1
  else: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
    if 82 not in [s[i],s[j]]:
      ans+=R[-1]-R[j]
      if j+(j-i)<n:
        if s[j+(j-i)]==82:ans-=1
    elif 71 not in [s[i],s[j]]:
      ans+=G[-1]-G[j]
      if j+(j-i)<n:
        if s[j+(j-i)]==71:ans-=1
    else:
      ans+=B[-1]-B[j]
      if j+(j-i)<n:
        if s[j+(j-i)]==66:ans-=1
print(ans)

Submission status: Time Limit Exceeded

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

    R_idx = [i for i in range(len(S)) if S[i] == "R"]
    G_idx = [i for i in range(len(S)) if S[i] == "G"]
    B_idx = [i for i in range(len(S)) if S[i] == "B"]
    #print(R_idx)
    #print(G_idx)
    #print(B_idx)
    ans = len(R_idx) * len(G_idx) * len(B_idx)
    for i in R_idx:
        for j in G_idx:
            m = min(i,j)
            M = max(i,j)
            d = M - m
            if m - d in B_idx:
                #print("({},{}), {}".format(i, j, m-d))
                ans -= 1
            if M + d in B_idx:
                #print("({},{}), {}".format(i, j, M+d))
                ans -= 1
            if d %  2 == 0:
                if m + d/2 in B_idx:
                    ans -= 1
        
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
cnt = 0
for k in range(N):
    for j in range(k):
        for i in range(j):
            if S[k] == S[j] or S[j] == S[i] or S[k] == S[i] or k - j == j - i:
                cnt = cnt + 1
            else:
                cnt = cnt + 0

total = int(N * (N-1) * (N-2) / 6)

print(total - cnt)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!

N = int(input())
RGB= list(input())

count = 0
sub = 0

def index_Multi(List,liter):
    #Listはリスト本体・literは検索したい文字
    index_L = []
    for val in range(0,len(List)):
        if liter == List[val]:
            index_L.append(val)
    return index_L
    
r_list = index_Multi(RGB,"R")
g_list = index_Multi(RGB,"G")
b_list = index_Multi(RGB,"B")

for index, a in enumerate(RGB):
    if a == "R":
        del g_list[0:len([i for i in g_list if i < index])]
        del b_list[0:len([i for i in b_list if i < index])]
        for g in g_list:
            for b in b_list:
                if g-index == b-g or b-index == g-b:
                    sub +=1
        count = count + len(g_list)*len(b_list)
                    
    elif a == "G":
        del b_list[0:len([i for i in b_list if i < index])]
        del r_list[0:len([i for i in r_list if i < index])]
        for b in b_list:
            for r in r_list:
                if b-index == r-b or r-index == b-r:
                    sub +=1
        count = count + len(b_list)*len(r_list)
                
    elif a == "B":
        del r_list[0:len([i for i in r_list if i < index])]
        del g_list[0:len([i for i in g_list if i < index])]
        for r in r_list:
            for g in g_list:
                if r-index == g-r or g-index == r-g:
                    sub +=1
        count = count + len(g_list)*len(r_list) 
        
print(count-sub)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
R = []
G = []
B = []
RD = []
GD = []
BD = []

S = input().rstrip()

for i, s in enumerate(S):
    if s == "R":
        R.append(i)
        RD.append(i*2)
    if s == "G":
        G. append(i)
        GD.append(i*2)
    if s == "B":
        B.append(i)
        BD.append(i*2)

ans = len(R) * len(G) * len(B)
for r in R:
    for b in B:
        if r + b in GD:
            ans -= 1
for b in B:
    for g in G:
        if b + g in RD:
            ans -= 1
for r in R:
    for g in G:
        if r + g in BD:
            ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=str(input())
list_R=[]
list_G=[]
list_B=[]
count=0
for i in range(N):
    if S[i]=="R":
        list_R.append(i)
    if S[i]=="G":
        list_G.append(i)
    if S[i]=="B":
        list_B.append(i)
for i in range(len(list_B)):
    for j in range(len(list_G)):
        for k in range(len(list_R)):
            x=[list_B[i],list_G[j],list_R[k]]
            x=sorted(x)
            if int(x[1])-int(x[0])!=int(x[2])-int(x[1]):
                count=count+1                
print(count)

Submission status: Time Limit Exceeded

Solution code:
from itertools import accumulate

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

R = [0] + list(accumulate(int(s == "R") for s in S))
G = [0] + list(accumulate(int(s == "G") for s in S))
B = [0] + list(accumulate(int(s == "B") for s in S))

memo = {"R": R, "G": G, "B": B}

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

        color = "RGB".replace(S[i], "").replace(S[j], "")
        C = memo[color]

        d = j - i
        x = N - j
        if d < x and S[j + d] == color:
            ans += C[N] - C[j] - 1
        else:
            ans += C[N] - C[j]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
r=g=b=0
for i in range(n):
    if s[i]=="R":
        r+=1
    elif s[i]=="G":
        g+=1
    else:
        b+=1
ans=r*g*b
sub=0
for i in range(n-2):
    for j in range(i+1,n-1):
        k=j+(j-i)
        if s[i]==s[j]:
            continue
        if k>=n or s[i]==s[k] or s[j]==s[k]:
            continue
        sub+=1
print(ans-sub)

Submission status: Time Limit Exceeded

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

rgb = [[], [], []]

for i, c in enumerate(s):
    rgb[(c > 'G') + (c > 'B')].append(i)

exclude = 0
for i in rgb[0]:
    for j in rgb[1]:
        ii = min(i, j)
        jj = max(i, j)
        d = jj - ii
        if ii - d >= 0 and ii - d in rgb[2]:
            exclude += 1
        if jj + d < n and jj + d in rgb[2]:
            exclude += 1
        if not d % 2 and ii + d // 2 in rgb[2]:
            exclude += 1

summ = len(rgb[0]) * len(rgb[1]) * len(rgb[2])
print(summ - exclude)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=list(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"])
count=0
#こぴる
for i in r:
    for j in g:
        if j<i:
            continue
        for k in b:
            if k<j:
                continue
            if (j-i)==(k-j):
                 continue   
            count+=1
for i in r:
    for j in b:
        if j<i:
            continue
        for k in g:
            if k<j:
                continue
            if (j-i)==(k-j):
                 continue   
            count+=1
for i in g:
    for j in b:
        if j<i:
            continue
        for k in r:
            if k<j:
                continue
            if (j-i)==(k-j):
                 continue   
            count+=1
for i in g:
    for j in r:
        if j<i:
            continue
        for k in b:
            if k<j:
                continue
            if (j-i)==(k-j):
                 continue   
            count+=1
for i in b:
    for j in r:
        if j<i:
            continue
        for k in g:
            if k<j:
                continue
            if (j-i)==(k-j):
                 continue   
            count+=1
for i in b:
    for j in g:
        if j<i:
            continue
        for k in r:
            if k<j:
                continue
            if (j-i)==(k-j):
                 continue   
            count+=1
print(count)

Submission status: Time Limit Exceeded

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

cand = ["RGB", "RBG", "GRB", "GBR", "BRG", "BGR"]

ans = S.count("R") * S.count("G") * S.count("B")
for i in range(N - 2):
    for d in range(1, N):
        if S[i:i + 2 * d + 1:d] in cand:
            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
sub=0
for i in range(n-2):
    for j in range(i+1,n-1):
        k=j+(j-i)
        if s[i]==s[j]:
            continue
        if k>=n or s[i]==s[k] or s[j]==s[k]:
            continue
        sub+=1
print(ans-sub)

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    S = input()
    com=()
    com=(True for i in range(N) for j in range(i+1,N) for k in range(j+1, N) if( (j-i) != (k-j) and (S[i] != S[j]) and (S[j] != S[k]) and (S[k] != S[i])))
    print(len(list(com)))

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
S = str(input())

num = np.zeros((3,N))
if S[N-1] == 'R':
    num[0][N-1] += 1
elif S[N-1] == 'G':
    num[1][N-1] += 1
else:
    num[2][N-1] += 1
    
for i in range(2,N+1):
    num[0][-i] = num[0][-i+1]
    num[1][-i] = num[1][-i+1]
    num[2][-i] = num[2][-i+1]
    if S[-i] == 'R':
        num[0][-i] += 1
    elif S[-i] == 'G':
        num[1][-i] += 1
    else:
        num[2][-i] += 1

ans = 0
for i in range(N):
    for j in range(i+1,N-1):
        if S[i] != S[j]:
            if S[i] != 'R' and S[j] != 'R':
                ans += num[0][j+1]
                if 2*j - i < N:
                    if S[2*j - i] == 'R':
                        ans -= 1
                        
            if S[i] != 'G' and S[j] != 'G':
                ans += num[1][j+1]
                if 2*j - i < N:
                    if S[2*j - i] == 'G':
                        ans -= 1
                        
            if S[i] != 'B' and S[j] != 'B':
                ans += num[2][j+1]
                if 2*j - i < N:
                    if S[2*j - i] == 'B':
                        ans -= 1
                    
print(int(ans))