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: Runtime Error

Solution code:
n = int(input())
s = list(str(input()))
z = 0
lnr = [i for i, x in enumerate(s) if x == 'R']
lng = [i for i, x in enumerate(s) if x == 'G']
lnb = [i for i, x in enumerate(s) if x == 'B']     
for i in lnr:
    for j in lnb:
        ln = sorted([i, j])
        if ln[0] + ln[0] - ln[1] in lng:
            z += 1
        if (ln[0] - ln[1]) % 2 == 0:
            if ln[1] + ((ln[0] - ln[1])//2) in lng:
            z += 1
        if ln[1] * 2 - ln[0] >=0:
            if ln[1] - (ln[0] - ln[1]) in lng:
            z += 1
print(len(lnr) * len(lng) * len(lnb) - z)

Submission status: Runtime Error

Solution code:
import bisect
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)
ans = 0
for i in r:
	for j in g:
		ans += len(b)
		if i > j:
			k = j-abs(i-j)
			m = i+abs(i-j)
		else:
			k = j+abs(i-j)
			m = i-abs(i-j)
		if k <= b[-1]:
			x = bisect.bisect_left(b,k)
			if b[x] == k:
				ans -= 1
		if m <= b[-1]:
			x = bisect.bisect_left(b,m)
			if b[x] == m:
				ans -= 1
		if i > j:
			k = i-abs(i-j)
		else:
			k = i+abs(i-j)
		if k in b:
			ans -= 1
		l = (i+j)//2
		if abs(i-l) == abs(j-l):
			x = bisect.bisect_left(b,l)
			if b[x] == l:
				ans -= 1
print (ans)

Submission status: Runtime Error

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

d = {}
for c in S:
    if c not in d:
        d[c] = 1
    else:
        d[c] += 1
total = d["R"] * d["G"] * d["B"]
#print(total)

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

print(total - dame)

Submission status: Runtime Error

Solution code:

def main():
    N = int(input())
    S = input()
    count = 0
    RGB = ["R", "G", "B"]

    for i in range(1, N-1):
        RGB = ["R", "G", "B"]
        RGB.remove(S[i - 1])
        for j in range(i+1, N):
            if S[i-1] == S[j-1]:
                continue
            RGB.remove(S[j-1])
            count += S.count(RGB[0], j, N)
            #print(S.count(RGB[0], j, N))
            if 2 * j - i > N:
                pass
            else:
                if S[2 * j - i - 1] == RGB[0]:
                    count -= 1

    print(count)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import numpy as np
n = int(input())
s = np.array(list(input()))

ans=0
# def calc(n):
# if ans[n] != 0:
#     return ans[n]
# else:
for i in range(0, n-2):
    for j in range(i, n-1):
        if s[i] == s[j]:
            continue
        for k in range(j, n):
            if s[k]==s[j]or s[i]==s[k]:
                continue
            if j-i == k-j:
                continue
            ans+=1
print(ans)

Submission status: Runtime Error

Solution code:
from math import gcd
from collections import Counter
from functools import reduce
from math import ceil, floor


def gcd_lst(numbers):
    return reduce(gcd, numbers)


n = int(input())
s = input()
s_cnt = Counter(s).most_common()
rgb_num = []
for key, val in s_cnt:
    rgb_num.append(val)
rgb_gcd = (rgb_num[0] * rgb_num[1] * rgb_num[2]) // gcd_lst(rgb_num)
dif_num = 0
for i in range(n-2):
    for j in range(i+1, ceil((n + i)/2)):
        if  (2 * j - i) < n:
            if s[i] != s[j] and s[2 * j - i] != s[i] and s[2 * j - i] != s[j]:
                dif_num += 1
print(rgb_gcd - dif_num)

Submission status: Runtime Error

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

i2 = range(len(S) - 3)
j2 = range(i + 1, len(S) - 1)
k2 = range(j + 1, len(S))
count = 0
for i in i2:
    for j in j2:
        for k in k2:
            if j - i == k -j: continue
            if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                count += 1
                

print(count)

Submission status: Runtime Error

Solution code:
import itertools
N = int(input())
S = list(input())
for i,j,k in list(itertools.combinations(range(1,N+1),3)):
    if len(set([S[i-1],S[j-1],S[k-1]]))==3:
      if k-j != j-i:
        ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
#from operator import itemgetter
#from collections import Counter
#import heapq
N = int(input())
S = input()
#N, M = map(int, input().split())
#A = list(map(lambda x: int(x)*(-1), input().split()))
#A = [int(input()) for i in range(N)]
#C = [list(map(int, input().split())) for i in range()]
R  =[]
G  =[]
B  =[]

START_TIME = time.time()
for i, s in enumerate(S):
    if s == 'R':
        R.append(i)
    elif s == 'G':
        G.append(i)
    else:
        B.append(i)
answer = len(R)*len(G)*len(B)
hiku = 0
for r in R:
    for g in G:
        for b in B:
            if b > max(r,g)+abs(r-g):
                break
            if abs(r-g) == abs(g-b) or abs(r-b) == abs(b-g) or abs(b-r) == abs(r-g):
                hiku += 1
answer -= hiku
print(answer)

Submission status: Runtime Error

Solution code:
import itertools
N = int(input())

d = {}
for i in range(N):
  d[i] = S[i]

l = list(itertools.combinations(d, 3))
ans = 0
for x in l:
  if d[x[0]] == d[x[1]] or d[x[0]] == d[x[2]] or d[x[1]] == d[x[2]]:
    continue

  if (x[1] - x[0] != x[2] - x[1]):
    ans += 1
    
print(ans)

Submission status: Runtime Error

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

cnt=0

for i in range(n-2):
  for j in range(i+1,n-1):
    for k in range(j+1,n):

      if (j-i)!=(k-j):
        if s[i]!=s[j] and s[j]!=s[k] and s[i]!=s[k]:
        
        cnt+=1

print(cnt)

Submission status: Runtime Error

Solution code:
import numpy as np
import itertools

n = int(input())
s = np.array(list(input()))

a = np.where(s == 'R')[0]
b = np.where(s == 'G')[0]
c = np.where(s == 'B')[0]

d = np.sort(np.array(list(itertools.product(a,b,c))), axis=1)
ans = np.count_nonzero(d[:,1] - d[:,0] != d[:,2] - d[:,1] )

print(ans)

Submission status: Runtime Error

Solution code:
def get_number(n,s):
    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)
    return r, g, b

def find_ijk(n,s,x,y,z):
    y.reverse()
    z.reverse()
    count = 0
    if n<=3:
        return count
    
    for i in x:
        a = s[i]
        
        for j in y:
            if j<=i:
                break
        else:
            b = s[j]
            
            for k in z:
                if k<=j:
                    break
                if j-i!=k-j:
                    c = s[k]
                    count+=1
                
    return count
                    

if __name__ == "__main__":
    n = int(input())
    s = input()
    r, g, b = get_number(n,s)
    #print(r,g,b)
    count = find_ijk(n,s,r,g,b)+find_ijk(n,s,r,b,g)+find_ijk(n,s,g,r,b)+\
    find_ijk(n,s,g,b,r)+find_ijk(n,s,b,r,g)+find_ijk(n,s,b,g,r)
    print(count)

Submission status: Runtime Error

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)

ans = 0
for n in range(3):
    for i in range(len(RGB[n])):
        j_start = 0
        while RGB[n][i] > RGB[(n + 1) % 3][j_start]:
            if j_start + 1 < len(RGB[(n + 1) % 3]):
                j_start += 1
            else:
                break
        for j in range(j_start, len(RGB[(n + 1) % 3])):
            k_start = 0
            while RGB[(n + 1) % 3][j] > RGB[(n + 2) % 3][k_start]:
                if k_start + 1 < len(RGB[(n + 2) % 3]):
                    k_start += 1
                else:
                    break
            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]:
                    ans += 1

        j_start = 0
        while RGB[n][i] > RGB[(n + 2) % 3][j_start]:
            if j_start + 1 < len(RGB[(n + 2) % 3]):
                j_start += 1
            else:
                break
        for j in range(len(RGB[(n + 2) % 3])):
            k_start = 0
            while RGB[(n + 2) % 3][j] > RGB[(n + 1) % 3][k_start]:
                if k_start + 1 < len(RGB[(n + 1) % 3]):
                    k_start += 1
                else:
                    break
            for k in range(len(RGB[(n + 1) % 3])):
                if RGB[n][i] < RGB[(n + 2) % 3][j] < RGB[(n + 1) % 3][k] and RGB[(n + 2) % 3][j] - RGB[n][i] != RGB[(n + 1) % 3][k] - RGB[(n + 2) % 3][j]:
                    ans += 1

print(ans)

Submission status: Runtime Error

Solution code:
def main():
    N = int(input())
    S = list(str(input()))
    print()
    sum = 0
    for i in range(0, N-2):
        si = S[i]
        for j in range(i, N-1):
            sj = S[j]
            for k in range(j, N):
                sk = S[k]
                #if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                if si != sj and sj != sk and sj != sk
                    if j - i != k -j:
                        sum += 1
    print(sum)



main()

Submission status: Runtime Error

Solution code:
import itertools
N = int(input())

d = {}
for i in range(N):
  d[i] = S[i]

l = list(itertools.combinations(d, 3))
ans = 0
for x in l:
  if d[x[0]] == d[x[1]] or d[x[0]] == d[x[2]] or d[x[1]] == d[x[2]]:
    continue

  if (x[1] - x[0] != x[2] - x[1]):
    ans += 1

Submission status: Runtime Error

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

posR = []
posG = []
posB = []

ctR = 0
ctG = 0
ctB = 0

for i in range(N):
    if S[i]=="R":
        posR.append(i)
        ctR += 1
    if S[i]=="G":
        posG.append(i)
        ctG += 1
    if S[i]=="B":
        posB.append(i)
        ctB += 1


def upper_bound(T,SSS):#スライスの値を返す。同じ値があったら右寄りに出す
    left = 0 #SSS[left]はTより真に小さい
    right = len(SSS)-1 #SSS[right]はTと同じかそれ以上

    if T<SSS[left]:
        return 0
    if T>=SSS[right]:
        return right+1

    while True:
        mid = (left+right)//2
        if right-left==1:
            return right
        if SSS[mid]<=T:
            left = mid
        else:
            right = mid


def calc(pos1,pos2,pos3):
    ct = 0
    for i in pos1:
        buff = upper_bound(i,pos2)
        for j in pos2[buff:]:#大丈夫？
            buff2 = upper_bound(j,pos3)
            ct += len(pos3)-buff2
            #print(i,j,len(pos3)-buff2)
            if 2*j-i in pos3:
                ct -= 1
    return ct

ans = 0
ans += calc(posR,posG,posB)
ans += calc(posR,posB,posG)
ans += calc(posG,posR,posB)
ans += calc(posG,posB,posR)
ans += calc(posB,posR,posG)
ans += calc(posB,posG,posR)
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:

def loop():
  	n = int(input())
	s = input()
    ans = 0
    for i in range(n):
        for j in range(i+1, n):
            if s[i] != s[j]:
                a = s[i]
                b = s[j]
                for k in range(j+1, n):
                    if a != s[k] and b != s[k] and j != (k + i)/2:
                        ans +=1

    print(ans)

loop()

Submission status: Runtime Error

Solution code:
import numpy as np

n = int(input())
s = np.array(list(input()))

a = np.where(s == 'R')[0]
b = np.where(s == 'G')[0]
c = np.where(s == 'B')[0]

d = np.sort(np.array(list(itertools.product(a,b,c))), axis=1)
ans = np.count_nonzero(d[:,1] - d[:,0] != d[:,2] - d[:,1] )

print(ans)

Submission status: Runtime Error

Solution code:
from itertools import product as pro

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

r = [i for i in range(n) if s[i] == 'R']
g = [i for i in range(n) if s[i] == 'G']
b = [i for i in range(n) if s[i] == 'B']
cnt = len(r)*len(g)*len(b)

len = max(len(r), len(g), len(b))
if len == len(r):
    max1 = set(r)
    min1 = g
    min2 = b

if len == len(g):
    max1 = set(g)
    min1 = r
    min2 = b

if len == len(b):
    max1 = set(b)
    min1 = g
    min2 = r

for i, j in pro(min1, min2):
    if 2*j-i in max1:
        cnt -= 1
    if 2*i-j in max1:
        cnt -= 1
    if (i+j)/2 in max1:
        cnt -= 1

print(cnt)

Submission status: Runtime Error

Solution code:
import math
import string
import collections
from collections import Counter
from collections import deque
import sys
sys.setrecursionlimit(2*10**5)
INF = 2**60


def readints():
    return list(map(int, input().split()))


def nCr(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))


def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen)


def divisor(n):
    divisor = []
    for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
    return divisor


# coordinates
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]


N = int(input())
S = input()
r = []
g = []
b = []
for i in range(N):
    if S[i] == 'R':
        r.append(i+1)
    if S[i] == 'G':
        g.append(i+1)
    if S[i] == 'B':
        b.append(i+1)
print(r)
print(g)
print(b)
c = [None]*100
for i in range(100):
    c[i] = [None]*100
cnt = 0
for i in range(len(r)):
    for j in range(len(g)):
        for k in range(len(b)):
            c[i][0] = r[i]
            c[i][1] = g[j]
            c[i][2] = b[k]

            if c[i][1]-c[i][0] != c[i][2]-c[i][1]:
                #print(c[i][0], c[i][1], c[i][2], 'Yes')
                cnt += 1

print(cnt)

Submission status: Runtime Error

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


r = [i+1 for i in range(len(s)) if s[i]=='R']
g = [i+1 for i in range(len(s)) if s[i]=='G']
b = [i+1 for i in range(len(s)) if s[i]=='B']

from itertools import product

cand1 = list(product(r, g))
cand2 = list(product(cand1, b))
ans = 0
for x in cand2:
   i, j, k = x[0][0], x[0][1], x[1]
   #print(i, j , k)
   c = set(s[i-1] + s[j-1] + s[k-1])
   if len(c) == 3 and j-i!=k-j:
      ans += 1

print(ans)

Submission status: Runtime Error

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([i,j,k])
      med=statistics.median([i,j,k])
      if mean==med:
        continue
      count+=1
print(count)

Submission status: Runtime Error

Solution code:
N = int(data[0])
S = list(data[1])
answer = 0
for i in range(N-2):
  I = i+1
  for j in range(N-1):
    J = j+1
    if J <= I:
      continue
    for k in range(N):
      K = k+1
      if K <= J:
        continue
      if J-I == K-J:
        continue
      if S[i] == S[j]:
        continue
      if S[i] == S[k]:
        continue
      if S[j] == S[k]:
        continue
      answer += 1
print(answer)

Submission status: Runtime Error

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

print(sum)

Submission status: Runtime Error

Solution code:
n=int(input())
s=input()
cnt=0
a=["R","G","B"]
for i in range(n):
    x=a.remove(s[i])
    for j in range(i+1,n):
        if s[i]!=s[j]:
            qw=s
            x.remove(s[j])
            k=j-1
            if j+k<n:
               qw[j+k]="P"
            q=x.pop(0)
            cnt+=qw.count(q)
print(cnt)

Submission status: Runtime Error

Solution code:
import numpy as np
import sys
import math

#n = map(int,input().split())

data = [2246640
,2332104
,2379085
,2434833
,2490534
,2554600
,2609625
,2693919
,2742052
,2813988
,2875245
,2952085
,3003306
,3096024
,3157249
,3224511
,3306240
,3388576
,3444609
,3533637
,3591322
,3693924
,3767085
,3842623
,3912324
,4027884
,4102093
,4181949
,4270422
,4361548
,4427853
,4548003
,4616104
,4718640
,4812789
,4918561
,5003286
,5131848
,5205481
,5299011
,5392008
,5521384
,5610705
,5739009
,5818390
,5930196
,6052893
,6156139
,6239472
,6402720
,6493681
,6623853
,6741078
,6864016
,6953457
,7094451
,7215016
,7359936
,7475145
,7593865
,7689630
,7886244
,7984165
,8130747
,8253888
,8403448
,8523897
,8684853
,8802826
,8949612
,9105537
,9267595
,9376656
,9574704
,9686065
,9827097
,9997134
,10174780
,10290813
,10493367
,10611772
,10813692]


k = int(input())

if k >= 119:
    print(data[k-119])
    sys.exit()


def gcd(a,b):
    if a > b:
        a , b = b , a

    # q = b / a
    r = b % a

    if r == 0:
        return a
    else:
        return gcd(a,r)
    

sum = 0



for i in range(k):
    for j in range(k):
        for l in range(k):
            sum += gcd(i+1,gcd(j+1,l+1))


print(sum)

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
R, G, B = [],[],[]
for i,s in enumerate(S):
    if s=='R':
        R.append(i)
    elif s=='G':
        G.append(i)
    else:
        B.append(i)

R.sort()
G.sort()
B.sort()

from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort
 
ans = len(R)*len(G)*len(B)
for r in R:
    for g in G:
        for p in [r*2-g,g*2-r,(r+g)/2]:
            if p<=B[-1] and p>=B[0]:
                ind = bisect_left(B,p)
                if p == B[ind]:
                    ans -= 1

print(ans)

Submission status: Runtime Error

Solution code:
import collections
n = int(input())
s = list(input())
a = collections.Counter(s)
a = list(a.items())
ans = 0
# ar = []
# for i in range(1,2000):
#     c_ar = []
#     for j in range(1,n+1,i):
#         c_ar.append(j)
#     if len(c_ar) >= 3:
#         ar.append(c_ar)
# print(ar)
# print(len(ar))
# for i in range(len(ar)):
#     print(ar[i])
#     print(len(ar[i]))
#     for j in range(len(ar[i])-2):
#         print(s[ar[i][j]-1],s[ar[i][j+1]-1],s[ar[i][j+2]-1])
#         print(ar[i][j]-1, ar[i][j+1]-1, ar[i][j+2]-1)
#         if s[ar[i][j]-1] != s[ar[i][j+1]-1] and s[ar[i][j+1]-1] != s[ar[i][j+2]-1] and s[ar[i][j+2]-1] != s[ar[i][j]-1]:
#             cnt += 1
#             print("Y")
#     print(cnt)
# print(a[0][1],a[1][1],a[2][1])
# print(a[0][1]*a[1][1]*a[2][1]-cnt)
if n == 1 or n == 2 or n == 3:
    print(0)
    exit()
for i in range(1,n//2+3):
    cnt = 0
    #print(i)
    for j in range(n):
        if cnt + 2*i > n-1:
            break
        x,y,z = s[cnt], s[cnt+i], s[cnt+2*i]
        #print(cnt, cnt+i, cnt+i+i)
        if x != y and y != z and z != x:
            ans += 1
        cnt += 1
print(a[0][1]*a[1][1]*a[2][1]-ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import collections
n = int(input())
s = list(input())
a = collections.Counter(s)
a = list(a.items())
ans = 0
# ar = []
# for i in range(1,2000):
#     c_ar = []
#     for j in range(1,n+1,i):
#         c_ar.append(j)
#     if len(c_ar) >= 3:
#         ar.append(c_ar)
# print(ar)
# print(len(ar))
# for i in range(len(ar)):
#     print(ar[i])
#     print(len(ar[i]))
#     for j in range(len(ar[i])-2):
#         print(s[ar[i][j]-1],s[ar[i][j+1]-1],s[ar[i][j+2]-1])
#         print(ar[i][j]-1, ar[i][j+1]-1, ar[i][j+2]-1)
#         if s[ar[i][j]-1] != s[ar[i][j+1]-1] and s[ar[i][j+1]-1] != s[ar[i][j+2]-1] and s[ar[i][j+2]-1] != s[ar[i][j]-1]:
#             cnt += 1
#             print("Y")
#     print(cnt)
# print(a[0][1],a[1][1],a[2][1])
# print(a[0][1]*a[1][1]*a[2][1]-cnt)

for i in range(1,n//2+3):
    cnt = 0
    #print(i)
    for j in range(n):
        if cnt + 2*i > n-1:
            break
        x,y,z = s[cnt], s[cnt+i], s[cnt+2*i]
        #print(cnt, cnt+i, cnt+i+i)
        if x != y and y != z and z != x:
            ans += 1
        cnt += 1
print(a[0][1]*a[1][1]*a[2][1]-ans)

Submission status: Runtime Error

Solution code:
import sys
from pprint import pprint

from collections import Counter
from math import factorial

def nCr(n, r):
    return factorial(n) // factorial(r) // factorial(n - r)

N = int(sys.stdin.readline())
S = sys.stdin.readline().strip()
# print(len(S))

counter = Counter(S)
rgb_count = 0

max_width = len(S) // 2
for width in range(1, max_width+1):
    max_left = len(S) - 2 * width
    # print(width, max_left)
    for i in range(max_left):
        if len(set([S[i], S[i+width], S[i+2*width]])) == 3:
            rgb_count += 1


all_count = nCr(counter["R"], 1) * nCr(counter["G"], 1) * nCr(counter["B"], 1) 
# print(all_count, rgb_count)
print(all_count - rgb_count)

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
r = []
g = []
b = []
for i, item in enumerate(s):
    if item=="R":
        r.append(i)
    elif item=="G":
        g.append(i)
    else:
        b.append(i)

count = 0
for low,mid,high in [(r,g,b), (r,b,g), (g,r,b), (g,b,r), (b,r,g), (b,g,r)]:
    for m in mid:
        ind = len(high) - 1
        for l in low:
            if l >= m:
                continue
            while (l+high[ind] > m*2) and (ind>0):
                ind -= 1
            if m < high[ind]:
                if l+high[ind] == m*2:
                    count += 1

#                 print(l, m, high[ind])
print(len(r)*len(g)*len(b) - count)

Submission status: Runtime Error

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

d = {}
for i in range(N):
  d[i] = S[i]

l = list(itertools.combinations(d, 3))
ans = 0
for x in l:
  if (x[1] - x[0] == x[2] - x[1])
      continue
    if d[x[0]] != d[x[1]] and d[x[0]] != d[x[2]] and d[x[1]] != d[x[2]]:
        ans += 1

print(ans)

Submission status: Runtime Error

Solution code:
if __name__ == '__main__':
    N = int(input())
    S = input()
    total = 0
    count_r = 0
    count_g = 0
    count_b = 0
    for i in range(N):
        if ch == 'R':
            count_r += 1
        elif ch == 'G':
            count_g += 1
        else:
            count_b += 1
        for j in range(i + 1, N):
            if S[i] != S[j]:
                r, g = S[i], S[j]
                other = 'B'
                if 'G' != r and 'G' != g:
                    other = 'G'
                if 'R' != r and 'R' != g:
                    other = 'R'
                k = j - i + j
                if k < N and S[k] == other:
                    total -= 1
    print(total + count_r*count_b*count_g)

Submission status: Runtime Error

Solution code:
total = 0
for i in range(0,n-2):
    for j in range(i+1,n-1):
        for k in range(j+1,n):
            if s[i] != s[j] and  s[i] != s[k] and s[j] != s[k]:
                if j!= (k+i)/2:
                    # if j<3:
                        # print(i,j,k)
                    total += 1
print(total)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import collections
n = int(input())
s = list(input())
a = collections.Counter(s)
a = list(a.items())
ans = 0
ar = []
# for i in range(1,2000):
#     c_ar = []
#     for j in range(1,n+1,i):
#         c_ar.append(j)
#     if len(c_ar) >= 3:
#         ar.append(c_ar)
# print(ar)
# print(len(ar))
# for i in range(len(ar)):
#     print(ar[i])
#     print(len(ar[i]))
#     for j in range(len(ar[i])-2):
#         print(s[ar[i][j]-1],s[ar[i][j+1]-1],s[ar[i][j+2]-1])
#         print(ar[i][j]-1, ar[i][j+1]-1, ar[i][j+2]-1)
#         if s[ar[i][j]-1] != s[ar[i][j+1]-1] and s[ar[i][j+1]-1] != s[ar[i][j+2]-1] and s[ar[i][j+2]-1] != s[ar[i][j]-1]:
#             cnt += 1
#             print("Y")
#     print(cnt)
# print(a[0][1],a[1][1],a[2][1])
# print(a[0][1]*a[1][1]*a[2][1]-cnt)

for i in range(1,2000):
    cnt = 0
    for j in range(n):
        if cnt + 2*i > n-1:
            break
        x,y,z = s[cnt], s[cnt+i], s[cnt+2*i]
        if x != y and y != z and z != x:
            ans += 1
        cnt += 1
print(a[0][1]*a[1][1]*a[2][1]-ans)

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
R = [i+1 for i in range(N) if S[i] == 'R']
G = [i+1 for i in range(N) if S[i] == 'G']
B = [i+1 for i in range(N) if S[i] == 'B']

len_r = len(R)
len_g = len(G)
len_b = len(B)

if len_r > len_g and len_r > len_b:
    X = G
    Y = B
    Z = R
elif len_g > len_r and len_g > len_b:
    X = R
    Y = B
    Z = G
elif len_b > len_g and len_b > len_r:
    X = G
    Y = R
    Z = B

ans = 0

for r in X:
    for g in Y:
        tmp = 0
        if r < g:
            a = r
            b = g
        else:
            a = g
            b = r
        i = b-a
        if a-i in Z:
            tmp+=1
        if b+i in Z:
            tmp+=1
        if a+(i/2) == b -(i/2) and a+(i/2) in Z:
            tmp += 1
        ans += len(Z) - tmp
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
R, G, B = [],[],[]
for i,s in enumerate(S):
    if s=='R':
        R.append(i)
    elif s=='G':
        G.append(i)
    else:
        B.append(i)

R.sort()
G.sort()
B.sort()

from fractions import gcd
from math import ceil
from collections import Counter, deque, defaultdict
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge
from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort
from itertools import accumulate, product, permutations, combinations
 
ans = len(R)*len(G)*len(B)
for r in R:
    for g in G:
        for p in [r*2-g,g*2-r,(r+g)/2]:
            if p<=B[-1] and p>=B[0]:
                ind = bisect_left(B,p)
                if p == B[ind]:
                    ans -= 1

print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
s = "F"+s

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

Submission status: Runtime Error

Solution code:
# from pprint import pprint
import sys
from itertools import combinations

N = int(input())
S = sys.stdin.readline().rstrip()

# N = 39
# S = 'RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB'
count = 0

for i,j,k in itertools.combinations(range(1, N + 1), 3):
    if all([S[i-1] != S[j-1], S[i-1] != S[k-1], S[j-1] != S[k-1]]):
        if j - i != k - j:
            count += 1

print(count)

Submission status: Runtime Error

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

A=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 (j-i) != (k-j):
                if (S[j]!=S[k])and(S[k]!=S[i]):
                    A+=1
print(A)

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
RGB = ['R', 'G', 'B']
import collections
dct = dict(collections.Counter(S))

cnt = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        if 2 * j - i < N:
            for c in RGB:
                if c != S[i] and c != S[j] and S[i] != S[j]:
                    if c == S[2 * j - i]:
                        cnt += 1
                        #print(i, j)
#print(cnt)
print(dct['R'] * dct['G'] * dct['B'] - cnt)

Submission status: Runtime Error

Solution code:
import numpy


def main():
    n = int(input())
    s = [ord(l) for l in input()]
    r = 0

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


main()

Submission status: Runtime Error

Solution code:
import numpy


def main():
    n = int(input())
    s = input()
    r = 0
    m = {
        "R": 1,
        "G": 2,
        "B": 3
    }
    s = [m[l] for l in s]
    for k in range(n):
        for j in range(k):
            for i in range(j):
                if j - i != k - j and s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
                    r += 1
    print(r)


main()

Submission status: Runtime Error

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

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

        RGB = ['R','G','B']
            RGB.remove(S[j])
            RGB.remove(S[i])
            sum += S[(j+1):].count(RGB[0])

            # S[k]
            k = 2 * j - i
            if k < len(S):
                if S[k] == RGB[0]:
                    sum -= 1
print(sum)

Submission status: Runtime Error

Solution code:
n=int(input())
s=input()
from collections import Counter
l=Counter()
for j,i in enumerate(s):
    if l[i]==0:
        l[i]=[]
    l[i].append(j)
ans=0
import bisect
import itertools
ll=Counter()
ll["R"]=len(l["R"])
ll["G"]=len(l["G"])
ll["B"]=len(l["B"])
for i,j in itertools.combinations(range(n),2):
    if s[i]!=s[j]:
        c="RGB".replace(s[i],"").replace(s[j],"")
        idx=bisect.bisect_left(l[c],j)
        ans+=ll[c]-idx
        if 2*j-i<n:
            if s[2*j-i]==c:
                ans-=1
print(ans)

Submission status: Runtime Error

Solution code:
from collections import defaultdict

N = int(input())
S = input()
cnt = 0
a = defaultdict(int)

for d in range((N-3)//2+1):
    left = 0
    middle = d+1
    right = 2*(d+1)
    while right < N:
        if d ==0:
            a[S[left]] += 1
        if S[left] != S[middle] and S[left] != S[right] and S[middle] != S[right]:
            cnt += 1
        left += 1
        middle += 1 
        right += 1
a[S[N-2]] += 1
a[S[N-1]] += 1
print(reduce(lambda x,y: x*y,a.values()) - cnt)

Submission status: Runtime Error

Solution code:
n = 39
s = str(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)
cnt = 0
for i in r:
    for j in g:
        if (i+j)%2==0:
            if (i + j) // 2 in b:
                cnt += 1
        if (2 * j) - i in b:
            cnt += 1
        if (2 * i) - j in b:
            cnt += 1
print((len(r) * len(g) * len(b))-cnt)