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: Wrong Answer

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

ans = 0
if r > 1 and g > 1 and b > 1:
    ans = r*g*b
    for i in range(1,n):
        m = min(i,n-i-1)
        for j in range(1,m+1):
            A,B,C = s[i],s[i-j],s[i+j]
            if A != B and A != C and B != C:
                ans -= 1
else:
    ans = 1
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline
from collections import defaultdict
from heapq import heappop, heappush

from decimal import *

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

##### 累乗 #####
# pow(x, y, z) -> x**y % z

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

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

	R = []
	G = []
	B = []
	for i,val in enumerate(S):
		if val == "R":
			R.append(i)
		elif val == "G":
			G.append(i)
		else:
			B.append(i)

	ans = 0
	for i,val in enumerate(R):
		tmp = bisect_right(G, val)
		if len(G) == tmp:
			break

		for j in range(tmp,len(G)):
			tmptmp = bisect_right(B, G[j])
			if len(B) == tmptmp:
				break

			blacks = G[j] - val + G[j]
			lislis = B[tmptmp:]
			fst = bisect_right(lislis, blacks)
			snd = bisect_left(lislis, blacks)

			#print("{} {}".format(fst,snd))
			if snd+1 == fst:
				ans += len(B)-tmptmp
			else:
				ans += len(B)-tmptmp-1

		tmp = bisect_right(B, val)
		if len(B) == tmp:
			break

		for j in range(tmp,len(B)):
			tmptmp = bisect_right(G, B[j])
			if len(G) == tmptmp:
				break

			blacks = B[j] - val + B[j]
			lislis = G[tmptmp:]
			fst = bisect_right(lislis, blacks)
			snd = bisect_left(lislis, blacks)
			if snd+1 == fst:
				ans += len(G)-tmptmp
			else:
				ans += len(G)-tmptmp-1


	for i,val in enumerate(B):
		tmp = bisect_right(G, val)
		if len(G) == tmp:
			break

		for j in range(tmp,len(G)):
			tmptmp = bisect_right(R, G[j])
			if len(R) == tmptmp:
				break

			blacks = G[j] - val + G[j]
			lislis = R[tmptmp:]
			fst = bisect_right(lislis, blacks)
			snd = bisect_left(lislis, blacks)
			if snd+1 == fst:
				ans += len(R)-tmptmp
			else:
				ans += len(R)-tmptmp-1

		tmp = bisect_right(R, val)
		if len(R) == tmp:
			break

		for j in range(tmp,len(R)):
			tmptmp = bisect_right(G, R[j])
			if len(G) == tmptmp:
				break

			blacks = R[j] - val + R[j]
			lislis = G[tmptmp:]
			fst = bisect_right(lislis, blacks)
			snd = bisect_left(lislis, blacks)
			if snd+1 == fst:
				ans += len(G)-tmptmp
			else:
				ans += len(G)-tmptmp-1

	for i,val in enumerate(G):
		tmp = bisect_right(B, val)
		if len(B) == tmp:
			break

		for j in range(tmp,len(B)):
			tmptmp = bisect_right(R, B[j])
			if len(R) == tmptmp:
				break

			blacks = B[j] - val + B[j]
			lislis = R[tmptmp:]
			fst = bisect_right(lislis, blacks)
			snd = bisect_left(lislis, blacks)
			if snd+1 == fst:
				ans += len(R)-tmptmp
			else:
				ans += len(R)-tmptmp-1

		tmp = bisect_right(R, val)
		if len(R) == tmp:
			break

		for j in range(tmp,len(R)):
			tmptmp = bisect_right(B, R[j])
			if len(B) == tmptmp:
				break

			blacks = R[j] - val + R[j]
			lislis = B[tmptmp:]
			fst = bisect_right(lislis, blacks)
			snd = bisect_left(lislis, blacks)
			if snd+1 == fst:
				ans += len(B)-tmptmp
			else:
				ans += len(B)-tmptmp-1

	print(ans)

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# special thanks :
#  https://nagiss.hateblo.jp/entry/2019/07/01/185421
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
class Graph(object):
    """
    隣接リストによる有向グラフ
    """

    def __init__(self):
        self.graph = defaultdict(list)

    def __len__(self):
        return len(self.graph)

    def add_edge(self, src, dst, weight=1):
        self.graph[src].append((dst, weight))

    def get_nodes(self):
        return self.graph.keys()


class Dijkstra(object):
    """
    ダイクストラ法（二分ヒープ）による最短経路探索
    計算量: O((E+V)logV)
    """

    def __init__(self, graph, start):
        self.g = graph.graph

        # startノードからの最短距離
        # startノードは0, それ以外は無限大で初期化
        self.dist = defaultdict(lambda: float('inf'))
        self.dist[start] = 0

        # 最短経路での1つ前のノード
        self.prev = defaultdict(lambda: None)

        # startノードをキューに入れる
        self.Q = []
        heappush(self.Q, (self.dist[start], start))

        while self.Q:
            # 優先度（距離）が最小であるキューを取り出す
            dist_u, u = heappop(self.Q)
            if self.dist[u] < dist_u:
                continue
            for v, weight in self.g[u]:
                alt = dist_u + weight
                if self.dist[v] > alt:
                    self.dist[v] = alt
                    self.prev[v] = u
                    heappush(self.Q, (alt, v))

    def shortest_distance(self, goal):
        """
        startノードからgoalノードまでの最短距離
        """
        return self.dist[goal]

    def shortest_path(self, goal):
        """
        startノードからgoalノードまでの最短経路
        """
        path = []
        node = goal
        while node is not None:
            path.append(node)
            node = self.prev[node]
        return path[::-1]

def int_sqrt2(n):
    def f(prev):
        while True:
            m = (prev + n / prev) / 2
            if m >= prev:
                return prev
            prev = m

    return f(int(math.sqrt(n) * (1 + 1e-10)))

# nCr mod m
# rがn/2に近いと非常に重くなる
def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res

# mを法とするaの乗法的逆元
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y

# nHr mod m
# 問題によって、combination()を切り替えること
def H(n, r, mod=10**9+7):
	# comb = Combination(n+r-1, mod)
	# return comb(n+r-1, r)
    return combination(n+r-1, r, mod)

class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  # 10
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        # 階乗のリストと階乗のmod逆元のリストを返す O(n)
        # self.make_modinv_list()が先に実行されている必要がある
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        # 0からnまでのmod逆元のリストを返す O(n)
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
R = 0
G = 0
B = 0
notToAllow = 0


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

total = R*G*B

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

print(R*G*B - notToAllow)

Submission status: Wrong Answer

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

count = 0
r=0
g=0
b=0
for i in range(0,n):
  if s[i] == 'R':
    r = r + 1
  if s[i]=='G':
    g = g+1
  if s[i]=='B':
    b = b+1
    
for i in range(0,n-3):
  ii = s[i]
  for j in range(i+1, n-1):
    jj = s[j]
    if ii==jj:
      continue
    if (2*j-i)<=(n-1):
      if ii!=s[2*j-i] and jj!=s[2*j-i]:
        count = count + 1
print(r*g*b-count)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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: Wrong Answer

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):
    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: Wrong Answer

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

R_Count=0
G_Count=0
B_Count=0

for i in range(len(S)):
    if S[i] == "R":
        R_Count += 1
    if S[i] == "G":
        G_Count += 1
    if S[i] == "B":
        B_Count += 1

result = R_Count * G_Count * B_Count

for i in range(1, N+1):
    for j in range(i+1, N+1):
        for k in range(j+1, N+1):
            if j - i < k - j:
                break
            if j - i == k - j:
                result -= 1

            
print(result)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
Rlist = []
Glist = []
Blist = []
for x in range(len(s)):
    if s[x] == "R": Rlist.append(x)
    elif s[x] == "G": Glist.append(x)
    elif s[x] == "B": Blist.append(x)

def Cmin(r, g):
    result = -1
    if g < r: result = Cmin(g, r)
    else: result = r - (g - r)
    return result
    
def Cmid(r, g):
    result = -1
    if g < r: result = Cmid(g, r)
    elif (g - r) % 2 != 0: result = -1
    else: result = r + (g - r) // 2
    return result

def Cmax(r, g):
    result = -1
    if g < r: result = Cmax(g, r)
    else: result = g + (g - r)
    return result

def Calc(AList, BList, CList):
    ans = 0
    for r in AList:
        for g in BList:
            ans += len(CList)
            if Cmin(r, g) in CList: ans -= 1
            if Cmid(r, g) in CList: ans -= 1
            if Cmax(r, g) in CList: ans -= 1
    return ans

r = len(Rlist)
g = len(Glist)
b = len(Blist)
ans = 0
if r   <= g  <= b: ans = Calc(Rlist, Glist, Blist)
elif r  <= b  <= g: ans = Calc(Rlist, Blist, Glist)
elif g  <= r  <= b: ans = Calc(Glist, Rlist, Blist)
elif g  <= b  <= r: ans = Calc(Glist, Blist, Rlist)
elif r  <= b  <= g: ans = Calc(Rlist, Blist, Glist)
elif r  <= g  <= b: ans = Calc(Rlist, Glist, Blist)
print(ans)

Submission status: Wrong Answer

Solution code:
import math
n = int(input())
s = input()
r_cnt = 0
g_cnt = 0
b_cnt = 0
for i in s:
    if i == 'R':
        r_cnt += 1
    elif i == 'G':
        g_cnt += 1
    elif i == 'B':
        b_cnt += 1
        
total = r_cnt*g_cnt*b_cnt

if n < 4:
    print(total)

if n == 4:
    for j in range(2):
        tmp = s[j]+s[j+1]+s[j+2]
        if (tmp == 'RGB') or (tmp == 'RBG') or (tmp == 'GBR') or (tmp == 'GRB') or (tmp == 'BRG') or (tmp == 'BGR'):
            total -= 1
            
    print(total)
else:
    for i in range(1,math.floor((len(s)-3)/2)+1):
        for j in range(n-2*i):
            tmp = s[j]+s[j+i]+s[j+2*i]
            if (tmp == 'RGB') or (tmp == 'RBG') or (tmp == 'GBR') or (tmp == 'GRB') or (tmp == 'BRG') or (tmp == 'BGR'):
                total -= 1

    print(total)

Submission status: Wrong Answer

Solution code:
def binary_search(target, left, right, nums):
    if(left > right):
        return -1
    mid = (right-left)//2 + left
    if(nums[mid] == target):
        return mid
    elif(nums[mid] < target):
        return binary_search(target, mid+1, right, nums)
    else:
        return binary_search(target, left, mid-1, nums)
def Main(N, S):
    ans = 0
    d = {'R':[], 'G':[], 'B':[]}
    for i, c in enumerate(S):
        d[c].append(i)
    print(d)
    lenb = len(d['B'])
    for i in d['R']:
        for j in d['G']:
            for k in d['B']:
                if abs(j-i) != abs(k-j) and abs(j-i) != abs(k-i) and abs(k-j) != abs(i-k):
                    ans += 1
    return ans
        
    

def main():
    N = int(input())
    S = str(input())
    print(Main(N,S))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline
from collections import defaultdict
from heapq import heappop, heappush

from decimal import *

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

##### 累乗 #####
# pow(x, y, z) -> x**y % z

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

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

	R = []
	G = []
	B = []
	for i,val in enumerate(S):
		if val == "R":
			R.append(i)
		elif val == "G":
			G.append(i)
		else:
			B.append(i)

	ans = 0
	for i,val in enumerate(R):
		tmp = bisect_right(G, val)
		if len(G) == tmp:
			break

		for j in range(tmp,len(G)):
			tmptmp = bisect_right(B, G[j])
			if len(B) == tmptmp:
				break

			blacks = G[j] - val + G[j]
			lislis = B[tmptmp:]
			fst = bisect_right(lislis, blacks)
			snd = bisect_left(lislis, blacks)
			if snd+1 == fst:
				ans += len(B)-tmptmp
			else:
				ans += len(B)-tmptmp-1

		tmp = bisect_right(B, val)
		if len(B) == tmp:
			break

		for j in range(tmp,len(B)):
			tmptmp = bisect_right(G, B[j])
			if len(G) == tmptmp:
				break

			blacks = B[j] - val + B[j]
			lislis = G[tmptmp:]
			fst = bisect_right(lislis, blacks)
			snd = bisect_left(lislis, blacks)
			if snd+1 == fst:
				ans += len(G)-tmptmp
			else:
				ans += len(G)-tmptmp-1


	for i,val in enumerate(B):
		tmp = bisect_right(G, val)
		if len(G) == tmp:
			break

		for j in range(tmp,len(G)):
			tmptmp = bisect_right(R, G[j])
			if len(R) == tmptmp:
				break

			blacks = G[j] - val + G[j]
			lislis = R[tmptmp:]
			fst = bisect_right(lislis, blacks)
			snd = bisect_left(lislis, blacks)
			if snd+1 == fst:
				ans += len(R)-tmptmp
			else:
				ans += len(R)-tmptmp-1

		tmp = bisect_right(R, val)
		if len(R) == tmp:
			break

		for j in range(tmp,len(R)):
			tmptmp = bisect_right(G, R[j])
			if len(G) == tmptmp:
				break

			blacks = R[j] - val + R[j]
			lislis = G[tmptmp:]
			fst = bisect_right(lislis, blacks)
			snd = bisect_left(lislis, blacks)
			if snd+1 == fst:
				ans += len(G)-tmptmp
			else:
				ans += len(G)-tmptmp-1

	for i,val in enumerate(G):
		tmp = bisect_right(B, val)
		if len(B) == tmp:
			break

		for j in range(tmp,len(B)):
			tmptmp = bisect_right(R, B[j])
			if len(R) == tmptmp:
				break

			blacks = B[j] - val + B[j]
			lislis = R[tmptmp:]
			fst = bisect_right(lislis, blacks)
			snd = bisect_left(lislis, blacks)
			if snd+1 == fst:
				ans += len(R)-tmptmp
			else:
				ans += len(R)-tmptmp-1

		tmp = bisect_right(R, val)
		if len(R) == tmp:
			break

		for j in range(tmp,len(R)):
			tmptmp = bisect_right(B, R[j])
			if len(B) == tmptmp:
				break

			blacks = R[j] - val + R[j]
			lislis = B[tmptmp:]
			fst = bisect_right(lislis, blacks)
			snd = bisect_left(lislis, blacks)
			if snd+1 == fst:
				ans += len(B)-tmptmp
			else:
				ans += len(B)-tmptmp-1

	print(ans)

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# special thanks :
#  https://nagiss.hateblo.jp/entry/2019/07/01/185421
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
class Graph(object):
    """
    隣接リストによる有向グラフ
    """

    def __init__(self):
        self.graph = defaultdict(list)

    def __len__(self):
        return len(self.graph)

    def add_edge(self, src, dst, weight=1):
        self.graph[src].append((dst, weight))

    def get_nodes(self):
        return self.graph.keys()


class Dijkstra(object):
    """
    ダイクストラ法（二分ヒープ）による最短経路探索
    計算量: O((E+V)logV)
    """

    def __init__(self, graph, start):
        self.g = graph.graph

        # startノードからの最短距離
        # startノードは0, それ以外は無限大で初期化
        self.dist = defaultdict(lambda: float('inf'))
        self.dist[start] = 0

        # 最短経路での1つ前のノード
        self.prev = defaultdict(lambda: None)

        # startノードをキューに入れる
        self.Q = []
        heappush(self.Q, (self.dist[start], start))

        while self.Q:
            # 優先度（距離）が最小であるキューを取り出す
            dist_u, u = heappop(self.Q)
            if self.dist[u] < dist_u:
                continue
            for v, weight in self.g[u]:
                alt = dist_u + weight
                if self.dist[v] > alt:
                    self.dist[v] = alt
                    self.prev[v] = u
                    heappush(self.Q, (alt, v))

    def shortest_distance(self, goal):
        """
        startノードからgoalノードまでの最短距離
        """
        return self.dist[goal]

    def shortest_path(self, goal):
        """
        startノードからgoalノードまでの最短経路
        """
        path = []
        node = goal
        while node is not None:
            path.append(node)
            node = self.prev[node]
        return path[::-1]

def int_sqrt2(n):
    def f(prev):
        while True:
            m = (prev + n / prev) / 2
            if m >= prev:
                return prev
            prev = m

    return f(int(math.sqrt(n) * (1 + 1e-10)))

# nCr mod m
# rがn/2に近いと非常に重くなる
def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res

# mを法とするaの乗法的逆元
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y

# nHr mod m
# 問題によって、combination()を切り替えること
def H(n, r, mod=10**9+7):
	# comb = Combination(n+r-1, mod)
	# return comb(n+r-1, r)
    return combination(n+r-1, r, mod)

class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  # 10
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        # 階乗のリストと階乗のmod逆元のリストを返す O(n)
        # self.make_modinv_list()が先に実行されている必要がある
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        # 0からnまでのmod逆元のリストを返す O(n)
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
L = [[] for i in range(3)]
for i in range(len(S)):
    if S[i] == 'R':
        L[0].append(i)
    elif S[i] == 'G':
        L[1].append(i)
    else:
        L[2].append(i)
ans = 0

for i in range(3):
    for j in range(3):
        if i == j:
            continue
        for k in range(3):
            if i == k or j == k:
                continue
            sb = 0
            sc = 0
            flgb = 0
            flgc = 0
            for a in range(len(L[i])):
                for b in range(sb, len(L[j])):
                    if L[i][a] <= L[j][b]:
                        if flgb == 0:
                            sb = b
                            flgb = 1
                        for c in range(sc, len(L[k])):
                            if L[j][b] <= L[k][c] and L[j][b] - L[i][a] != L[k][c] - L[j][b]:
                                if flgc == 0:
                                    sc = c
                                    flgc = 1
                                ans += 1

print(ans)

Submission status: Wrong Answer

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

count = 0

for i in range(N):
    for i2 in range(N):
        for i3 in range(N):
            if i < i2 < i3 and S[i] == "R" and S[i2] == "G" and S[i3] == "B" and i2 - i != i3 - i2:
                count += 1

print(count)

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    s = input()
    ans = 0
    allc = set(('R', 'G', 'B'))
    for i in range(n):
        for j in range(i + 1, n):
            a, b = s[i], s[j]
            if a == b:
                continue
            rest = (allc - set((a, b))).pop()
            ans += s[j + 1:].count(rest)
    return ans - 1  # -1 for index rule.


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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
S = input()
Ans = 0
for i in range(1, 1 + N):
    for j in range(2, N):
        for k in range(3, N - 1):
            if i < j and j < k and S[i-1] != S[j-1] and S[j-1] != S[k-1] and S[i-1] != S[k-1] and (j-i) != (k-j):
                        Ans += 1
print(Ans)

Submission status: Wrong Answer

Solution code:
import numpy
import sys
import statistics

num=int(input())
txt=input()
count=0
arr=numpy.zeros(len(txt),dtype=">i4")
for i in range(len(txt)):
  if txt[i]=='G':
    arr[i]=1
  elif txt[i]=='B':
    arr[i]=2
if 0 not in arr or 1 not in arr or 2 not in arr:
  print(0)
  sys.exit()
ri=numpy.where(arr==0)[0]
gi=numpy.where(arr==1)[0]
bi=numpy.where(arr==2)[0]
for i in ri:
  for j in gi:
    for k in bi:
      mean=statistics.mean([float(i),float(j),float(k)])
      med=statistics.median([i,j,k])
      diff=abs(mean-med)
      if diff<=1e-5:
        continue
      count+=1
print(ri)
print(gi)
print(bi)
print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
Rlist = []
Glist = []
Blist = []
for x in range(len(s)):
    if s[x] == "R": Rlist.append(x)
    elif s[x] == "G": Glist.append(x)
    elif s[x] == "B": Blist.append(x)

def Cmin(r, g):
    result = -1
    if g < r: result = Cmin(g, r)
    else: result = r - (g - r)
    return result
    
def Cmid(r, g):
    result = -1
    if g < r: result = Cmid(g, r)
    elif (g - r) % 2 != 0: result = -1
    else: result = r + (g - r) // 2
    return result

def Cmax(r, g):
    result = -1
    if g < r: result = Cmax(g, r)
    else: result = g + (g - r)
    return result

def Calc(AList, BList, CList):
    ans = 0
    for r in AList:
        for g in BList:
            ans += len(CList)
            if Cmin(r, g) in CList: ans -= 1
            if Cmid(r, g) in CList: ans -= 1
            if Cmax(r, g) in CList: ans -= 1
    return ans

r = len(Rlist)
g = len(Glist)
b = len(Blist)
ans = 0
if r < g < b: ans = Calc(Rlist, Glist, Blist)
elif r < b < g: ans = Calc(Rlist, Blist, Glist)
elif g < r < b: ans = Calc(Glist, Rlist, Blist)
elif g < b < r: ans = Calc(Glist, Blist, Rlist)
elif r < b < g: ans = Calc(Rlist, Blist, Glist)
elif r < g < b: ans = Calc(Rlist, Glist, Blist)
print(ans)

Submission status: Wrong Answer

Solution code:
# import itertools


def main():
    n = int(input())
    s = input()
    # comb = itertools.combinations(range(n), 3)
    ans = 0
    allc = set(('R', 'G', 'B'))
    for i in range(n):
        for j in range(i + 1, n):
            a, b = s[i], s[j]
            if a == b:
                continue
            rest = (allc - set((a, b))).pop()
            ans += s[j + 1:].count(rest)
    return ans - 1  # -1 for index rule.


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

Submission status: Wrong Answer

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

l_r = [i for i, s in enumerate(S) if s=='R']
l_g = [i for i, s in enumerate(S) if s=='G']
l_b = [i for i, s in enumerate(S) if s=='B']

ans = len(l_r)*len(l_g)*len(l_b)

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

Submission status: Wrong Answer

Solution code:
N = int(input())
str = input()
i, j, k = [], [], []
total = 0
for n in range(N):
    if str[n]=='R':
        i.append(n+1)
    elif str[n]=='G':
        j.append(n+1)
    else:
        k.append(n+1)

for I in i:
    for J in j:
        total += 1 if not(J*2-I in k) else 0
print(total)

Submission status: Wrong Answer

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(N):
  for j in range(N//2):
    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: Wrong Answer

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

    su = 0
    # for k in range(2, N):
    #     for j in range(1, k):
    #         if S[j] == S[k]:
    #             continue
    #         for i in range(j):
    #             # print(i, j, k)
    #             if j-i == k-j:
    #                 pass
    #             elif S[i] != S[j] and S[i] != S[k]:
    #                 su += 1
    #             # su += 1
    # print(su)

    for i, si in enumerate(S):
        for j, sj in enumerate(S[i+1:]):
            if si == sj:
                continue
            for k, sk in enumerate(S[i+j+2:]):
                # print(si, sj, sk)
                # print(i+1, j+i+2, k+j+i+2)
                if j == k:
                    pass
                elif sj != sk and si != sk:
                    su += 1
                    print('gets')
    print(su)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

R_list=[]
G_list=[]
B_list=[]
daburi = 0

for i in range(n):
    if s.find('R') == 0:
        R_list.append(i)
    elif s.find('G') == 0:
        G_list.append(i)
    else:
        B_list.append(i)
    s = s[1:]

for j in range(len(R_list)):
    for k in range(len(G_list)):
        for l in range(len(B_list)):
            #if abs(R_list[j] - G_list[k]) == abs(G_list[k]- B_list[l]) or abs(R_list[j] - G_list[k]) == abs(R_list[j] - B_list[l]) or abs(G_list[k]- B_list[l]) == abs(R_list[j]- B_list[l]) :
            if abs(R_list[j] - G_list[k]) == abs(G_list[k] - B_list[l]):
                if abs(R_list[j] - G_list[k]) == abs(R_list[j] - B_list[l]):
                    if abs(G_list[k]- B_list[l]) == abs(R_list[j]- B_list[l]) :
                        daburi +=1

ans = len(R_list)*len(G_list)*len(B_list)
print(ans - daburi)