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

Solution code:
import sys
from collections import Counter
def input(): return sys.stdin.readline().rstrip()
def main():
    n=int(input())
    s=input()
    c_s=Counter(s)
    sum_a=0
    for diff in range(1,(n-1)//2+1):
        for i in range(n-2*diff):
            if s[i]!=s[i+diff] and s[i+diff]!=s[i+2*diff] and s[i]!=s[i+2*diff]:
                sum_a+=1
    print(c_s['R']*c_s['G']*c_s['B']-sum_a)

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:

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

remain = [[0,0,0]]
for i in range(n):
    r, g, b = remain[-1]
    if s[-i-1] == "R":
        r = r+1
    elif s[-i-1] == "G":
        g = g + 1
    elif s[-i-1] == "B":
        b = b + 1

    remain.append([r,g,b])

total = 0

rgb="RGB"
for i in range(n-2):
    first = s[i]
    search = []
    for j in range(3):
        if j==rgb.find(first):
            continue
        else:
            search.append(j)
    total = total + (remain[-i-2][search[0]]*remain[-i-2][search[1]])
    try:
        for j in range(1,n):
            if s[i] != s[i+j] and s[i] != s[i+j+j] and s[i+j] != s[i+j+j]:
                total -= 1
    except:
        pass


print(total)

Submission status: Accepted

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

# keyは(色、jの位置)のタプル
dp = {}

ans = 0

for i in range(n - 2):
    c_i = S[i]
    for j in range(i + 1, n - 1):
        if S[j] == c_i:
            continue
        c_j = S[j]
        d_ij = j - i
        if (c_i, j) in dp:
            ans += dp[(c_i, j)]
            if j + d_ij < n:
                c_k = S[j + d_ij]
                if c_i != c_j and c_j != c_k and c_k != c_i:
                    ans -= 1
        else:
            tmp = 0
            for k in range(j + 1, n):
                c_k = S[k]
                if c_i != c_j and c_j != c_k and c_k != c_i:
                    tmp += 1
            dp[(c_i, j)] = tmp
            if j + d_ij < n:
                c_k = S[j + d_ij]
                if c_i != c_j and c_j != c_k and c_k != c_i:
                    tmp -= 1
            ans += tmp
print(ans)

Submission status: Accepted

Solution code:
import sys
def getch(i,j):
  rgb = {'R':0,'G':1,'B':2}
  for k in rgb:
    if i == k or j == k:
      continue
    return k,rgb[k]
N = int(input())
S = list(input())
l = len(S)
rgb_num = [[0,0,0] for i in range(l)]
for i in range(l):
  for j in range(i+1,l):
    if S[j] == 'R':
      rgb_num[i][0] += 1
    elif S[j] == 'G':
      rgb_num[i][1] += 1
    else:
      rgb_num[i][2] += 1

su = 0

for i in range(l):
  si = S[i]
  for j in range(i+1,l):
    sj = S[j]
    if si != sj:
      k = 2*(j+1) - (i+1)-1
      sk,n = getch(si,sj)
      if k < l and S[k] == sk:
        su += rgb_num[j][n]-1
      else:
        su += rgb_num[j][n]
print(su)

Submission status: Accepted

Solution code:
n=int(input())
s=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)
  elif s[i]=="B":
    b.append(i+1)
count=0
j=0
rcount=[]
gcount=[]
sb=set(b)
for i in r:
  for j in g:
    if j>i and j+(j-i) in sb:
      count+=1
    elif i>j and i+(i-j) in sb:
      count+=1
sr=set(r)
for i in g:
  for j in b:
    if j>i and j+(j-i) in sr:
      count+=1
    elif i>j and i+(i-j) in sr:
      count+=1
sg=set(g)
for i in r:
  for j in b:
    if j>i and j+(j-i) in sg:
      count+=1
    elif i>j and i+(i-j) in sg:
      count+=1

print(len(r)*len(g)*len(b)-count)

Submission status: Accepted

Solution code:

def combi_2(n):
    if n < 2:
        return 0
    return n * (n-1) // (2 * 1)

def combi_3(n):
    if n < 3:
        return 0
    return n * (n-1) * (n-2) // (3 * 2 * 1)


def main():

    n = int(input())
    s = input()
    #条件1
    numR = s.count("R")
    numG = s.count("G")
    numB = s.count("B")
    total = combi_3(len(s))

    red_pair = combi_2(numR) * numB + combi_2(numR) * numG
    gre_pair = combi_2(numG) * numR + combi_2(numG) * numB
    blu_pair = combi_2(numB) * numR + combi_2(numB) * numG
    three_red = combi_3(numR)
    three_gre = combi_3(numG)
    three_blu = combi_3(numB)


    first_error = red_pair + gre_pair + blu_pair + three_red + three_gre + three_blu


    #条件２をカウント　ただし、条件１に当てはまるものは除外
    second_error = 0
    for i in range(1,n+1-2):
        for j in range(i+1,n+1-1):
            k = j + (j-i)
            if k > n:
                continue
            #print(i-1,j-1,k-1)
            if s[i-1] == s[j-1] or s[i-1] == s[k-1] or s[j-1] == s[k-1]:
                continue
            second_error += 1
            #print(i,j,k)
    #print("total:",total)
    #print("first_error:",first_error)
    #print("second_error:",second_error)

    print(total - first_error - second_error)











if __name__ == '__main__':
    pass
    main()

Submission status: Accepted

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

R = [None] * S.count("R")
G = [None] * S.count("G")
B = [None] * S.count("B")

ri = 0
gi = 0
bi = 0
for i in range(N):
    if S[i] == "R":
        R[ri] = i+1
        ri+=1
    elif S[i] == "G":
        G[gi] = i+1
        gi += 1
    else:
        B[bi] = i+1
        bi += 1
R = set(R)
G = set(G)
B = set(B)

c = 0
for r in R:
    for g in G:
        for d in [(r + g) / 2, 2 * r - g, 2 * g - r]:
            if d in B:
                c += 1

ans = len(R) * len(G) * len(B) - c
print(ans)

Submission status: Accepted

Solution code:
ans=0
n=int(input())
k=str(input())
r=k.count("R")
b=k.count("B")
g=k.count("G")
ans+=r*b*g
    
for i in range(n-2):
  ii=k[i]
  for j in range(i+1,(n-i)//2+1+i):
    jj=k[j]
    l=j+(j-i)
    #print(l)
    if jj!=ii and l<n:
      #print(i,j)
      ll=k[l]
      if ii!=ll and jj!=ll:
        ans-=1
          #print(i,j,l)
print(ans)

Submission status: Accepted

Solution code:
import sys, math
def input(): return sys.stdin.readline().strip()


def func(R, G, B, lR, lG, lB):
    pG = lG - 1
    pB = lB - 1
    sumR = 0
    sumG = 0

    for i in range(lR - 1, -1, -1):
        while pG >= 0 and R[i] < G[pG]:
            while pB >= 0 and G[pG] < B[pB]:
                pB -= 1
            sumG += (lB - pB - 1)
            pG -= 1
        sumR += sumG
    return sumR


def main():
    N = int(input())
    S = input()
    
    R = []
    G = []
    B = []
    lR = 0
    lG = 0
    lB = 0
    for i in range(N):
        if S[i] == "R":
            R.append(i)
            lR += 1
        elif S[i] == "G":
            G.append(i)
            lG += 1
        else:
            B.append(i)
            lB += 1
    
    ans = 0
    ans += func(R, G, B, lR, lG, lB)
    #print("RGB->{}".format(func(R, G, B, lR, lG, lB)))
    ans += func(R, B ,G, lR, lB, lG)
    #print("RBG->{}".format(func(R, B ,G, lR, lB, lG)))
    ans += func(G, R, B, lG, lR, lB)
    #print("GRB->{}".format(func(G, R, B, lG, lR, lB)))
    ans += func(G, B, R, lG, lB, lR)
    #print("GBR->{}".format(func(G, B, R, lG, lB, lR)))
    ans += func(B, R, G, lB, lR, lG)
    #print("BRG->{}".format(func(B, R, G, lB, lR, lG)))
    ans += func(B, G, R, lB, lG, lR)
    #print("BGR->{}".format(func(B, G, R, lB, lG, lR)))

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

    print(ans - subtract)
    


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value

N = ii()
S = input()
h = [0]*N
h = 'RGB'*1000

#h = h.replace('R', '1').replace('G', '2').replace('B', '7')
'''
cnt = -1
ans = 0
for i in range(N):
    cnt = int(h[i])
    for j in range(i+1, N):
        num = cnt + int(h[j])
        if num == 3 or num == 9 or num==8:
            for k in range(j+1, N):
                if num + int(h[k]) == 10 and k!=2*j-i:
                    ans += 1
'''

#h = h.replace('R', '1').replace('G', '2').replace('B', '7')

c_r = c_g = c_b = 0
h = ''
for i, a in enumerate(S):
    if a == 'R':
        c_r += 1
        h += '1'
    if a == 'G':
        c_g += 1
        h += '2'
    if a == 'B':
        c_b += 1
        h += '7'

ans = c_r * c_g * c_b

for i in range(N):
    for j in range(i+1, N):
        if 2*j-i<N and int(h[i]) + int(h[j]) + int(h[2*j-i]) == 10:
            ans -= 1
print(ans)

Submission status: Accepted

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

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

ok = len(R) * len(G) * len(B)
for i in range(1,N):
  for j in range(N//2+1):
    if i - j < 0 or i + j >= N:
      break
    else:
      if S[i-j] != S[i] and S[i] != S[i+j] and S[i-j] != S[i+j]:
        ok -= 1

print(ok)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
s = list(input())
c = Counter(s)
total = c['R'] * c['G'] * c['B']
count = 0
for i in range(n):
	for j in range(i + 1, n):
			k = 2 * j - i
			if k < n and s[i] != s[j] and s[j] != s[k] and s[i] != s[k]:
				count += 1
print(total - count)

Submission status: Accepted

Solution code:
## coding: UTF-8
from decimal import *
from itertools import permutations, combinations,combinations_with_replacement,product
import math
from math import gcd

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

r_list = []
g_list = []
b_list = []

for i in range(N):
    if(S[i] == 'R'):
        r_list.append(i)
    elif(S[i] == 'G'):
        g_list.append(i)
    else:
        b_list.append(i)

#print('r:{}, g:{}, b:{}'.format(r_list, g_list, b_list))

R_count = len(r_list)
G_count = len(g_list)
B_count = len(b_list)
#print(R_count * G_count * B_count)

b_num_list = [0 for i in range(N)]
for i in range(B_count):
    b_num_list[b_list[i]] += 1
#print('b_num_list:{}'.format(b_num_list))
    

counter = R_count * G_count * B_count
NG_list = []
for i in range(R_count):
    for j in range(G_count):
        r = r_list[i]
        g = g_list[j]
        hit_count = 0
        #if( (g+g-r) in b_list):
        #print('g+g+r:{}'.format(g+g-r))
        if(g+g-r >= 0 and g+g-r <= N-1): #and b_num_list[g+g-r] == 1)
            #print('b_num_list[g+g-r]:{}'.format(b_num_list[g+g-r]))
            if(b_num_list[g+g-r] == 1):
                hit_count += 1
        #if( (g+r)%2 == 0 and ((g+r)/2) in b_list ):
        if( (g+r)%2 == 0 and b_num_list[int((g+r)/2)] == 1):
            hit_count+=1
        #if( (r-g+r) in b_list):
        if(r+r-g >= 0 and r+r-g <= N-1 ):#and b_num_list[r+r-g] == 1):
            if(b_num_list[r+r-g] == 1):
                hit_count+=1
        counter -= hit_count
print(counter)

Submission status: Accepted

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

r_cnt = s.count('R')
g_cnt = s.count('G')
b_cnt = s.count('B')

total = r_cnt*g_cnt*b_cnt

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

print(total)

Submission status: Accepted

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

sub = 0
for j in range(1, (N - 1) // 2 + 1):
    for i in range(0, N - 2 * j):
        if S[i] != S[i + j] and S[i + j] != S[i + 2 * j] and S[i + 2 * j] != S[i]:
            sub += 1

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

ans = r * g * b - sub
print(ans)

Submission status: Accepted

Solution code:
import sys,math
def input():
    return sys.stdin.readline()[:-1]
N = int(input())
S = list(input())

ANS = 0
dp = [None]*N
r,g,b = 0,0,0
for i in range(N):
    elm = S[i]
    if elm=='R':
        r += 1
    elif elm=='G':
        g += 1
    else:
        b += 1
    dp[i] = [r,g,b]

TTL = dp[-1]
for i in range(N):
    f = S[i]
    for j in ['R','G','B']:
        if j==f:
            continue

        for k in range(i+1,N):
            elm = S[k]
            if elm!=j:
                continue
            for l in ['R','G','B']:
                if l==f or l==elm:
                    continue

                if l=='R':
                    d = 0
                elif l=='G':
                    d = 1
                else:
                    d = 2
                ANS += TTL[d] - dp[k][d]

                if k+k-i<N and S[k+k-i]==l:
                    ANS -= 1
print(ANS)

Submission status: Accepted

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

from collections import defaultdict 


counter = [defaultdict(int) for _ in range(N)]
for n in range(N):
    for i in range(n, N):
        counter[n][S[i]] += 1


ans = 0
for x in range(N):
    for y in range(x + 1, N):
        if S[x] != S[y]:
            length = y - x
            if "R" not in (S[x], S[y]):
                color = "R"
            elif "G" not in (S[x], S[y]):
                color = "G"
            else:
                color = "B"

            if (y + 1) < N:
                ans += counter[y + 1][color]

            if (y + length) < N and S[y + length] == color:
               ans -= 1

print(ans)

Submission status: Accepted

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

import random
from collections import Counter

res = 1
val = Counter(s).values()
if len(val) < 3:
    print(0)
    exit()

for i in val:
    res *= i

count = 0
for i in range(n):
    j = 1
    while i + 2 * j < n:
        if (s[i] != s[i+j]) and (s[i+j] != s[i+2*j]) and (s[i] != s[i+2*j]):
            count += 1
        j += 1

print(res - count)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
cnt = 0
for i in range(1,n-1):
    if s[i] == 'R':
        cnt += (s[:i].count('G'))*(s[i+1:].count('B')) + (s[:i].count('B'))*(s[i+1:].count('G'))
    if s[i] == 'G':
        cnt += (s[:i].count('R'))*(s[i+1:].count('B')) + (s[:i].count('B'))*(s[i+1:].count('R'))
    if s[i] == 'B':
        cnt += (s[:i].count('G'))*(s[i+1:].count('R')) + (s[:i].count('R'))*(s[i+1:].count('G'))
for i in range(1,n-1):
    if s[i] == 'R':
        for j in range(1,min(i,n-i-1)+1):
            if s[i-j] == 'G' and s[i+j]=='B':
                cnt -=1
            if s[i-j] == 'B' and s[i+j]=='G':
                cnt -=1
    if s[i] == 'G':
        for j in range(1,min(i,n-i-1)+1):
            if s[i-j] == 'R' and s[i+j]=='B':
                cnt -=1
            if s[i-j] == 'B' and s[i+j]=='R':
                cnt -=1
    if s[i] == 'B':
        for j in range(1,min(i,n-i-1)+1):
            if s[i-j] == 'G' and s[i+j]=='R':
                cnt -=1
            if s[i-j] == 'R' and s[i+j]=='G':
                cnt -=1
print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
l=[[] for _ in range(3)]
for i in range(n):
    if s[i]=='R':
        l[0].append(i)
    elif s[i]=='G':
        l[1].append(i)
    else:
        l[2].append(i)
ans=len(l[0])*len(l[1])*len(l[2])
for i in range(n-2):
    for j in range(i+1,n-1):
        if 2*j-i<n:
            if s[i]!=s[j] and s[j]!=s[2*j-i] and s[i]!=s[2*j-i]:
                ans-=1
print(ans)

Submission status: Accepted

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


def solve(N: int, S: str):
    rest_r = []
    rest_g = []
    rest_b = []

    c_r = 0
    c_g = 0
    c_b = 0

    i_r = []
    i_g = []
    i_b = []

    for i in range(N-1, -1, -1):
        if S[i] == 'R':
            c_r+=1
            i_r.append(i)
        if S[i] == 'G':
            c_g+=1
            i_g.append(i)
        if S[i] == 'B':
            c_b+=1
            i_b.append(i)
        rest_r.append(c_r)
        rest_g.append(c_g)
        rest_b.append(c_b)

    rest_r.reverse()
    rest_g.reverse()
    rest_b.reverse()

    i_r.reverse()
    i_g.reverse()
    i_b.reverse()

    sum = 0

    # r, g, b
    for r in i_r:
        for g in i_g:
            i_max = max(r, g)
            sum += rest_b[i_max]
            i = i_max + abs(g - r)
            if i < N and S[i] == 'B':
                sum -= 1
    for r in i_r:
        for g in i_b:
            i_max = max(r, g)
            sum += rest_g[i_max]
            i = i_max + abs(g - r)
            if i < N and S[i] == 'G':
                sum -= 1

    for r in i_g:
        for g in i_b:
            i_max = max(r, g)
            sum += rest_r[i_max]
            i = i_max + abs(g - r)
            if i < N and S[i] == 'R':
                sum -= 1

    print(sum)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    S = next(tokens)  # type: str
    solve(N, S)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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


i=0
while i<N-1:
    j=i+1
    while 2*j-i<N:
        ans-=1*(S[i]!=S[j])*(S[j]!=S[2*j-i])*(S[2*j-i]!=S[i])
        j+=1
    i+=1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
s_reverse = s[::-1]
ans = 0
r_cnt = 0
b_cnt = 0
g_cnt = 0
r = [0] * n
b = [0] * n
g = [0] * n
for i in range(n):
    if s_reverse[i] == 'R':
        r_cnt += 1
    elif s_reverse[i] == 'G':
        g_cnt += 1
    else:
        b_cnt += 1
    r[n-i-1] = r_cnt
    g[n-i-1] = g_cnt
    b[n-i-1] = b_cnt
    
for i in range(n-2):
    for j in range(i+1, n-1):
        if s[i] == 'R' and s[j] == 'G':
            if (2*j - i) < n:
                if s[j+(j-i)] == 'B':
                    ans -= 1
            ans += b[j+1]
        elif s[i] == 'R' and s[j] == 'B':
            if (2 * j - i) < n:
                if s[j+(j-i)] == 'G':
                    ans -= 1
            ans += g[j+1]
        elif s[i] == 'G' and s[j] == 'R':
            if (2 * j - i) < n:
                if s[j+(j-i)] == 'B':
                    ans -= 1
            ans += b[j+1]
        elif s[i] == 'G' and s[j] == 'B':
            if (2 * j - i) < n:
                if s[j+(j-i)] == 'R':
                    ans -= 1
            ans += r[j+1]
        elif s[i] == 'B' and s[j] == 'R':
            if (2 * j - i) < n:
                if s[j+(j-i)] == 'G':
                    ans -= 1
            ans += g[j+1]
        elif s[i] == 'B' and s[j] == 'G':
            if (2 * j - i) < n:
                if s[j+(j-i)] == 'R':
                    ans -= 1
            ans += r[j+1]

print(ans)

Submission status: Accepted

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

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

for i in range(n):
  if s[i] == 'R':
    R.append(i+1)
  elif s[i] == 'G':
    G.append(i+1)
  else:
    B.append(i+1)
    
    
R_set = set(R)
G_set = set(G)
B_set = set(B)
ans = func(R,G,B,B_set)+func(R,B,G,G_set)+func(G,R,B,B_set)+func(G,B,R,R_set) + func(B,R,G,G_set)+func(B,G,R,R_set)
print(ans)

Submission status: Accepted

Solution code:
import sys, math

n = int(sys.stdin.readline().strip())
s = sys.stdin.readline().strip()
res = 0

r = []
g = []
b = []
for i, c in enumerate(s):
    if c == "R":
        r.append(i)
    if c == "G":
        g.append(i)
    if c == "B":
        b.append(i)


res = len(r) * len(b) * len(g)

def bin_find(tar):
    left = 0
    right = len(b)
    while left < right:
        mid = (left+right) // 2
        if b[mid] < tar:
            left = mid+1
        elif b[mid] > tar:
            right = mid
        else:
            return mid
    return -1

for i in r:
    for j in g:
        diff = i - j
        if diff < 0:
            diff = -diff
        if diff % 2 == 0:
            target = diff // 2 + min(i,j)
            if target < n and s[target] == 'B':
                res -= 1
        target = min(i,j) - diff
        if target >= 0 and s[target] == 'B':
            res -= 1
        target = max(i,j) + diff
        if target < n and s[target] == "B":
            res -= 1

print(res)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
#a,b = map(int,input().split())
#a_L = list(map(int,input().split()))

R,G,B = [],[],[]


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


base = len(R) * len(G) * len(B)

B = set(B)

ans = 0
for r in R:
    for g in G:
        y = max(r,g)
        x = min(r,g)
        
        # x < 0 < y

        tmp = int((x+y)/2)
        if (x+y)%2==0 and (tmp in B):
            ans += 1
        # x<y<0
        tmp = 2*y-x
        if tmp in B:
            ans += 1
        #0 < x < y
        tmp = 2*x-y
        if tmp in B:
            ans += 1

print(base-ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

rgb = defaultdict(list)
blue = defaultdict(int)

for i in range(n):
    if s[i] == 'R':
        rgb[0].append(i)
    elif s[i] == 'G':
        rgb[1].append(i)
    else:
        blue[i] = i

ans = len(rgb[0]) * len(rgb[1]) * len(blue)

for r in rgb[0]:
    for g in rgb[1]:
        tmp = sorted([r, g])
        if tmp[0] - (tmp[1]-tmp[0]) in blue:
            ans -= 1
        if tmp[1] + (tmp[1]-tmp[0]) in blue:
            ans -= 1
        if (tmp[1]+tmp[0]) % 2 == 0 and (tmp[1]+tmp[0])//2 in blue:
            ans -= 1

print(ans)

Submission status: Accepted

Solution code:
from functools import reduce
from fractions import gcd
import math
import bisect
import itertools
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
INF = float("inf")


def main():
    N = int(input())
    S = input()[:-1]

    A = [[] for _ in range(3)]
    for i in range(N):
        if S[i:i+1] == "R":
            A[0].append(i)
        if S[i:i+1] == "G":
            A[1].append(i)
        if S[i:i+1] == "B":
            A[2].append(i)

    res = 0
    for P in itertools.permutations(range(3)):
        a = P[0]
        b = P[1]
        c = P[2]
        for i in A[a]:
            x = bisect.bisect_left(A[b], i)
            for j in A[b][x:]:
                res += len(A[c]) - bisect.bisect_left(A[c], j)
    
    for l in range(1, math.ceil(N/2)):
        n = 0
        while n+l*2 < N:
            if S[n] != S[n+l] and S[n+l] != S[n+l*2] and S[n+l*2] != S[n]:
                res -= 1
            n += 1
    
    print(res)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

rs = set()
gs = set()
bs = set()
for i, s in enumerate(S):
    if s == 'R':
        rs.add(i+1)
    elif s == 'G':
        gs.add(i+1)
    elif s == 'B':
        bs.add(i+1)

count = 0
for i in rs:
    for j in gs:
        a = min(i, j)
        b = max(i, j)
        d = b - a
        e = 0
        if (a - d) in bs:
            e += 1
        if (b + d) in bs:
            e += 1
        if d % 2 == 0 and (b - (d // 2)) in bs:
            e += 1
        count += (len(bs) - e)
print(count)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 12 20:59:07 2020

@author: naoki
"""

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

Dp = [[0 for _ in range(3)] for _ in range(N)]

if S[0] == "R":
    Dp[0][0] = 1
elif S[0] == "G":
    Dp[0][1] = 1
elif S[0] == "B":
    Dp[0][2] = 1

allcount = 0
for j in range(1,N):
    Dp[j][0] = Dp[j-1][0]
    Dp[j][1] = Dp[j-1][1]
    Dp[j][2] = Dp[j-1][2]
    
    if S[j] == "R":
        Dp[j][0] += 1
        allcount += Dp[j-1][1]*Dp[j-1][2]
    elif S[j] == "G":
        Dp[j][1] += 1
        allcount += Dp[j-1][0]*Dp[j-1][2]
    elif S[j] == "B":
        Dp[j][2] += 1
        allcount += Dp[j-1][0]*Dp[j-1][1]
    
for j in range(1,N):
    for i in range(1,j):
        if j -i -i >= 0:
            if S[j-i] != S[j-2*i] and S[j-i] !=S[j] and S[j-2*i] !=S[j]:
                allcount -= 1
                pass
print(allcount)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    s = input()
    r, g, b = [], [], []
    for i in range(n):
        t = s[i]
        if t == 'R':
            r.append(i)
        elif t == 'G':
            g.append(i)
        else:
            b.append(i)
    ans = len(r) * len(g) * len(b)

    for i in range(1, n // 2 + 1):
        for k in range(n):
            try:
                if {s[k], s[k + i], s[k + 2 * i]} == {'R', 'G', 'B'}:
                    ans -= 1
            except IndexError:
                break
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

import itertools
# import sys


def count_triplets(n, s):
    rs = [i for i in range(n) if s[i] == "R"]
    gs = [i for i in range(n) if s[i] == "G"]
    bs = [i for i in range(n) if s[i] == "B"]
    rgbs = [rs, gs, bs]
    gt_rs = [sum(r >= i for r in rs) for i in range(n + 2)]
    gt_gs = [sum(r >= i for r in gs) for i in range(n + 2)]
    gt_bs = [sum(r >= i for r in bs) for i in range(n + 2)]
    gts = [gt_rs, gt_gs, gt_bs]
    m = 3
    res = 0
    for col1, col2 in itertools.combinations(range(m), r=2):
        col3 = m - col1 - col2
        for r, g in itertools.product(rgbs[col1], rgbs[col2]):
            if r >= g:
                r, g = g, r
            res += gts[col3][g + 1]
            if 0 <= 2 * g - r < n:
                res -= gts[col3][2 * g - r]
                res += gts[col3][2 * g - r + 1]
    return res


def main():
    n = int(input())
    s = input()
    res = count_triplets(n, s)
    print(res)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():
    colors = ['R', 'G', 'B']
    N = int(input())
    S = input()
    
    ans = 0
    cnts = {'R': [], 'G': [], 'B': []}
    if S[-1] == 'R':
        cnts = {'R': [1], 'G': [0], 'B': [0]}
    elif S[-1] == 'G':
        cnts = {'R': [0], 'G': [1], 'B': [0]}
    else:
        cnts = {'R': [0], 'G': [0], 'B': [1]}

    for i, s in enumerate(S[::-1][1:]):
        for color in colors:
            if s == color:
                cnts[color].append(cnts[color][i] + 1)
            else:
                cnts[color].append(cnts[color][i])

    for color in colors:
        cnts[color] = cnts[color][::-1]

    for i in range(N-2):
        for j in range(i+1, N-1):
            if S[i] == S[j]:
                continue
            oth = colors[:]
            oth.remove(S[i])
            oth.remove(S[j])
            oth = oth[0]
            cnt = cnts[oth][j+1]
            ans += cnt
            if j + (j - i) < N:
                if S[j + (j - i)] == oth:
                    ans -= 1
            
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

for i in range(L-2):
    for j in range(i+1,L-1):
        if s[j]!=s[i]:
            if j+j-i<=L-1:
                if s[j+j-i]!=s[i] and s[j+j-i]!=s[j]:
                    ans+=1
print(r*b*g-ans)

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
r = S.count('R')
g = S.count('G')
b = S.count('B')
cnt=0
for i in range(N-2):
    for j in range(i+1,N-1):
        if(j+(j-i) >= N):
            break
        if((S[j+(j-i)] != S[i]) and (S[j+(j-i)] != S[j]) and (S[i] != S[j])):
            cnt += 1
print(r*g*b-cnt)

Submission status: Accepted

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

R, G, B = set(), set(), set()
r, g, b = 0, 0, 0

for i, s in enumerate(S):
    if s == "R":
        R.add(i + 1)
        r += 1

    elif s == "G":
        G.add(i + 1)
        g += 1

    else:
        B.add(i + 1)
        b += 1

ans = r * g * b
punish = 0


for rr in R:
    for gg in G:
        if (min(rr, gg) - abs(rr - gg)) in B:
            punish += 1

        if (max(rr, gg) + abs(rr - gg)) in B:
            punish += 1
      
        if (abs(rr - gg) % 2 == 0) and (abs(rr - gg)//2 + min(rr, gg) in B):
            punish += 1
      

print(ans - punish)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
def main():
    N=int(input())
    a_list=input()

    r=0
    g=0
    b=0
    s=0
    for i,a in enumerate(a_list):
        if a=="R":
            r+=1
            s+=g*b
        elif a=="G":
            g+=1
            s+=r*b
        elif a=="B":
            b+=1
            s+=r*g
    
    for i in range(1,N):
        for j in range(N):
            if j+2*i>=N:
                break

            s1=a_list[j]
            s2=a_list[j+i]
            s3=a_list[j+i*2]
            if s1!=s2:
                if s2!=s3:
                    if s1!=s3:
                        s-=1
    
    print(s)




if __name__ =='__main__': 
    main()

Submission status: Accepted

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

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)

count = len(R)*len(G)*len(B)
for i in range(1,N-1):
    f = s[i-1::-1]
    b = s[i+1:]
    for j in range(min(len(f),len(b))):
        if f[j]!=s[i] and f[j] != b[j] and s[i] != b[j]:
            count -=1
print(count)

Submission status: Accepted

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

r, g, b = [0, 0, 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

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
l = list(input())
r = 0
g = 0
b = 0
for i in range(n):
  r += 1 if l[i] == "R" else 0
  g += 1 if l[i] == "G" else 0
  b += 1 if l[i] == "B" else 0
       
ans = 0
for j in range(n):
  for i in range(j):
    tmp = j-i+j
    if tmp >= n:
      continue
    if l[i] != l[j] and(l[i] != l[tmp]) and(l[j] != l[tmp]):
      ans -= 1
      
print(r * b * g + ans)