Problem:


Score : 400 points

Problem Statement

We have a string S of length N consisting of R, G, and B.

Find the number of triples (i,~j,~k)~(1 \leq i < j < k \leq N) that satisfy both of the following conditions:

S_i \neq S_j, S_i \neq S_k, and S_j \neq S_k.

j - i \neq k - j.

Constraints

1 \leq N \leq 4000

S is a string of length N consisting of R, G, and B.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of triplets in question.

Sample Input 1

4
RRGB

Sample Output 1

1

Only the triplet (1,~3,~4) satisfies both conditions. The triplet (2,~3,~4) satisfies the first condition but not the second, so it does not count.

Sample Input 2

39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB

Sample Output 2

1800



Sample input:
4
RRGB


Sample output:
1

Submission status: Accepted

Solution code:
import math
from math import gcd,pi
INF = float("inf")

import sys
sys.setrecursionlimit(10**6)
import itertools
from collections import Counter,deque
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]


def main():
    n = i_input()
    s = s_input()

    r_cn = s.count("R")
    g_cn = s.count("G")
    b_cn = s.count("B")

    ans = r_cn*g_cn*b_cn

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


if __name__=="__main__":
    main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from collections import Counter
import sys
input = sys.stdin.readline

def main():
    N = int(input())
    S = input()
    ans = 0
    r, g, b = [0 for _ in range(N+1)], [0 for _ in range(N+1)], [0 for _ in range(N+1)]
    for i in range(N):
        r[i+1], g[i+1], b[i+1] = r[i], g[i], b[i]
        if S[i] == "R":
            r[i+1] += 1
        elif S[i] == "G":
            g[i+1] += 1
        else:
            b[i+1] += 1
    
    for i in range(1, N-1):
        if S[i] == "R":
            ans += g[i] * (b[N] - b[i+1]) + b[i] * (g[N] - g[i+1])
            for j in range(1, min(i, N-i-1)+1):
                if {S[i-j], S[i+j]} == {"G", "B"}:
                    ans -= 1
        
        elif S[i] == "G":
            ans += r[i] * (b[N] - b[i+1]) + b[i] * (r[N] - r[i+1])
            for j in range(1, min(i, N-i-1)+1):
                if {S[i-j], S[i+j]} == {"R", "B"}:
                    ans -= 1
        elif S[i] == "B":
            ans += g[i] * (r[N] - r[i+1]) + r[i] * (g[N] - g[i+1])
            for j in range(1, min(i, N-i-1)+1):
                if {S[i-j], S[i+j]} == {"G", "R"}:
                    ans -= 1
    print(ans)

main()

Submission status: Accepted

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

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

cnt = 0
for i in range(N-3):
    if S[i] == 'R':
        r -= 1
        cnt += g * b 
        l = 1
        while (i + 2 * l < N):
            if (S[i+l] == 'G' and S[i+2*l] == 'B') or (S[i+l] == 'B' and S[i+2*l] == 'G'):
                cnt -= 1
            l += 1
    if S[i] == 'G':
        g -= 1
        cnt += r * b 
        l = 1
        while (i + 2 * l < N):
            if (S[i+l] == 'R' and S[i+2*l] == 'B') or (S[i+l] == 'B' and S[i+2*l] == 'R'):
                cnt -= 1
            l += 1
    if S[i] == 'B':
        b -= 1
        cnt += g * r
        l = 1
        while (i + 2 * l < N):
            if (S[i+l] == 'G' and S[i+2*l] == 'R') or (S[i+l] == 'R' and S[i+2*l] == 'G'):
                cnt -= 1
            l += 1
print(cnt)

Submission status: Accepted

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


if '__main__' == __name__:
    resolve()

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    S = input()
    RGBdict = {"R":0,"G":0,"B":0}
    for i in range(N):
        RGBdict[S[i]] += 1

    if N%2 == 0:
        maxsa = (N//2) -1
    else:
        maxsa = N//2

    tmp = N - 2
    cnt = 0
    for j in range(maxsa+1):
        num = tmp-(j*2)
        for k in range( num ):
            if (S[N-num+k] != S[N-num+k-j-1]) and (S[N-num+k] != S[N-num+k-2*j-2]) and (S[N-num+k-j-1] != S[N-num+k-2*j-2]):
                cnt += 1

    ans = RGBdict["R"]*RGBdict["G"]*RGBdict["B"]
    ans -= cnt
    return ans

print(main())

Submission status: Accepted

Solution code:
n = int(input())
s = input()
r = 0
g = 0
b = 0
for i in s:
    if i == 'R':
        r += 1
    elif i == 'G':
        g += 1
    else:
        b += 1
ans = r * g * b

for i in range(n):
    for j in range(i+1,n):
        if s[i] == s[j]:
            continue
        k = 2*j - i
        if k >= n or s[k] == s[i] or s[k] == s[j]:
            continue
        ans -= 1
        
print(ans)

Submission status: Accepted

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 j in range(i+1,n):
        if s[i] == s[j]:
            continue
        k = 2*j - i
        if k >= n or s[k] == s[i] or s[k] == s[j]:
            continue
        ans -= 1
        
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
R = set()
G = set()
B = set()
for i in range(N):
    if S[i] == 'R':
        R.add(i)
    elif S[i] == 'G':
        G.add(i)
    else:
        B.add(i)

ans = len(R)*len(G)*len(B)
if ans == 0:
    print(0)
    exit()
for i in R:
    for j in G:
        d = abs(j-i)
        a = min(i, j)-d
        b = (i+j)/2
        c = max(i, j)+d
        s = {a, b, c}
        for k in s:
            if k in B:
                ans -= 1
print(ans)

Submission status: Accepted

Solution code:
import collections
from functools import reduce

n = int(input())
S = input()

c = collections.Counter(list(S))
ans = c['R'] * c['G'] * c['B']

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

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter as C
n=int(input())
S=list(input())
a=1
L=C(S).most_common(3)
for l in L:
  a*=l[1]
for i in range(1,n):
  for j in range(n-2*i):
    if len(set((S[j],S[j+i],S[j+2*i])))==3:
      a-=1
print(a if len(L)>2 else 0)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
ans,r,g,b=0,0,0,0
for s in S:
  if s=='R':
    r+=1
  elif s=='G':
    g+=1
  elif s=='B':
    b+=1
ans=r*g*b
for i in range(N-2):
  a=S[i]
  for j in range(i+1,N-1):
    if 2*j-i>=N:
      break
    b,c=S[j],S[j+j-i]
    if a!=b and b!=c and c!=a:
      ans-=1
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
R=[]
G=[]
B=[]
def f():
  for i in range(n):
    if s[i]=="R":
      R.append(i)
    elif s[i]=="G":
      G.append(i)
    else:
      B.append(i)
  r=len(R)
  g=len(G)
  b=len(B)
  if r==0 or g==0 or b==0:
    return 0
  else:
    ans=r*g*b
    m=0
    for j in range(1,n-1):
      for k in range(1,min(j,n-1-j)+1):
        lt=s[j-k]
        md=s[j]
        rt=s[j+k]
        if lt!=md and md!=rt and rt!=lt:
          m+=1
    return ans-m
print(f())

Submission status: Accepted

Solution code:
def main():
  N = int(input())
  S = input()
  ans=S.count('R')*S.count('G')*S.count('B')
  for i in range(N):
    for j in range(i+1, N):
      k = 2*j-i
      if k>=N:
        break
      if (S[i]!=S[j]) and (S[j]!=S[k]) and(S[i]!=S[k]):
        ans-=1      
  print(ans)

if __name__=='__main__':
  main()

Submission status: Accepted

Solution code:
n=int(input())
s=list(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)

import bisect
def binary_search(a, x):
    # 数列aのなかにxと等しいものがあるか返す
    i = bisect.bisect_left(a, x)
    if i != len(a) and a[i] == x:
        return True
    else:
        return False

ans=0
for i in range(len(r)):
    for j in range(len(g)):
        p=0
        if r[i]<g[j]:
            if binary_search(b,g[j]-r[i]+g[j]):
                p+=1
            if binary_search(b,r[i]+(g[j]-r[i])/2):
                p+=1
            if binary_search(b,r[i]-(g[j]-r[i])):
                p+=1
        else:
            if binary_search(b,r[i]-g[j]+r[i]):
                p+=1
            if binary_search(b,g[j]+(r[i]-g[j])/2):
                p+=1
            if binary_search(b,g[j]-(r[i]-g[j])):
                p+=1
        ans+=len(b)-p

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())
ans = s.count("R")*s.count("G")*s.count("B")
for i in range(n):
    for d in range(n):
        j = i + d
        k = j + d
        if k >= n:
            break
     
        if s[i] != s[j] and  s[i] != s[k] and s[k] != s[j]:
            ans -= 1
print(ans)

Submission status: Accepted

Solution code:
import itertools
import bisect
import sys
sys.setrecursionlimit(10**9)

def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))


INF=10**20
def main():
    N=ii()
    S=list(input())

    R,G,B=[],[],[]
    Rset,Gset,Bset = set(),set(),set()
    
    for i in range(N):
        s = S[i]
        if s == "R":
            R.append(i)
            Rset.add(i)

        elif s == "G":
            G.append(i)
            Gset.add(i)
        
        else:
            B.append(i)
            Bset.add(i)

    ans = 0
    dic = {"R":(R,Rset),"G":(G,Gset),"B":(B,Bset)}
    for comb in itertools.permutations(["R","G","B"]):
        X,Y,Z = dic[comb[0]][0], dic[comb[1]][0], dic[comb[2]][0]
        Xset,Yset,Zset = dic[comb[0]][1], dic[comb[1]][1], dic[comb[2]][1]

        # print(X,Y,Z)
        for x in X:
            for y in Y:
                if not x < y: continue
                ignore_z = 2*y - x
                has_ignore_target = ignore_z in Zset

                iz = bisect.bisect_left(Z,y+1)
                if not 0<=iz<len(Z): continue
                z_count = len(Z)-iz

                if has_ignore_target:
                    z_count -= 1
                
                assert z_count >= 0
                ans += z_count
                # print(ans)


    print(ans)





if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
s=input()
r,g,b=[],[],[]
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=len(r)*len(g)*len(b)

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

print(ans)

Submission status: Accepted

Solution code:
import itertools
N=int(input())
S=input()
ans=S.count("R")*S.count("G")*S.count("B")
c=list(itertools.permutations(["R","B","G"],3))
for j in range(1,N):
    for i in range(N-j*2):
        if (S[i],S[i+j],S[i+j*2]) in c:
            ans-=1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = "A" + input()
r, g, b = 0, 0, 0

for s in S[1:]:
    if s == "R":
        r += 1
    elif s == "G":
        g += 1
    else:
        b += 1

pat = r*g*b

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

print(pat)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import collections


def main():
    N = int(input())
    S = input()
    RGB = ('R', 'G', 'B')
    cnt = collections.Counter(S)
    ans = cnt['R'] * cnt['G'] * cnt['B']

    for i in range(N - 2):
        for j in range(i + 1, N - 1):
            if S[i] != S[j]:
                if 2 * j - i < N:
                    if S[2 * j - i] == RGB[3 - RGB.index(S[i]) - RGB.index(S[j])]:
                        ans -= 1

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from itertools import permutations

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

out = 0
ans = s.count("R")*s.count("G")*s.count("B")

combi = list(permutations(["R","G","B"],3))

for i in range(1,n):
    for j in range(n-i*2):
        if (s[j], s[j+i], s[j+i*2]) in combi:
            ans -= 1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
total = S.count("R") * S.count("G") * S.count("B")
sub = 0
for i in range(N):
    for j in range(i + 1, N):
        if S[i] == S[j]:
            continue
        h = j + j - i
        if h > N - 1 or S[j] == S[h] or S[h] == S[i]:
            continue
        sub += 1
print(total - sub)

Submission status: Accepted

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(0,N):
  for j in range(i,N):
    k = 2*j - i
    if j < k <= N-1:
      if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
        ans -= 1

print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    S = input()
    RGB = [0]*3
    for s in S:
        if s == "R":
            RGB[0] += 1
        elif s == "G":
            RGB[1] += 1
        else:
            RGB[2] += 1
    ans = RGB[0] * RGB[1] * RGB[2]
    for d in range(1, N):
        for i in range(N-d+1):
            if N <= i + 2*d:
                break
            if S[i] != S[i+d] and S[i] != S[i+2*d] and S[i+d] != S[i+2*d]:
                ans -= 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Rr = 0
Gr = 0
Br = 0
Rl = 0
Gl = 0
Bl = 0

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

ans = 0
for i in range(N-1):
  if S[i] == "R":
    ans += Gl*Br
    ans += Bl*Gr
    Rl += 1
  elif S[i] == "G":
    ans += Rl*Br
    ans += Bl*Rr
    Gl += 1
  else:
    ans += Rl*Gr
    ans += Gl*Rr
    Bl += 1
    
  if S[i+1] == "R":
    Rr -= 1
  elif S[i+1] == "G":
    Gr -= 1
  else:
    Br -= 1
  
for i in range(1, N-1):
  for k in range(1, min(i, N-1-i)+1):
    if S[i] != S[i+k] and S[i] != S[i-k] and S[i+k] != S[i-k]:
      ans -= 1
  
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
d = {'R': 0, 'G': 0, 'B': 0}
for i in range(N):
    d[S[i]]+=1
tot = d['R']*d['G']*d['B']

counter = 0
for i in range(N):
    width = 1
    tmp = i+2
    while tmp < N:
        if S[i] != S[i+width] and S[i] != S[i+2*width] and S[i+width] != S[i+2*width]:
            counter += 1
        width += 1
        tmp = i + 2 * width
print(tot-counter)

Submission status: Accepted

Solution code:
def main():
    import bisect
    n = int(input())
    s = input()
    r,g,b = 0,0,0
    for i in range(n):
        if s[i]=='R':
            r+=1
        elif s[i]=='G':
            g+=1
        else:
            b+=1
    ans = r*g*b
    for i in range(n-1):
        for j in range(i+1,n):
            k = 2*(j+1)-(i+1)-1
            if k>n-1:
                continue
            if s[i]!=s[j] and s[i] != s[k] and s[j]!=s[k]:
                ans -= 1
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import Counter
def solve():
  N = int(input())
  S = list(input())
  c = Counter(S)
  ans = c['R']*c['G']*c['B']
  for i in range(N):
    for j in range(i+1,N):
      k = j*2-i
      if k>N-1:
        break
      if S[i]!=S[j] and S[j]!=S[k] and S[i]!=S[k]:
        ans -= 1
  return ans
print(solve())

Submission status: Accepted

Solution code:
ini = lambda : int(input())
inm = lambda : map(int,input().split())
inl = lambda : list(map(int,input().split()))
gcd = lambda x,y : gcd(y,x%y) if x%y else y
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])
    if arr==[]:
        arr.append([n, 1])
    return arr
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    divisors.sort()
    return divisors

#maincode-------------------------------------------------
n = ini()
s = input()
r = s.count('R')
g = s.count('G')
b = s.count('B')
ans = r*g*b

for j in range(n):
    for i in range(j):
        k = 2*j-i
        if k < n:
            if (s[i] == s[j]):
                continue
            if (s[i] == s[k]):
                continue
            if (s[j] == s[k]):
                continue
            ans -= 1
print(ans)

Submission status: Accepted

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 = r * g * b
for i in range(n):
    d = 1
    j = i + d
    k = j + d
    while k < n:
        if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
            ans -= 1
        d += 1
        j += 1
        k += 2
print(ans)

Submission status: Accepted

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:
        if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
            ans -= 1
        d += 1
        j += 1
        k += 2
print(ans)

Submission status: Accepted

Solution code:
def solve():
    N = int(input())
    S = input()
    cnt = {}
    for c in S:
        cnt[c] = cnt.get(c, 0) + 1
    total_triplets = cnt.get('R', 0) * cnt.get('G', 0) * cnt.get('B', 0)
    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: Accepted

Solution code:
#!/usr/bin/env python3
def main():
    N = int(input())
    S = input()

    res = 0
    R, G, B = S.count('R'), S.count('G'), S.count('B')
    for i in range(N - 2):
        for j in range(i + 1, N - 1):
            k = j + j - i
            if k >= N:
                continue
            if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                res += 1
    print(R * G * B - res)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**7)
import bisect
import heapq
import itertools
import math
from collections import Counter, defaultdict, deque
from copy import deepcopy
from decimal import Decimal
from math import gcd
from operator import add, itemgetter, mul, xor
def cmb(n,r,mod):
  bunshi=1
  bunbo=1
  for i in range(r):
    bunbo = bunbo*(i+1)%mod
    bunshi = bunshi*(n-i)%mod
  return (bunshi*pow(bunbo,mod-2,mod))%mod
mod = 10**9+7
def I(): return int(input())
def LI(): return list(map(int,input().split()))
def MI(): return map(int,input().split())
def LLI(n): return [list(map(int, input().split())) for _ in range(n)]

#bisect.bisect_left(list,key)はlistのなかでkey未満の数字がいくつあるかを返す
#つまりlist[i] < x となる i の個数
#bisect.bisect_right(list, key)はlistのなかでkey以下の数字がいくつあるかを返す
#つまりlist[i] <= x となる i の個数
#これを応用することで
#len(list) - bisect.bisect_left(list,key)はlistのなかでkey以上の数字がいくつあるかを返す
#len(list) - bisect.bisect_right(list,key)はlistのなかでkeyより大きい数字がいくつあるかを返す
#これらを使うときはあらかじめlistをソートしておくこと！
n = I()
s = input()
c = Counter(s)
ans = 0
for i in range(n):
    for k in range(i+1,n):
        if (i+k)%2 == 0:
            j = (i+k)//2
            if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
                ans+=1
ans_2 = c["R"]*c["G"]*c["B"]
print(ans_2-ans)

Submission status: Accepted

Solution code:
import bisect, collections, copy, heapq, itertools, math, string
from functools import reduce
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

N = I()
S = S()
R_num = [n for n, v in enumerate(S) if v == 'R']
G_num = [n for n, v in enumerate(S) if v == 'G']
B_num = [n for n, v in enumerate(S) if v == 'B']


cnt = 0
for r in R_num:
    for g in G_num:
        x = r + g
        if x % 2 == 0:
            if x // 2 in B_num:
                cnt += 1
for g in G_num:
    for b in B_num:
        x = g + b
        if x % 2 == 0:
            if x // 2 in R_num:
                cnt += 1
for r in R_num:
    for b in B_num:
        x = r + b
        if x % 2 == 0:
            if x // 2 in G_num:
                cnt += 1

ans = len(R_num) * len(G_num) * len(B_num) - cnt
print(ans)

Submission status: Accepted

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

    r = g = b = 0
    for c in S:
        if c == "R":
            r += 1
        if c == "G":
            g += 1
        if c == "B":
            b += 1

    tmp = 0
    for i in range(len(S)):
        for j in range(i + 1, len(S)):
            k = 2*j - i
            if  k < len(S) and S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
                tmp += 1
    
    ans = r*g*b - tmp

    print(ans)

if __name__ == "__main__":
    main5()

Submission status: Accepted

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

    r = g = b = 0
    for c in S:
        if c == "R":
            r += 1
        if c == "G":
            g += 1
        if c == "B":
            b += 1

    tmp = 0
    for i in range(len(S)):
        for j in range(len(S)):
            if i < j:
                k = 2*j - i
                if  k < len(S) and S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
                    tmp += 1
    
    ans = r*g*b - tmp

    print(ans)

if __name__ == "__main__":
    main5()

Submission status: Accepted

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

    R = set()
    G = set()
    B = set()

    for i in range(len(S)):
        if   S[i] == "R":
            R.add(i)
        elif S[i] == "G":
            G.add(i)
        elif S[i] == "B":
            B.add(i)

    ans = 0
    for r in R:
        for g in G:
            a, b = sorted([r, g])
            tmp = len(B)

            if ((b + a) % 2 == 0) and ((b + a) // 2) in B:
                tmp -= 1

            if (2*a - b) in B:
                tmp -= 1

            if (2*b - a) in B:
                tmp -= 1

            ans += tmp
                
    print(ans)

if __name__ == "__main__":
    main4()

Submission status: Accepted

Solution code:
import sys

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))


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

Submission status: Accepted

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 d in range(n):
        j = i + d
        k = j + d
        if k >= n:
            break 
#        logging.debug("{},{},{}".format(s[i],s[j],s[k]))
        if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
#            logging.debug("☆")
            ans -= 1
print(ans)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
    n = int(input())
    s = input()
    r = s.count('R') * s.count('G') * s.count('B')

    for i1 in range(n-2):
        for i2 in range(i1 + 1, n-1):
            k = i2 * 2 - i1
            if k < n:
                if s[i1] != s[i2] and s[i1] != s[k] and s[i2] != s[k]:
                    r -= 1
    print(r)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

for i in range(len(S)-2):
    j = i + 1
    while len(S) > 2*j - i:
        k = 2*j - i
        if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
            count -= 1
        j += 1

print(count)

Submission status: Accepted

Solution code:

from collections import Counter

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

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

print(ctr["R"] * ctr["G"] * ctr["B"] - res)

Submission status: Accepted

Solution code:

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

d = {"R": 0, "G": 1, "B": 2}
ctr = [[0] * 3 for _ in range(N + 1)]
for i in range(N):
    ctr[i + 1][d[S[i]]] += 1
    for j in range(3):
        ctr[i + 1][j] += ctr[i][j]


ans = 0
for i in range(N - 1):
    for j in range(i + 1, N):
        if S[i] != S[j]:
            c = (set("RGB") - set([S[i], S[j]])).pop()
            cnt = ctr[-1][d[c]] - ctr[j + 1][d[c]]

            k = 2 * (j + 1) - (i + 1) - 1
            if j < k < N and c == S[k]:
                cnt -= 1

            ans += max(0, cnt)

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
ans=0
r,g,b=0,0,0
for i in range(n):
    if s[i]=="R":
        r+=1
    elif s[i]=="G":
        g+=1
    else:
        b+=1
for i in range(1,n):
    for j in range(i+1,n):
        if (j-i)+j<=n:
            k=(j-i)+j-1
            if  s[i-1]!=s[j-1] and  s[j-1]!=s[k] and s[k]!=s[i-1]:
                ans-=1
ans+=r*g*b
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

t="RGBRGB"

c=0
cm=0
for i in range(3):
    for j in range(1,n-1):
        if s[j]==t[i] :
            low = s[:j].count(t[i+1])
            high = s[j:].count(t[i+2])
            c += low*high
            low = s[:j].count(t[i+2])
            high = s[j:].count(t[i+1])
            c += low*high
            for k in range(1,j+1):
                if j+k>n-1 :
                    break
                if s[j-k]!=t[i] and s[j+k]!=t[i] and s[j-k]!=s[j+k]:
                    cm+=1


print(c-cm)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import copy

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

cnt = [[0]*n for i in range(3)]
r, g, b = 0, 0, 0
for i in range(n-1, -1, -1):
    
    if s[i] == 'R':
        r += 1
        cnt[0][i] = copy.deepcopy(r)
        cnt[1][i] = copy.deepcopy(g)
        cnt[2][i] = copy.deepcopy(b)
    if s[i] == 'G':
        g += 1
        cnt[0][i] = copy.deepcopy(r)
        cnt[1][i] = copy.deepcopy(g)
        cnt[2][i] = copy.deepcopy(b)
    if s[i] == 'B':
        b += 1
        cnt[0][i] = copy.deepcopy(r)
        cnt[1][i] = copy.deepcopy(g)
        cnt[2][i] = copy.deepcopy(b)
        

ans = 0
t = ['R', 'G', 'B']
for i in range(n-2):
    for j in range(i+1,n-1):
        if s[i] == s[j] :
            continue
        if s[i] != 'R' and s[j] != 'R':
            k = 0
        elif s[i] != 'G' and s[j] != 'G':
            k = 1
        else:
            k = 2
        if 2*j - i < n and s[2*j - i] == t[k]:
            ans += cnt[k][j+1] - 1
        else:
            ans += cnt[k][j+1]
print(ans)

Submission status: Accepted

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

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

for start in range(n):
    for kousa in range(n):
        if start+2*kousa>n-1:
            break
        if s[start]!=s[start+kousa] and s[start+kousa]!=s[start+2*kousa] and s[start+2*kousa]!=s[start]:
            ans-=1

print(ans)