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:
# -*- 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

			for k in range(tmptmp,len(B)):
				if G[j] - val == B[k] - G[j]:
					pass
				else:
					ans += 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

			for k in range(tmptmp,len(G)):
				if B[j] - val == G[k] - B[j]:
					pass
				else:
					ans += 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

			for k in range(tmptmp,len(R)):
				if G[j] - val == R[k] - G[j]:
					pass
				else:
					ans += 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

			for k in range(tmptmp,len(G)):
				if R[j] - val == G[k] - R[j]:
					pass
				else:
					ans += 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 = []
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)
x = 0
for i in r:
    for j in g:
        for k in b:
            if j - i != k - j:
                x += 1
print(x)

Submission status: Wrong Answer

Solution code:
import sys
from itertools import combinations

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

	sum = 0

	l = list(range(N))

	comb = list(combinations(l,3))

	for c in comb:
		print(c)
		if (S[c[0]] != S[c[1]]) and (S[c[0]] != S[c[2]]) and (S[c[1]] != S[c[2]]):
			if (c[1] - c[0]) != (c[2] - c[1]):
				sum += 1

	print(sum)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
ans = 0
for i in range(N):
    for j in range(i+1,N):
        for k in range(j+1, N):
            if 2*j == k+i:
                continue
            if S[i]!=S[k]!=S[j]:
                ans+=1
print(ans)

Submission status: Wrong Answer

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

cnt = 0
for i in range(N - 2):
  for j in range(i + 1, N - 1):
    if S[i] == S[j]:
      break
    for k in range(j + 1, N):
      if S[i] != S[k] and S[j] != S[k] and (j - i != k - j):
        cnt += 1
print(cnt)

Submission status: Wrong Answer

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

sum = 0
r_n = 0
g_n = 0
b_n = 0
amount = n * (n-1) * (n-2) // 6

r_n = "R" in s
g_n = "G" in s
b_n = "B" in s

if r_n > 1: r_n = r_n * (r_n - 1) // 2
if g_n > 1: g_n = g_n * (g_n - 1) // 2
if r_n > 1: b_n = b_n * (b_n - 1) // 2
sum = amount - r_n - g_n - b_n
print(sum)

Submission status: Wrong Answer

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

def solve(N: int, S: str):
    r = []
    g = []
    b = []

    for i, x in enumerate(S):
        if x ==  "R":r.append(i)
        elif x =="G":g.append(i)
        elif x =="B":b.append(i)

    r = np.array(r)
    g = np.array(g)
    b = np.array(b)


    def rgb(r, g, b):
        sb = set(b)
        ans = 0
        for r_ in r:
            for g_ in g[g>r_]:
                if (g_ + (g_ - r_)) in sb:
                    ans -= 1
                if (g_ - (g_ - r_)) in sb:
                    ans -= 1
                ans += len(b)
        return ans

    k = rgb(r,g,b) + rgb(b,g,r) + rgb(g,r,b)
    print(k)


    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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: Wrong Answer

Solution code:
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 = 0
if(N<=3):
  print(0)
else:
  for i in range(N):
    if(S[i]=="R"):
      R.pop(0)
      for j in range(len(G)):
        for k in range(len(B)):
          if(G[j]<B[k]):
            if(G[j]-i != B[k]-G[j]):
              ans += 1
          elif(B[k]<G[j]):
            if(B[k]-i != G[j]-B[k]):
              ans += 1
    elif(S[i]=="G"):
      G.pop(0)
      for j in range(len(R)):
        for k in range(len(B)):
          if(R[j]<B[k]):
            if(R[j]-i != B[k]-R[j]):
              ans += 1
          elif(B[k]<R[j]):
            if(B[k]-i != R[j]-B[k]):
              ans += 1
    else:
      B.pop(0)
      for j in range(len(R)):
        for k in range(len(G)):
          if(R[j]<G[k]):
            if(R[j]-i != G[k]-R[j]):
              ans += 1
          elif(G[k]<R[j]):
            if(G[k]-i != R[j]-G[k]):
              ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
import sys

# A, B, C = [int(x) for x in stdin.readline().rstrip().split()]
# A,B = [int(x) for x in stdin.readline().rstrip().split()]
# U = input().split()
# A, B = [str(x) for x in stdin.readline().rstrip().split()]
N = int(input())
S = list(input())
r = []
g = []
b = []
for i in range(N):
    if S[i] == 'R':
        r.append(i+1)
    elif S[i] == 'G':
        g.append(i+1)
    else:
        b.append(i+1)
print(r)
print(g)
print(b)
cnt = 0
for i in range(len(r)):
    for j in range(len(g)):
        for k in range(len(b)):
            if r[i] != min(r[i], g[j], b[k]) and r[i] != max((r[i], g[j], b[k])):
                if r[i]-min(r[i], g[j], b[k]) != max((r[i], g[j], b[k]))-r[i]:
                    cnt += 1
            elif g[j] != min(r[i], g[j], b[k]) and g[j] != max((r[i], g[j], b[k])):
                if g[j]-min(r[i], g[j], b[k]) != max((r[i], g[j], b[k]))-g[j]:
                    cnt += 1
            else:
                if b[k]-min(r[i], g[j], b[k]) != max((r[i], g[j], b[k]))-b[k]:
                    cnt += 1
print(cnt)

Submission status: Wrong Answer

Solution code:
import collections
n = int(input())
s = list(input())

ss = collections.Counter(s)
ans = 1
for i in ss:
    ans *= ss[i]

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

Submission status: Wrong Answer

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

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

# print(RGB)

ans = 0
for n in range(3):
    for i in range(len(RGB[n])):
        for j in range(len(RGB[(n + 1) % 3])):
            for k in range(len(RGB[(n + 2) % 3])):
                if RGB[n][i] < RGB[(n + 1) % 3][j] < RGB[(n + 2) % 3][k] and RGB[(n + 1) % 3][j] - RGB[n][i] != RGB[(n + 2) % 3][k] - RGB[(n + 1) % 3][j]:
                    # print(RGB[n][i], RGB[(n + 1) % 3][j], RGB[(n + 2) % 3][k])
                    ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
r = []
g = []
b = []
n_r = n_g = n_b = 0

for i in range(N):
    if S[i] == "R":
        r.append(i)
        n_r += 1
    elif S[i] == "G":
        g.append(i)
        n_g += 1
    else:
        b.append(i)
        n_b += 1

all = n_r*n_b*n_g

l = N//2
tmp = 0
for i in range(1,l):
    j = 0
    while j+2*i<=N-1:
        if S[j] != S[j+i] and S[j+i] != S[j+2*i] and S[j] != S[j+2*i]:
            tmp += 1
#            print(j,j+i,j+i*2)
        j += 1
print(all-tmp)

Submission status: Wrong Answer

Solution code:
import collections

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

c = collections.Counter(s)

ans_count = c['R'] * c['G'] * c['B']

for i in range(1, (n-1)//2):
    for j in range(n):
        if j+(2*i) >= n:
            break

        if s[j] != s[j+i] and s[j+i] != s[j+(2*i)] and s[j] != s[j+(2*i)]:
            ans_count -= 1


print(ans_count)

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

			for k in range(tmptmp,len(B)):
				#if G[j] - val == B[k] - G[j]:
					#pass
				#else:
					ans += 1

	for i,val in enumerate(R):
		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

			for k in range(tmptmp,len(G)):
				#if B[j] - val == G[k] - B[j]:
					#pass
				#else:
					ans += 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

			for k in range(tmptmp,len(R)):
				#if G[j] - val == R[k] - G[j]:
					#pass
				#else:
					ans += 1

	for i,val in enumerate(B):
		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

			for k in range(tmptmp,len(G)):
				if R[j] - val == G[k] - R[j]:
					pass
				else:
					ans += 1



	for i,val in enumerate(G):
		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

			for k in range(tmptmp,len(B)):
				if R[j] - val == B[k] - R[j]:
					pass
				else:
					ans += 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 = list(input())

R=[0]*n
G=[0]*n
B=[0]*n

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

ans=0
for i in range(n):
    if R[i]==1:
        lG=sum(G[:i])
        lB=sum(B[:i])
        rG=sum(G[i:])
        rB=sum(B[i:])
        ans+=(lG+rG)*(lB+rB)
        for j in range(1,n//2):
            if R[i]==1:
                if 0<=i-j<n and 0<=i+j<n:
                    if G[i-j]==1 and B[i+j]==1:
                        ans-=1
                    if B[i-j]==1 and G[i+j]==1:
                        ans-=1
                if 0<=i-2*j<n:
                    if G[i-j]==1 and B[i-2*j]==1:
                        ans-=1
                    if B[i-j]==1 and G[i-2*j]==1:
                        ans-=1
                if 0<=i+2*j<n:
                    if G[i+j]==1 and B[i+2*j]==1:
                        ans-=1
                    if B[i+j]==1 and G[i+2*j]==1:
                        ans-=1

print(ans)

Submission status: Wrong Answer

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

rc = S.count("R")
gc = S.count("G")
bc = S.count("B")

tc = int(rc*gc*bc)

aa = 0
for i in range(N):
  for j in range(1,N//2):
    
    if i+j+j < N:
      if S[i] != S[i+j]:
        if S[i] != S[i+j+j]:
          if S[i+j] != S[i+j+j]:
            aa += 1
            
print(int(tc-aa))

Submission status: Wrong Answer

Solution code:
length = int(input())
rgb = input()
count = 0

r = [index for index, r in enumerate(rgb, start=1) if r == 'R']
g = [index for index, g in enumerate(rgb, start=1) if g == 'G']
b = [index for index, b in enumerate(rgb, start=1) if b == 'B']

for rr in r:
    for gg in g:
        if gg < rr:
            continue
        for bb in b:
            if bb < gg:
                continue
            if rr != gg != bb and gg-rr != bb-gg:
                count += 1
print(count)

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

			for k in range(tmptmp,len(B)):
				#if G[j] - val == B[k] - G[j]:
					#pass
				#else:
					ans += 1

	for i,val in enumerate(R):
		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

			for k in range(tmptmp,len(G)):
				#if B[j] - val == G[k] - B[j]:
					#pass
				#else:
					ans += 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

			for k in range(tmptmp,len(R)):
				#if G[j] - val == R[k] - G[j]:
					#pass
				#else:
					ans += 1

	for i,val in enumerate(B):
		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

			for k in range(tmptmp,len(G)):
				#if R[j] - val == G[k] - R[j]:
					#pass
				#else:
					ans += 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

			for k in range(tmptmp,len(R)):
				#if B[j] - val == R[k] - B[j]:
					#pass
				#else:
					ans += 1

	for i,val in enumerate(G):
		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

			for k in range(tmptmp,len(B)):
				#if R[j] - val == B[k] - R[j]:
					#pass
				#else:
					ans += 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()

w_r = []
w_g = []
w_b = []
for i in range(len(S)):
    if S[i] == 'R':
        w_r.append(i)
    elif S[i] == 'G':
        w_g.append(i)
    else:
        w_b.append(i)

ret = 0


def calc(w_1, w_2, w_3):
    ret = 0
    for i in w_1:
        for k in w_2:
            for jj in w_3:
                j = int(i + k // 2)
                if i < k and j != jj and i < jj and jj < k:
                    ret += 1
    return ret


ret += calc(w_r, w_g, w_b)
ret += calc(w_r, w_b, w_g)
ret += calc(w_g, w_r, w_b)
ret += calc(w_g, w_b, w_r)
ret += calc(w_b, w_r, w_g)
ret += calc(w_b, w_g, w_r)

print(ret)

Submission status: Wrong Answer

Solution code:
n = int(input())
List = list(input())
count = 0
print(List)
for k in range(n - 2):
    for l in range(k + 1, n - 1):
        if List[k] != List[l]:
            for m in range(l + 1, min(2 * l - k, n)):
                if List[k] != List[m] and List[l] != List[m]:
                    count += 1
            for m in range(min(2 * l - k + 1, n), n):
                if List[k] != List[m] and List[l] != List[m]:
                    count += 1
print(count)