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
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import numpy as np
from numba import njit
 
MOD = 10**9 + 7
 
N = int(readline())
S = np.array(list(read().rstrip()), np.int8)
 
R = np.sum(S == ord('R'))
B = np.sum(S == ord('B'))
G = np.sum(S == ord('G'))
 
@njit
def f(S):
    N = len(S)
    ret = 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]:
                ret += 1
    return ret
 
answer = R * B * G - f(S)
print(answer)

Submission status: Accepted

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

	listS = list(S)

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

	ans = r*g*b

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

	print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
Sr = Sg = Sb = 0
for c in S:
    if c == 'R':
        Sr += 1
    elif c == 'G':
        Sg += 1
    else:
        Sb += 1
A = Sr * Sg * Sb
for sep in range(1,(N+1)//2):
    for i in range(N-2*sep):
        if S[i] != S[i+sep] and S[i+sep] != S[i+2*sep] and S[i] != S[i+2*sep]:
            A -= 1
print(A)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math, string, itertools, fractions, heapq, collections, re, array, bisect, copy, functools, random
import sys
from collections import deque, defaultdict, Counter; from heapq import heappush, heappop
from itertools import permutations, combinations, product, accumulate, groupby
from bisect import bisect_left, bisect_right, insort_left, insort_right
from operator import itemgetter as ig
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20; INF = float("INF"); ans = 0; tmp = 0; cnt = 0; ansli = []; tmpli = []; candili = []; stillset = set()
eps = 1.0 / 10 ** 10; mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]; ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]; ddn9 = ddn + [(0, 0)]
"""for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:"""
def wi(): return list(map(int, sys.stdin.readline().split()))
def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]#WideIntPoint
def ws(): return sys.stdin.readline().split()
def i(): return int(sys.stdin.readline())
def s(): return input()
def s_list(): return list(input())
def hi(n): return [i() for _ in range(n)]
def hs(n): return [s() for _ in range(n)]#HeightString
def mi(n): return [wi() for _ in range(n)]#MatrixInt
def num_grid(n): return [[int(i) for i in sys.stdin.readline().split()[0]] for _ in range(n)]#NumberGrid
def mip(n): return [wip() for _ in range(n)]
def ms(n): return [ws() for _ in range(n)]
def grid(n): return [s_list() for _ in range(n)]


r_list= []
g_list = []
b_list = []
def length(t):
    if t == "R":
        return len(g_list), len(b_list)
    elif t == "G":
        return len(b_list), len(r_list)
    else:
        return len(r_list), len(g_list)

def rgb_list(t):
    if t == "R":
        return g_list, b_list
    elif t == "G":
        return b_list, r_list
    else:
        return r_list, g_list
n = i()
s = s()

for i, t in enumerate(s):
    if t == "R":
        r_list.append(i)
    elif t == "G":
        g_list.append(i)
    else:
        b_list.append(i)

rgb = ["R", "G", "B"]
s_start = 0
t_start = 0
r_start = 0
g_start = 0
b_start = 0

for i, first in enumerate(s):
    a1 = 0
    a2 = 0
    if first == "R":
        for g_i in range(g_start, len(g_list)):
            if i < g_list[g_i]:
                g_start = g_i
                a1 = 1
                break
        for b_i in range(b_start, len(b_list)):
            if i < b_list[b_i]:
                b_start = b_i
                a2 = 1
                break
        if a1 == 1 and a2 == 1:
            ans += (len(g_list) - g_start) * (len(b_list) - b_start)

    elif first == "G":
        for r_i in range(r_start, len(r_list)):
            if i < r_list[r_i]:
                r_start = r_i
                a1 = 1
                break
        for b_i in range(b_start, len(b_list)):
            if i < b_list[b_i]:
                b_start = b_i
                a2 = 1
                break
        if a1 == 1 and a2 == 1:
            ans += (len(r_list) - r_start) * (len(b_list) - b_start)

    else:
        for g_i in range(g_start, len(g_list)):
            if i < g_list[g_i]:
                g_start = g_i
                a1 = 1
                break
        for r_i in range(r_start, len(r_list)):
            if i < r_list[r_i]:
                r_start = r_i
                a2 = 1
                break
        if a1 == 1 and a2 == 1:
            ans += (len(g_list) - g_start) * (len(r_list) - r_start)

for i, first in enumerate(s):
    interval = 1
    while i + 2 * interval < len(s):
        if first != s[i + interval] and s[i + interval] != s[i + 2 * interval] and s[i + 2 * interval] != first:
            ans -= 1
        interval += 1
print(ans)

Submission status: Accepted

Solution code:
import math, string, itertools, fractions, heapq, collections, re, array, bisect, copy, functools, random
import sys
from collections import deque, defaultdict, Counter; from heapq import heappush, heappop
from itertools import permutations, combinations, product, accumulate, groupby
from bisect import bisect_left, bisect_right, insort_left, insort_right
from operator import itemgetter as ig
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20; INF = float("INF"); ans = 0; tmp = 0; cnt = 0; ansli = []; tmpli = []; candili = []; stillset = set()
eps = 1.0 / 10 ** 10; mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]; ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]; ddn9 = ddn + [(0, 0)]
"""for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:"""
def wi(): return list(map(int, sys.stdin.readline().split()))
def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]#WideIntPoint
def ws(): return sys.stdin.readline().split()
def i(): return int(sys.stdin.readline())
def s(): return input()
def s_list(): return list(input())
def hi(n): return [i() for _ in range(n)]
def hs(n): return [s() for _ in range(n)]#HeightString
def mi(n): return [wi() for _ in range(n)]#MatrixInt
def num_grid(n): return [[int(i) for i in sys.stdin.readline().split()[0]] for _ in range(n)]#NumberGrid
def mip(n): return [wip() for _ in range(n)]
def ms(n): return [ws() for _ in range(n)]
def grid(n): return [s_list() for _ in range(n)]


r_list= []
g_list = []
b_list = []
def length(t):
    if t == "R":
        return len(g_list), len(b_list)
    elif t == "G":
        return len(b_list), len(r_list)
    else:
        return len(r_list), len(g_list)

def rgb_list(t):
    if t == "R":
        return g_list, b_list
    elif t == "G":
        return b_list, r_list
    else:
        return r_list, g_list
n = i()
s = s()

for i, t in enumerate(s):
    if t == "R":
        r_list.append(i)
    elif t == "G":
        g_list.append(i)
    else:
        b_list.append(i)

rgb = ["R", "G", "B"]
s_start = 0
t_start = 0

a = [i for i in range(n)]

def isOK(mid, point):
    if mid >= point:
        return True
    else:
        return False

#汎用的な二分探索のテンプレ
#条件を満たす最小値（ng,ok）
def binary_search(the_list, point):  # pointより大きい最小値
    ng = -1 #「index = 0」が条件を満たすこともあるので、初期値は -1
    ok = len(the_list) #「index = a.size()-1」が条件を満たさないこともあるので、初期値は a.size()

    #ok と ng のどちらが大きいかわからないことを考慮
    while abs(ok - ng) > 1:
        mid = the_list[(ok + ng) // 2]
        if isOK(mid, point):
            ok = (ok + ng) // 2
        else:
            ng = (ok + ng) // 2
    if ok < len(the_list):
        return the_list[ok], ok
    else:
        return "", ""

def go(a):
    if a == "":
        return False
    return True

for i, first in enumerate(s):
    s_len, t_len = length(first)
    s_list, t_list = rgb_list(first)

    s_i, s_num = binary_search(s_list, i)
    if go(s_i):
        t_i, t_num = binary_search(t_list, i)
        if go(t_num):
            ans += (len(s_list) - s_num) * (len(t_list) - t_num)

for i, first in enumerate(s):
    interval = 1
    while i + 2 * interval < len(s):
        if first != s[i + interval] and s[i + interval] != s[i + 2 * interval] and s[i + 2 * interval] != first:
            ans -= 1
        interval += 1
print(ans)

Submission status: Accepted

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

count = [0] * 3

for i in range(n):
    if s[i] == 'R':
        count[0] += 1
    if s[i] == 'G':
        count[1] += 1
    if s[i] == 'B':
        count[2] += 1

ans = count[0] * count[1] * count[2]

for i in range(n):
    k = 1
    while i + 2 * k < n:
        l = []
        l.append(s[i])
        l.append(s[i+k])
        l.append(s[i+2*k])
        if l.count('R') == 1 and l.count('G') == 1 and l.count('B') == 1:
            ans -= 1
        
        k += 1

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=[str(x) for x in input()]

error=0
for i in range(n-2):
    for j in range(i+1,(n+i+1)//2):
        k=2*j-i
        if s[i]!=s[j] and s[j]!=s[k] and s[k]!=s[i]:
            error += 1
r=s.count('R')
g=s.count('G')
b=s.count('B')
print(r*g*b-error)

Submission status: Accepted

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


#total = S.count('R') * S.count('G') * S.count('B')

r = g = b = 0
for s in range(N):
    if S[s] == 'R':
        r += 1
    elif S[s] == 'G':
        g += 1
    else:
        b += 1

total = r * g * b


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

print(total)

Submission status: Accepted

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


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


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

print(total)

Submission status: Accepted

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

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

#j-i= k-j k=2*j-i

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

Submission status: Accepted

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

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

#j-i= k-j k=2*j-i

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

Submission status: Accepted

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

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

for i in range(N):
    for j in range(i+1,N):
        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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
S=input()
count=0
count=S.count('R')*S.count('G')*S.count('B')
for i in range(N-2):
    for j in range(1,(N+i+1)//2-i):

        if(S[i]+S[i+j]+S[i+2*j] in ['RGB','RBG','GRB','GBR','BGR','BRG']):

            count=count-1
print(count)

Submission status: Accepted

Solution code:
import sys


def main():
    input = sys.stdin.readline
    N = int(input())
    S = input().strip()
    A = S.count('R')*S.count('G')*S.count('B')
    assert 1 <= N <= 4000
    assert N == len(S)
    total = 0
    for j in range(1, N-1):
        for i in range(j):
            k = 2*j-i
            if k < len(S):
                if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                    total += 1
    print(A - total)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys


def main():
    input = sys.stdin.readline
    N = int(input())
    S = input().strip()
    A = S.count('R')*S.count('G')*S.count('B')
    assert 1 <= N <= 4000
    total = 0
    for j in range(1, N-1):
        for i in range(j):
            k = 2*j-i
            if k < len(S):
                if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                    total += 1
    print(A - total)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
import collections


def main():
    input = sys.stdin.readline
    N = int(input())
    S = input().strip()
    A = S.count('R')*S.count('G')*S.count('B')
    total = 0
    for j in range(1, N-1):
        for i in range(j):
            k = 2*j-i
            if k < len(S):
                if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                    total += 1
    print(A - total)


def main2():
    N = int(input())
    S = input()
    A = S.count('R')*S.count('G')*S.count('B')
    order = 0
    for i in range(N-2):
        for j in range(i+1, N-1):
            order += 1
            if 2*j-i < N:
                if S[i] != S[j] and S[i] != S[2*j-i] and S[j] != S[2*j-i]:
                    A -= 1
    print(A)
    print(order)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
S=input()
A=S.count('R')*S.count('G')*S.count('B')
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[i] != S[2*j-i] and S[j] != S[2*j-i]:
                A-=1
print(A)

Submission status: Accepted

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

Submission status: Accepted

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

r = 0
g = 0
b = 0

for i in range(N):
  if S[i] == "R":
    r += 1
  elif S[i]== "G":
    g += 1
  else:
    b += 1
         
a = 0         
         
for i in range(N):
  for j in range(i+1,N):
    k = 2*j - i
    if k < N:
      if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
        a += 1
      
print(r*g*b - a)

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
    elif S[i]=="B":
        b+=1
# j-i=k-j  2j-i-k=0 k=i-2j について調べるもしRGBなら、それをrgbからマイナスする
tmp=0
for i in range(N):
    for j in range(i+1,N):
        k=2*j-i
        if k<N and k>=0:
            if S[i]!=S[j] and S[i]!=S[k] and S[j]!=S[k]:
                tmp+=1
# print("rgb",r*g*b)
# print("tmp",tmp)
print(r*g*b-tmp)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools
N = int(input())
S = input()
out = S.count('R') * S.count('G') * S.count('B')

RGBs = list(itertools.permutations(['R', 'G', 'B'], 3))

for i in range(1, N):
  for j in range(N-i*2):
    if (S[j], S[j+i], S[j+i*2]) in RGBs:
      out -= 1
print(out)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
s = list(S)
r = 0
g = 0
b = 0
for i in range(N):
    if s[i] == 'R':
        r = r + 1
    if s[i] == 'G':
        g = g + 1
    if s[i] == 'B':
        b = b + 1
p = 0
for i in range(N-1):
    for j in range(i+1,N):
        if 2*j-i < N:
            if s[2*j-i] == s[i] or s[2*j-i] == s[j] or s[i] == s[j]:
                p = p
            else:
                p = p+1
        else:
            break
print(r*g*b-p)

Submission status: Accepted

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

an = s.count('R') *s.count('G') *s.count('B')

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

print(an)

Submission status: Accepted

Solution code:
import sys
import math
from collections import deque
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
def I(): 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 S(): return sys.stdin.readline()
def LS(): return sys.stdin.readline().split()
def LS2(N): return [sys.stdin.readline().split() for i in range(N)]
def FILL(i,h,w): return [[i for j in range(w)] for k in range(h)]
def sisha(num,digit): return Decimal(str(num)).quantize(Decimal(digit),rounding=ROUND_HALF_UP)
#'0.01'や'1E1'などで指定、整数に戻すならintをかます
MOD = 1000000007
INF = float("inf")
sys.setrecursionlimit(10**6+10)

input = sys.stdin.readline


N = I()
S = input().rstrip()
R = S.count('R')
G = S.count('G')
B = N-R-G

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

Submission status: Accepted

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


c = 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[k] != s[i]:
            c = c+1
            
        

R = 0
G = 0
B = 0

for i in range(n):
    if s[i] == 'R':
        R = R+1
    elif s[i] == 'G':
        G = G+1
    elif s[i] == 'B':
        B = B+1

print(R*G*B-c)

Submission status: Accepted

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
from collections import deque
from fractions import gcd
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def inputI(): return int(input().strip())
def inputS(): return input().strip()
def inputIL(): return list(map(int,input().split()))
def inputSL(): return list(map(str,input().split()))
def inputILs(n): return list(int(input()) for _ in range(n))
def inputSLs(n): return list(input().strip() for _ in range(n))
def inputILL(n): return [list(map(int, input().split())) for _ in range(n)]
def inputSLL(n): return [list(map(str, input().split())) for _ in range(n)]

#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])
    return arr

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#############
# Main Code #
#############

N = inputI()
S = inputS()

r=0
g=0
b=0
for i in S:
  if i =="R":
    r+=1
  elif i =="G":
    g+=1
  else:
    b+=1

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

print(r*b*g-count)

Submission status: Accepted

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

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

minus = 0

for i in range(n):
    for j in range(i+1,n):
        k = 2*j - i
        if k >= n or j >= n:
            break
        if s[i] != s[j] and s[j]!= s[k] and s[i] != s[k]:
            minus += 1
            
            
print(r*g*b - minus)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
r = s.count('R')
g = s.count('G')
b = n - r - g

ans = r * g * b
for i in range(1, (n // 2) + 1):
    for j in range(0, n - (2 * i) + 1):
        if j + (2 * i) >= n:
            break
        if ((s[j] != s[j + i]) & (s[j] != s[j + (2 * i)]) & (s[i + j] != s[j + (2 * i)])):
            ans -= 1
        
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
str=input()
count=str.count("R")*str.count("G")*str.count("B")
count0=0
for i in range(0,n-2):
    for j in range(i+1,int((n+i-1)/2)+1):
        k=2*j-i
        if str[k]!=str[i] and str[k]!=str[j] and str[i]!=str[j]:
            count0=count0+1
print(count-count0)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
nr=s.count('R')
ng=s.count('G')
nb=s.count('B')
ans=nr*ng*nb
for i in range(1,1+(n-1)//2):
      for j in range(n-2*i):
            if s[j]!=s[j+i] and s[j]!=s[2*i+j] and s[j+i]!=s[j+2*i]:
                  ans-=1
print(ans)

Submission status: Accepted

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

ans = s.count('R')*s.count('G')*s.count('B')
for i in range(n-2):
    for j in range(i+1, i+(n-i-1)//2+1):
        k = 2*j-i
        if k < n and s[k] != s[i] and s[k] != s[j] and s[i] != s[j]:
            ans -= 1

print(ans)

Submission status: Accepted

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

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

for i in range(N-2):
  for j in range(i+1,N-1):
    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)

Submission status: Accepted

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

#条件1を満たす(i,j,k)の組→R,G,Bの選び方
r,g,b = S.count('R'),S.count('G'),S.count('B')

ans = r*g*b

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

print(ans)

Submission status: Accepted

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

RGB = [list()] * 3
color = ['R', 'G', 'B']
RGB[0] = [i for i, s in enumerate(S) if s == color[0]]
RGB[1] = [i for i, s in enumerate(S) if s == color[1]]
RGB[2] = [i for i, s in enumerate(S) if s == color[2]]

order = sorted([0, 1, 2], key=lambda i: len(RGB[i]))
color = [color[i] for i in order]
RGB = [RGB[i] for i in order]

# b = list()
ans = len(RGB[0]) * len(RGB[1]) * len(RGB[2])
for c1 in RGB[0]:
    for c2 in RGB[1]:
        p = c1 * 2 - c2
        if 0 <= p and p < N and S[p] == color[2]:
            # b.append((c1, c2, p))
            ans -= 1

        p = c2 * 2 - c1
        if 0 <= p and p < N and S[p] == color[2]:
            # b.append((c1, c2, p))
            ans -= 1

        if (c1 + c2) % 2 == 0 and S[(c1 + c2) // 2] == color[2]:
            # b.append(c1, c2, c1 + c2 / 2)
            ans -= 1

print(ans)

# b = [sorted(t) for t in b]
# print(sorted(b))

Submission status: Accepted

Solution code:
n = int(input())
s = input()
c = s.count

# 文字列からR,G,Bをひとつずつを選んだときの組み合わせ総数をカウントする
ans = c("R")*c("G")*c("B")

for a in range(n-2):  # 基準
    for j in range((n-a+1)//2):  # 文字の距離
        # 全ての組み合わせから距離の等しい組み合わせを排除する
        # 距離の等しい組み合わせは、a,b,cの組み合わせのうち、
        # aを固定、a-bの距離をjで固定したとき,b=a+j, c=a+2*jと表せる
        if s[a] != s[a+j] and s[a+j] != s[a+2*j] and s[a+2*j] != s[a]:
            ans -= 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
Rcount = 0
Gcount = 0
Bcount = 0
cnt = 0
for i in range(len(s)):
         if s[i] == "R":
                  Rcount += 1
         elif s[i] == "G":
                  Gcount += 1
         elif s[i] == "B":
                  Bcount += 1
RGB = Rcount * Gcount * Bcount
for i in range(1,(len(s)//2)+2):
         for j in range(len(s)):
                  if i * 2 + j >= len(s):
                           break
                  else:
                           if s[j] != s[j+i] and s[j+i] != s[j+i+i] and s[j+i+i] != s[j]:
                                    cnt += 1
print(RGB - cnt)

Submission status: Accepted

Solution code:
import sys

n = int(sys.stdin.readline().rstrip())
s = sys.stdin.readline().rstrip()
r = []
b = []
g = []
for i, si in enumerate(s):
    if si == 'R':
        r.append(i + 1)
    elif si == 'G':
        g.append(i + 1)
    elif si == 'B':
        b.append(i + 1)
count = 0
for i in range(n):
    for j in range(i + 1, n):
        k = j + j - i
        if k < n and s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
            count += 1
print(len(r) * len(g) * len(b) - count)

Submission status: Accepted

Solution code:
def D():
    n = int(input())
    s = input()
    r,g,b = [],[],[]
    for i in range(n):
        if s[i]=="R": r.append(i)
        elif s[i]=="G": g.append(i)
        else: b.append(i)
    ans = len(r)*len(g)*len(b)
    for i in range(n):
        for j in range(i,n):
            if j + (j-i) >= n: continue
            if s[i]!=s[j] and s[i]!=s[2*j-i] and s[j]!=s[2*j-i]:
                ans -= 1
    print(ans)

D()

Submission status: Accepted

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

ri = [i for i, x in enumerate(s) if x=='R']
gi = [i for i, x in enumerate(s) if x=='G']
bi = [i for i, x in enumerate(s) if x=='B']

ans = len(ri) * len(gi) * len(bi)

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
r = 0
g = 0
b = 0
for s in S:
    if s == "R":
        r += 1
    elif s == "G":
        g += 1
    else:
        b += 1

ret = r * g * b

for i in range(N):
    for d in range(N):
        j = i + d
        k = j + d
        if k >= N:
            break
        if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
            ret -= 1

print(ret)

Submission status: Accepted

Solution code:
n=int(input())
s=list(input())
ans=s.count('R')*s.count('G')*s.count('B') #1番目の条件を満たすものの総数は(Rの個数)*(Gの個数)*(Bの個数)
for i in range(n): #判定を簡単にするためR:=1,G:=2,B:=4と置き換えておく (i<j<K)
    if s[i]=='R':
        s[i]=1
    elif s[i]=='G':
        s[i] = 2
    elif s[i]=='B':
        s[i]=4
for i in range(n): #すべての(i,j) (i<j)について(i,j,2*j-i)が1番目の条件を満たし、かつ2番目の条件を満たさないかを判定する
    for j in range(i+1,n):
        k=2*j-i
        if k>=n: #k>=nのとき、そのような組を取ることはできない
            continue
        if s[i]+s[j]+s[k]==7: #i番目の数とj番目の数とk番目の数の和が7になるとき、(i,j,2*j-i)は1番目の条件を満たし、かつ2番目の条件を満たさないので、1番目の条件を満たすものの総数から1を引く
            ans-=1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
ans = s.count('R') * s.count('G') * s.count('B')

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

print(ans)

Submission status: Accepted

Solution code:
def main():
    import collections
    import sys
    sys.setrecursionlimit(5000000)
    input = sys.stdin.readline
    N=int(input())
    S=input()
    if N<=3:
        print(0)
    else:
        str_list=list(S[:N])
        result=0
        c=collections.Counter(str_list)
        RGB_list=c.most_common()

        if len(c)<3:
            print(0)

        else:

            list1=[i for i, x in enumerate(str_list) if x == RGB_list[0][0]]
            list2=[i for i, x in enumerate(str_list) if x == RGB_list[1][0]]
            list3=[i for i, x in enumerate(str_list) if x == RGB_list[2][0]]
            list23=[]

        
            for i in list3:
                for j in list2:
                    list23+=[2*i-j,2*j-i,(i+j)/2]
            c1=collections.Counter(list23)
            for k in list1:
                for l in c1.keys():
                    if k==l:
                        result+=c1[l]
            print(int(RGB_list[0][1]*RGB_list[1][1]*RGB_list[2][1]-result))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
S = input()
rcnt = S.count('R')
gcnt = S.count('G')
bcnt = S.count('B')
ans = rcnt * gcnt * bcnt

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