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:
def main():
    N = int(input())
    S = input()
    R = set()
    G = set()
    B = set()
    for i,s in enumerate():
        if s == "R":
            R.add(i)
        elif s == "B":
            B.add(i)
        else:
            G.add(i)
    ans = 0
    for r in R:
        for b in B:
            for g in G:
                k = max(r, b, g)
                i = min(r, b, g)
                j = r + b + g - k - i
                if j - i != k - j:
                    ans += 1
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

c = Counter(list(s))
ans = 1
for i in list(c.values):
    ans *= i
for i in range(n-2):
    for j in range(i+1, n-1):
        k = j + (j-i)
        if k >= n:
            break
        if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
            ans -= 1
print(ans)

Submission status: Runtime Error

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

I = []
J = []
K = []

for n in range(1,N+1):
  if S[n-1] == "R":
    I.append(n)
  elif S[n-1] == "G":
    J.append(n)
  else:
    K.append(n)
    
cnt = 0
for a in I:
  for b in J:
    for c in K:
      ls = [a,b,c]
      ls.sort()a
      if ls[1] - ls[0] != ls[2] - ls[1]:
        cnt += 1
    
print(cnt)

Submission status: Runtime Error

Solution code:
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf,comb
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
from functools import reduce
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n = INT()
s = input()

r = s.count('R')
g = s.count('G')
b = n - r - g
ans = comb(n, 3) - ( comb(r, 2) + comb(g, 2) + comb(b, 2) ) * (n-2) + ( comb(r, 3) + comb(g, 3) + comb(b, 3) ) * 2
for i in range(n):
    d = 1
    j = i + d
    k = j + d
    while k < n:
        t = s[i] + s[j] + s[k]
        if t.count('R') == t.count('G') == 1:
            ans -= 1
        d += 1
        j += 1
        k += 2
print(ans)

Submission status: Runtime Error

Solution code:
def solve():
    N = int(input())
    S = input()
    cnt = {}
    for c in S:
        cnt[c] = cnt.get(c, 0) + 1
    total_triplets = cnt['R'] * cnt['G'] * cnt['B']
    for stride in range(1, N):
        for i in range(0, N-2*stride):
            j = i + stride
            k = j + stride
            if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
                total_triplets -= 1
    print(total_triplets)

if __name__ == "__main__":
    solve()

Submission status: Runtime Error

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

num = len(s)
ans = 0
for i in range(num):
    a = set(s[i])
    for j in range(i+1, num):
        if s[j] in a:
            continue
        a.add(s[j])
        for k in range(j+1, num):
            if s[k] in a or j-i == k-j:
                continue
            ans += 1
        a.remove()

print(ans)

Submission status: Runtime Error

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

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

def str_finditer(S,s):
    n = 0
    while True:
        n = S.find(s,n)
        if n == -1:
            break
        yield n
        n += 1

R = list(str_finditer(S, 'R'))
G = list(str_finditer(S, 'G'))
B = list(str_finditer(S, 'B'))

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

for i in R:
    for j in G:
        for k in B:
            L = sorted((i,j,k))
            if L[2] == L[1] + L[1] - L[0]:
                ans -= 1
print(ans)

Submission status: Runtime Error

Solution code:
import sys
from collections import Counter
input = sys.stdin.readline
n = int(input())
s = list(input())

c  = list(Counter(s).values())  

num = c[0]*c[1]*c[2]

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

Submission status: Runtime Error

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

# N=4000
# S=[random.choice(['R','G','B']) for _ in range(N)]

S_dict={'R':set(),'G':set(),'B':set()}
for s_idx,s in enumerate(S):
    S_dict[s].add(s_idx+1)
    
# print(S_dict)
B_ = [0]*(2*N)
for s_b in S_dict['B']:
    B_[s_b]=1
    
S_R=S_dict['R']
S_G=S_dict['G']
    
ans=0
ans__=len(S_dict['B'])
for s_r in S_R:
    for s_g in S_G:
        ans_=ans__
        c_= abs(s_r-s_g)
        c_mean = 0
        if (s_r + s_g)%2 == 0:
            c_mean = (s_r + s_g)//2
            ans_ = get_ans(ans_,c_mean)
            
#         print('=====')
#         print(s_r,s_g,c_,c_mean)
        
        if s_r > s_g:
            ans_ = get_ans(ans_,s_r + c_)
            ans_ = get_ans(ans_,s_g - c_)
            
#             print(s_r + c_,s_g - c_)
            
        else:
            ans_=get_ans(ans_,s_r - c_)
            ans_=get_ans(ans_,s_g + c_)
            
#             print(s_r - c_,s_g + c_)
        
        ans+=ans_
        
#         print(ans_,ans)
        
print(ans)

Submission status: Runtime Error

Solution code:
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def s(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

n = k()
s = s()
r = s.count("R")
g = s.count("G")
b = s.count("B")
ans = r*g*b
for i in range(n-2):
    for j in range((n-i+1)//2):
        if s[i]!=s[i+j] and s[i+j]!=s[i+2*j] and s[i+2*j]!=s[i]:
            a-=1

print(a)

Submission status: Runtime Error

Solution code:
from collections import deque
s = input()
Q = int(input())

SS =deque(list(s))
rev_count = 0

for i in range(Q):
    que = input()
    if que[0] == '1':
        rev_count +=1
    elif que[0] == '2':
        if (rev_count + int(que[2]))%2 == 0:
            SS.append(que[4])
        else:
            SS.appendleft(que[4])

print("".join(SS) if rev_count %2 == 0 else ("".join(SS))[-1::-1])

Submission status: Runtime Error

Solution code:
from collections import Counter

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

a, b, c = Counter(S).values()
r = a*b*c

for i in range(N):
    for j in range(i+1, N):
        k = 2*j-i
        # print(i, j, k)
        if k < N and len({S[i], S[j], S[k]}) == 3:
            r -= 1

print(r)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

alphabet_dict = {}
for i in range(1, N+1):
    alphabet = S[i-1]
    if alphabet in  alphabet_dict.keys():
        alphabet_dict[alphabet].append(i)
    else:
        alphabet_dict[alphabet] = [i]

result = 0
for r in alphabet_dict["R"]:
    for g in alphabet_dict["G"]:
        for b in alphabet_dict["B"]:
            i = min([r,g,b])
            k = max([r,g,b])
            j =set([r,g,b])-set([i,k])
            j = list(j)[0]
            if j-i!=k-j:
                result+=1
print(result)

Submission status: Runtime Error

Solution code:
from itertools import groupby
from statistics import median
from time import time
from sys import exit
import numpy as np

N = int(input())

line = ([(index, v) for index, v in enumerate(input())])


def types(line): return line[1]
def index(line): return line[0]


gp = groupby(sorted(line, key=types), types)

d = {}

for k, v in gp:
    indexes = np.array([i for i, value in v])
    d[k] = indexes

if 'R' not in d or 'G' not in d or 'B' not in d:
    print(0)
    exit()

delete = 0


for differ in range(1, N):
    for start in range(N):
        if start + differ*2 > N - 1:
            break

        i = line[start]
        j = line[start + differ]
        k = line[start + differ*2]

        if len({i[1], j[1], k[1]}) != 3:
            continue

        upper = max(i[0], j[0], k[0])
        lower = min(i[0], j[0], k[0])
        median_num = median([i[0], j[0], k[0]])
        if upper - median_num == median_num - lower:
            delete += 1

print(d['R'].size * d['G'].size * d['B'].size - delete)

Submission status: Runtime Error

Solution code:
from itertools import product, groupby
from statistics import median
from time import time
from sys import exit
import numpy as np

N = int(input())

line = ([(index, v) for index, v in enumerate(input())])


def types(line): return line[1]
def index(line): return line[0]


gp = groupby(sorted(line, key=types), types)

d = {}

for k, v in gp:
    indexes = np.array([i for i, value in v])
    d[k] = indexes


delete = 0

for differ in range(1, N):
    for start in range(N):
        if start + differ*2 > N - 1:
            break
        i = line[start]
        j = line[start + differ]
        k = line[start + differ*2]

        if len({i[1], j[1], k[1]}) == 3:
            upper = max(i[0], j[0], k[0])
            lower = min(i[0], j[0], k[0])
            median_num = median([i[0], j[0], k[0]])
            if upper - median_num == median_num - lower:
                delete += 1

print(d['R'].size * d['G'].size * d['B'].size - delete)

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
count_R = 0
count_G = 0
count_B = 0
count = 0
for i in range(N):
    if S[i] == 'R':
        count_R = count_R+1
    elif S[i] == 'G':
        count_G = count_G+1
    else:
        count_B = count_B+1
count = count_R*count_G*count_B

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

Submission status: Runtime Error

Solution code:
import itertools
from collections import Counter

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

C=Counter(S)
r = C['R']
g = C['G']
b = C['B']


cnt = r * g * b

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

print(cnt)

Submission status: Runtime Error

Solution code:
import itertools
from collections import Counter

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

C=Counter(S)
r = C['R']
g = C['G']
b = C['B']


cnt = r * g * b

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

print(cnt)

Submission status: Runtime Error

Solution code:
def main():
    n = int(input())
    s_list = list(input())
    
    count = 0
    for i in range(len(s_list)-2):
        j  = i + 1
        while s_list[i] == s_list[j]:
            j += 1
            k = j + 1
            while s_list[i] == s_list[k] or s_list[j] == s_list[k]:
                k += 1
            if j-i != k-j:
                count += 1
    
    print(count)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

count = 0

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

print(count)

Submission status: Runtime Error

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

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

ans = r * g * b

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

print(ans)

Submission status: Runtime Error

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

rcount=0
gcount=0
bcount=0
for i in range(n):
    if s[i]=="R":
        rcount+=1
    elif s[i]=="G":
        gcount+=1
    else:
        bcount+=1

al=rcount*bcount*gcount

for i in range(n-2):
    for k in range(i+1,n):
        dis=k-i
        l=k+dis
        if k+dis>n:
            continue
        if s[i]!=s[k] and s[i]!=s[l] and s[k]!=s[l]:
            al-=1

print(al)

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
r = []
g = []
b = [0]*4000
for i,j in enumerate(s):
  if j == 'R':
    r.append(i)
  if j == 'G':
    g.append(i)
  if j == 'B':
    b[i] += 1
cnt = len(r)*len(g)*sum(b)
for i in r:
  for j in g:
    if b[2*j-i] == 1 :
      cnt -= 1
    if b[2*i-j] == 1 :
      cnt -= 1
    if (i+j)%2 == 0 and b[(i+j)//2] == 1:
      cnt -= 1
print(cnt)

Submission status: Runtime Error

Solution code:
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main(){
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> a(n);
  int rn=0,gn=0,bn=0;
  rep(i,n){
    if(s[i]=='R'){
      a[i]=0;
      rn++;
    }else if(s[i]=='G'){
      a[i]=2;
      gn++;
    }else{      
      a[i]=3;
      bn++;
    }
  }
  
  int miss=0;
  rep(i,n){
    for(int j=i+1;2*j-i<n;j++){
      if(a[i]+a[j]+a[2*j-i]==5){
        miss++;
      }
    }
  }
  
  cout << rn*gn*bn-miss;
  return 0;
}

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)
    elif s[i] == "G":
        G.append(i)
    else:
        B.append(i)

ans = len(R)*len(G)*len(B)
for i in range(n-2):
    for j in range(i+1,n-1):
        k  = 2*j-i
        if k<n:
            if (s[i]=s[j]) and (s[i]=s[k]) and (s[j]=s[k]):
                ans-=1
print(ans)

Submission status: Runtime Error

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

for i in range(n):
  if s[i] == 'R':
    s[i] = 1
  elif s[i] == 'G':
    s[i]
  elif s[i] == 'B':
    s[i] = 4
  
for i in range(n):
  for j in range(i+1, n):
    k = 2*j - i
    if k >= n:
      continue
    if s[i]+s[j]+s[k]==7:
      ans-=1

print(ans)

Submission status: Runtime Error

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

for i in range(n):
  if s[i] == 'R':
    s[i] = 1
  elif s[i] == 'G':
    s[i]
  elif s[i] = 4
  
for i in range(n):
  for j in range(i+1, n):
    k = 2*j - i
    if k >= n:
      continue
    if s[i]+s[j]+s[k]==7:
      ans-=1

print(ans)

Submission status: Runtime Error

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

r = s.count("R")
g = s.count("G")
b = s.count("B")
ans = r*g*b

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

Submission status: Runtime Error

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

ruisekiR = []*(n+1)
ruisekiG = []*(n+1)
ruisekiB = []*(n+1)

for i, c in enumerate(s):
  ruisekiR[i+1] = ruisekiR[i] + (1 if c == 'R' else 0)
  ruisekiG[i+1] = ruisekiG[i] + (1 if c == 'G' else 0)
  ruisekiB[i+1] = ruisekiB[i] + (1 if c == 'B' else 0)
  
for i in range(n-2):
  for j in range(i+1,n-1):
    if s[i] == s[j]:
      continue
    c = 'R'
    if 'R' in (s[i],s[j]):
      c = 'G'
    if 'G' in (s[i],s[j]):
      c = 'B'
    
    if c == 'R':
      ans += ruisekiR[n]-ruisekiR[j]
    if c == 'G':
      ans += ruisekiG[n]-ruisekiG[j]
    if c == 'B':
      ans += ruisekiB[n]-ruisekiB[j]
    
    if j+(j-i) < n and s[j+(j-i)] == c:
      ans -= 1
      
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
from bisect import bisect

n=int(input())
dic={ 'R':[], 'G':[], 'B':[] }

stri=list(input())
count=0
for i,v in enumerate(stri):
  #print(i,v)
  dic[v].append(i)
  
for name in dic:
  dic[name]=np.asarray(dic[name])
#print(dic)
for i in range(n):
  lib=['R','G','B']
  lib.remove(stri[i])
  #print(lib)
  for col in lib:
    for j in dic[col][dic[col]>i]:
      t=[i for i in lib if i != col].pop()
      skip_k=2*i-j
      idx=bisect(dic[t],skip_k)-1
      if dic[t][idx]==skip_k:
        count+=len(dic[t][dic[t]>j])-1
      else:
        count+=len(dic[t][dic[t]>j])
      
print(count)

Submission status: Runtime Error

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

from collections import Counter
rgb = dict(Counter(s))
if len(dict) < 3:
    print('0')
else:
    p = rgb['R'] * rgb['G'] * rgb['B']
    for i in range(1, (len(s)-1)//2 + 1):
        for j in range(len(s) - 2*i):
            if {s[j], s[j + i], s[j + 2*i]} == {'R', 'G', 'B'}:
                p -= 1
    print(p)

Submission status: Runtime Error

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

from collections import Counter
rgb = dict(Counter(s))
p = rgb['R'] * rgb['G'] * rgb['B']
for i in range(1, (len(s)-1)//2 + 1):
    for j in range(len(s) - 2*i):
        if {s[j], s[j + i], s[j + 2*i]} == {'R', 'G', 'B'}:
            p -= 1
print(p)

Submission status: Runtime Error

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

from collections import Counter
rgb = dict(Counter(s))
p = rgb['R'] * rgb['G'] * rgb['B']
for i in range(1, len(s)//2):
    for j in range(len(s) - 2*i):
        if {s[j], s[j + i], s[j + 2*i]} == {'R', 'G', 'B'}:
            p -= 1
print(p)

Submission status: Runtime Error

Solution code:
from collections import Counter
n=int(input())
s_=input()
s=Couter(s_)
rgb = s['R'] * s['G'] * s['B']
cnt=0
for i in range(len(s_)):
  for j in range(i+1,len(s_)):
    h=j-i+j
    if h<n:
      if s_[i]!=s_[j] and s_[h]!=s_[j] and s_[i]!=s_[h]:
        cnt+=1

print(rgb-cnt)

Submission status: Runtime Error

Solution code:
import itertools


input()
s = list(input())
r =[]
g =[]
b =[]
n= len(s)
ans =0
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)

for ri , gi in itertools.product(r,g):
    rg=gi-a
    if rg+gi in b:
        ans -=1
    if  ri-rg in b:
        ans -=1
    if (gi+ri)/2 in b:
        ans -=1

ans += len(r)*len(b)*len(g)
print(ans)

Submission status: Runtime Error

Solution code:
input()

s = list(input())
r =[]
g =[]
b =[]
n= len(s)
ans =0
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)

for ss in itertools.product(r,g,b):
    sss =sorted(ss)
    if sss[2] + sss[0] != 2*sss[1]:
        ans +=1

print(ans)

Submission status: Runtime Error

Solution code:
import sys
from io import StringIO
import unittest


def resolve():
    n = int(input())

    # 1文字ずつチェックするのでSET。
    s = list(input())

    # 全パターンの数(RGB1つずつ選択する場合)
    ans = s.count("R")*s.count("G")*s.count("B")

    # ここから条件(j-i≠k-j)を満たさないパターンを減算。
    s_len = len(s)
    minus = 0

    # i<=距離A=>j<=距離A=>k という状態で、距離Aが等しいパターンを列挙していく。
    for i in range(s_len-2):
        first = s[i]
        for j in range(1, 9999):

            # 文字数を超えるパターンは検証しない
            if i+j+j > s_len - 1:
                break

            # 1文字目,2文字目,3文字目が全部別の場合、除外するパターンである。(合計数から減算する)
            if first is not s[i+j] and first is not s[i+j+j] and s[i+j] is not s[i+j+j]:
                minus += 1

    print(ans - minus)




    # ans = 0
    # for i in range(len(s) - 2):
    #
    #     target = list("RGB".replace(s[i], ""))
    #     ans += s[i:len(s)].count(target[0]) * s[i:len(s)].count(target[1])
    #
    # for i in range(len(s) - 2)
    #
    #
    #
    #     for j in range(i+1, len(s) - 1):
    #         if s[i] is s[j]:
    #             continue
    #         target = "RGB".replace(s[i], "").replace(s[j], "")
    #
    #         ans += s[j:len(s)].count(target)
    #
    #         if j+(j-i) < len(s) :
    #             if s[j+(j-i)] is target:
    #                 ans -= 1
    #
    # print(ans)


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """4
RRGB"""
        output = """1"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB"""
        output = """1800"""
        self.assertIO(input, output)

#     def test_入力例_3(self):
#         input = """4080
# RBRBGRBGG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)

if __name__ == "__main__":
    unittest.main()
	resolve()

Submission status: Runtime Error

Solution code:
n=int(input())
s=str(input())
t=len(s)
#setの方が参照速度が速い
r=set()
g=set()
b=set()
a=0
for i in range(n):
    if s[i]=='R':
        r.add(i)
    if s[i]=='G':
        g.add(i)
    if s[i]=='B':
        b.add(i)
d=len(r)*len(g)*len(b)
for ri in r:
    for gi in g:
        if 2ri-gi in b:
            d-=1
        if 2gi-ri in b:
            d-=1
        if (gi+ri)/2 in b:
            d-=1

print(d)

Submission status: Runtime Error

Solution code:
N = int(input())
s = input()
 
r_cnt = s.count('R')
g_cnt = s.count('G')
b_cnt = s.count('B')

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

print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
s = input()
 
r_cnt = s.count('R')
g_cnt = s.count('G')
b_cnt = s.count('B')

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

print(ans)

Submission status: Runtime Error

Solution code:
from collections import Counter

N = int(input())
S = input()
count = 0
for i in range(N):
	for j in range(i+1,N):
		try:
			if(len(set([S[i],S[j],S[2*j-i]])) == 3):
				count+=1
		except:
			pass
a = dict(Counter(list(S)))
print(a["R"]*a["B"]*a["G"]-count)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
from numba import njit
from sys import stdin
def S(): return stdin.readline().rstrip()
def I(): return int(stdin.readline().rstrip())
 
@njit
def main():
    n = I()
    s = list(S())
    ans = 0
    for i in range(n-2):
        color = {s[i]}
        for j in range(i+1,n-1):
            if not s[j] in color:
                color.add(s[j])
                for k in range(j+1,n):
                    if not s[k] in color:
                        if k-j != j-i:
                            ans += 1
                color = {s[i]}
    
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
_input = input
#'''
allinputs = iter(input().splitlines())
input = lambda : next(allinputs)
#'''

#ここに書きたいコードを書く
#データ読み込み

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

RGB_num=[0]*3

for c in S:
	if c=='R':
		RGB_num[0] += 1
	elif c=='G':
		RGB_num[1] += 1
	else:
		RGB_num[2] += 1

#print(RGB_num)
joken2=0

print(S[0])
for i in range(N-2):
	#print("i",i,S[i+1])
	for j in range(min(i+1,N-i-2)):
		if S[i+1] != S[i-j] and S[i+1] != S[i+j+2] and S[i-j] != S[i+j+2]:
			joken2 += 1
			#print(i,j,S[i-j]+S[i+1]+S[i+j+2],"j2")
		#else:
			#print(i,j,S[i-j]+S[i+1]+S[i+j+2])

joken1=RGB_num[0]*RGB_num[1]*RGB_num[2]

print(joken1-joken2)

input = _input

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
def S(): return stdin.readline().rstrip()
def I(): return int(stdin.readline().rstrip())


n = I()
s = S()

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

print(ans)

Submission status: Runtime Error

Solution code:
from numba import jit

N=int(input())
S=input()
R=0
G=0
B=0
for i in range(N):
    s=S[i]
    if s=="R":
        R+=1
    elif s=="G":
        G+=1
    else:
        B+=1
ans=R*G*B

@njit
def calc(ans):
    for i in range(N-2):
        for j in range(i+1, N-1):
            if 0<=j+j-i<N:
                si=S[i]
                sj=S[j]
                sk=S[2*j-i]
                if si!=sj and sj!=sk and sk!=si:
                    ans-=1
    return ans
print(calc(ans))

Submission status: Runtime Error

Solution code:
def main():
    from collections import Counter
    n, s = int(input()), input()
    c = Counter(s)
    d = list(c.values())

    ans = d[0] * d[1] * d[2]
    rgb = {"R", "G", "B"}
    for i in range(1, n // 2 + 1):
        for j in range(i):
            x = s[j::i]
            for a, b, c in zip(x, x[1:], x[2:]):
                g = {a, b, c}
                if g == rgb:
                    ans -= 1

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
#ans = s.count('R')*s.count('G')*s.count('B')
ans = 0
for i in range(n-2):
	s1 = s[i]
	for j in range(i+1,n-1):
    	s2 = s[j]
        if s1 == s2:
        	pass
        else:
        	s[2*j-i] != s1 and s[2*j-i] != s2:
            ans += 1
print(ans)