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 collections
import math
n = int(input())
s_list = list(input())
c = collections.Counter(s_list)
ans = c["R"] * c["G"] * c["B"]
for i in range(n):
    for j in range(i + 1, n):
        k = j + j - i
        if k < n:
            if s_list[i] != s_list[j] and s_list[i] != s_list[k] and s_list[j] != s_list[k]:
                ans -= 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
r = 0
g = 0
b = 0
for x in s:
    if x == 'R':
        r += 1
    elif x == 'G':
        g += 1
    elif x == 'B':
        b += 1

ng_cnt = 0
for i in range(n):
    for j in range(i+1, n):
        if s[i] == s[j]:
            continue
        else:
            k = 2 * j - i
            if k < n and s[i] != s[k] and s[j] != s[k]:
                ng_cnt += 1

print(r * g * b - ng_cnt)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
S = list(input())
s_c = Counter(S)

a = s_c["R"] * s_c["B"] * s_c["G"]

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

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


if __name__ == "__main__":
    main()

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-2):
        for j in range(i+1, i+1+(N-1-i)//2):
            k = j + (j - i)
            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:
def main():
    N = int(input())
    S = input()
    
    ans = S.count('R') * S.count('G') * S.count('B')

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

R = S.count('R')
G = S.count('G')
B = S.count('B')

impossible = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        delta = j - i
        k = j + delta
        if k >= n:
            break

        pairs = {S[i], S[j], S[k]}
        if len(pairs) == 3:
            impossible += 1

ans = R * G * B - impossible
print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    S = input()
    
    ans = S.count('R') * S.count('G') * S.count('B')
    colors = set(list('RGB'))

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline

N = int(readline())
S = [1 if s == 'R' else 2 if s == 'G' else 0 for s in readline().strip()]


cnt = [[0]*3 for _ in range(N)]
for i in range(N):
    cnt[i][S[i]] = 1
for s in range(3):
    for i in range(1, N):
        cnt[i][s] += cnt[i-1][s]

ans = 0
for j in range(1, N-1):
    c1 = S[j]
    c2 = c1 - 1
    c3 = c1 - 2
    r1c2 = cnt[j-1][c2]
    r2c2 = cnt[-1][c2] - cnt[j][c2]
    r1c3 = cnt[j-1][c3]
    r2c3 = cnt[-1][c3] - cnt[j][c3]
    ans += r1c2*r2c3 + r1c3*r2c2
for i in range(N):
    for j in range(1, N):
        if i+2*j >= N:
            break
        if len(set([S[i], S[i+j], S[i+2*j]])) == 3:
            ans -= 1

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
def main():
    n = int(input())
    s = input()
    d = Counter(s) 
    ans = d['R'] * d['G'] * d['B']
    cnt = 0
    for i in range(n):
        for j in range(i+1,n): 
            k = 2*j-i 
            if k < n:
                x = s[i]
                y = s[j]
                z = s[k]
                if (x!=y) and (x!=z) and (y!=z):
                    cnt+=1
    
    print(ans-cnt)
main()

Submission status: Accepted

Solution code:
def remaining(s1, s2):
    if not('R' in [s1, s2]):
        return 'R'
    elif not('G' in [s1, s2]):
        return 'G'
    else:
        return 'B'

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

# acc[color][i]: i番目からN番目までのcolorの数
acc = {
    'R': [0 for _ in range(N)],
    'G': [0 for _ in range(N)],
    'B': [0 for _ in range(N)]
}
acc[S[-1]][-1] = 1
for i in range(N-2, -1, -1):
    acc[S[i]][i] = acc[S[i]][i+1]+1
    if S[i] == 'R':
        acc['G'][i] = acc['G'][i+1]
        acc['B'][i] = acc['B'][i+1]
    elif S[i] == 'G':
        acc['R'][i] = acc['R'][i+1]
        acc['B'][i] = acc['B'][i+1]
    else:
        acc['G'][i] = acc['G'][i+1]
        acc['R'][i] = acc['R'][i+1]


if __name__ == "__main__":
    ans = 0
    for i in range(N-2):
        for j in range(i+1, N-1):
            if S[i]==S[j]:
                continue
            color = remaining(S[i], S[j])
            ans += acc[color][j+1]
            if j+j-i < N:
                if S[j+(j-i)] != S[i] and S[j+(j-i)] != S[j]:
                    ans -= 1
    print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from itertools import *
from functools import *
from collections import *
from heapq import heapify, heappop, heappush, heappushpop

INF = float('inf')
NIL = - 1


N = int(input())
S = input().rstrip()

r_count = S.count('R')
g_count = S.count('G')
b_count = S.count('B')

# ret = 0
# 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]:
#             ret += 1

ret = 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[j] != S[k] and S[k] != S[i]:
            ret += 1


ans = r_count * g_count * b_count - ret
print(ans)

Submission status: Accepted

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

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

cnt = {'R': 0, 'G': 0, 'B': 0}
for c in S:
    cnt[c] += 1

print(cnt['R'] * cnt['G'] * cnt['B'] - same)

Submission status: Accepted

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

R = [X for X, x in enumerate(S) if x == 'R']
G = [Y for Y, y in enumerate(S) if y == 'G']
B = [Z for Z, z in enumerate(S) if z == 'B']

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

cnt=0
for i in range(0,N-2):
    for j in range(i+1,N-1):
        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(mae-cnt)

Submission status: Accepted

Solution code:
from time import time

def main():
    N = int(input())
    S = input()

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

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

if __name__ == '__main__':
    start = time()
    main()
    # print(time() - start)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from itertools import *
from functools import *
from collections import *
from heapq import heapify, heappop, heappush, heappushpop
from numba import njit

INF = float('inf')
NIL = - 1


N = int(input())
S = input().rstrip()

r_count = S.count('R')
g_count = S.count('G')
b_count = S.count('B')

ret = 0
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]:
            ret += 1

ans = r_count * g_count * b_count - ret
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
    
ct = 0
for i in range(n):
    for j in range(i + 1, n):
        k = j + j - i
        if k >= n:
            break
        if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
            ct += 1                

print(r*g*b-ct)

Submission status: Accepted

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

R = 0
G = 0
B = 0

# R,G,Bの数を数える
for i in range(N):
    if S[i] == 'R':
        R += 1
    elif S[i] == 'G':
        G += 1
    else:
        B += 1
    
# 等間隔で(R,G,B)の組み合わせとなっている数を数える
# 間隔(step)は1～1999のいずれか
count = 0
for step in range(1,2000):
    start = 0
    while (start + step * 2) < N:
        i,j,k = start, start+step, start+step*2
        if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
            count += 1
        start += 1

# print(count)

print(R*G*B - count)

Submission status: Accepted

Solution code:


def solve():

    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):
            # j - i = k - j
            k = j-i+j
            if k >= N:
                continue
            if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
                ans -= 1

    print(ans)


if __name__ == "__main__":
    solve()

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 diff in range(n):
        j = i + diff
        k = i + 2*diff
        if k >= n:
            break
        elif 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()

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

Submission status: Accepted

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

r = 0
r_l = []
g = 0
g_l = []
b = 0
b_l = []

for i in range(n):
    if s[i] == "R":
        r += 1
        r_l.append(i)
    if s[i] == "G":
        g += 1
        g_l.append(i)
    if s[i] == "B":
        b += 1
        b_l.append(i)
        
ans = r*g*b

for i in r_l:
    for j in g_l:
        tmp = abs(i-j)
        if 0 <= i-tmp and s[i-tmp] == "B":
            ans -= 1
        if 0 <= j-tmp and s[j-tmp] == "B":
            ans -= 1
        if i+tmp < n and s[i+tmp] == "B":
            ans -= 1
        if j+tmp < n and s[j+tmp] == "B":
            ans -= 1

        if (tmp / 2).is_integer():
            if s[max(i,j)-(tmp//2)] == "B":
                ans -= 1
            if max(i,j)-(tmp//2) != min(i,j)+(tmp//2) and s[min(i,j)+(tmp//2)] == "B":
                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-2):
    t = 1
    while i + 2 * t < N:
        if(S[i] != S[i+t] and S[i] != S[i+2*t] and S[i+t] != S[i+2*t]):
            ans -= 1
        t += 1
print(ans)

Submission status: Accepted

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

# 全ての組み合わせ数
allcnt = S.count("R") * S.count("G") * S.count("B")

# 除外するものチェック
rem = 0
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]:
      rem += 1
print(allcnt - rem)

Submission status: Accepted

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

rcnt=s.count("R")
gcnt=s.count("G")
bcnt=s.count("B")
sum=rcnt*gcnt*bcnt#条件1を満たす組み合わせの数

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[j]!=s[k]:#等間隔の組み合わせを除く
            sum-=1
print(sum)

Submission status: Accepted

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

  
ans = 0

for i in range(len(R)):
  for j in range(len(G)):
    temp = 0
    if 2*R[i] - G[j] >0 and 2*R[i] - G[j] <= n:
      if s[2*R[i] - G[j] -1] == "B":
        temp += 1
    if 2*G[j] - R[i] >0 and 2*G[j] - R[i] <= n:
      if s[2*G[j] - R[i]-1] == "B":
        temp += 1
    if (R[i]+G[j])%2 == 0:
      if s[(R[i]+G[j])//2-1] == "B":
        temp += 1
    ans += len(B)- temp

print(ans)

Submission status: Accepted

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

r = 0
g = 0
b = 0
for c in s:
    if c == 'R':
        r += 1
    if c == 'G':
        g += 1
    if c == 'B':
        b += 1

total = r*g*b

for i in range(0, n-2):
    for j in range(i+1, n-1):
        k = j - i + j
        if k >= n:
            break
        elif s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
            total -= 1
print(total)

Submission status: Accepted

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

rc = 0
gc = 0
bc = 0

for si in S:
    if si == 'R':
        rc += 1
    elif si == 'G':
        gc += 1
    else:
        bc += 1


bad = 0
for i in range(N):
    for j in range(i+1, N):
        k = j + (j-i)
        if k > N-1:
            # continue
            break
        if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
            # print('bad', i,j,k,S[i],S[j],S[k])
            bad += 1

ans = rc * gc * bc
# print(ans)
ans -= bad
print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    S = list(str(input()))
    sum = S.count("R") * S.count("G") * S.count("B")
    for i in range(0, N-2):
        si = S[i]
        for j in range(i+1, N-1):
            sj = S[j]
            k = 2*j -i  # Nを超える
            if k < N:
                sk = S[k]
                #if j - i != k -j and si != sj and si != sk and sj != sk:
                if si != sj and si != sk and sj != sk:
                    sum -= 1
    print(sum)

    # 提出 #11871188


main()

Submission status: Accepted

Solution code:
n = int(input())

rgb = input()

r = rgb.count("R")
g = rgb.count("G")
b = rgb.count("B")

ans = r * g * b

for i in range(n):
    for k in range(n):
        a = i + k
        b = a + k
        if b >= n:break
        if rgb[i] != rgb[a] and rgb[i] != rgb[b] and rgb[a] != rgb[b]:
            ans -= 1 
print(ans)

Submission status: Accepted

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

r = set(r)
g = set(g)
b = set(b)
for i in r:
    for j in g:
        if 2 * j - i in b:
            val +=1
for i in g:
    for j in b:
        if 2 * j - i in r:
            val +=1
for i in b:
    for j in r:
        if 2 * j - i in g:
            val +=1

print(len(r) * len(g) * len(b) - val)

Submission status: Accepted

Solution code:
from collections import Counter

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

c = Counter(s)
ans = c['R'] * c['G'] * c['B']

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

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
r,g,b=map(s.count,'RGB')
print(r*g*b-len([1for i in range(n)for j in range(i,n,2)if s[i]!=s[j]!=s[i+j>>1]!=s[i]]))

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 - 2):
    for j in range(i + 1, n - 1):
        k = j + (j - i)
        if k < n and 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()

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-i + j
        if k < N and S[i] != S[j] and S[j] != S[k] and S[i] != S[k]:
            ans -= 1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

    rcs = [0] * N
    gcs = [0] * N
    bcs = [0] * N

    for i in range(N):
        if S[i] == 'R':
            rcs[i] = 1
        elif S[i] == 'G':
            gcs[i] = 1
        elif S[i] == 'B':
            bcs[i] = 1

    for i in range(1, N):
        rcs[i] += rcs[i - 1]
        gcs[i] += gcs[i - 1]
        bcs[i] += bcs[i - 1]

    result = 0
    for i in range(N):
        if S[i] == 'R':
            for j in range(i + 1, N):
                if S[j] == 'R':
                    continue
                elif S[j] == 'G':
                    result += bcs[N - 1] - bcs[j]
                    t = 2 * j - i
                    if t < N and S[t] == 'B':
                        result -= 1
                elif S[j] == 'B':
                    result += gcs[N - 1] - gcs[j]
                    t = 2 * j - i
                    if t < N and S[t] == 'G':
                        result -= 1
        elif S[i] == 'G':
            for j in range(i + 1, N):
                if S[j] == 'G':
                    continue
                elif S[j] == 'R':
                    result += bcs[N - 1] - bcs[j]
                    t = 2 * j - i
                    if t < N and S[t] == 'B':
                        result -= 1
                elif S[j] == 'B':
                    result += rcs[N - 1] - rcs[j]
                    t = 2 * j - i
                    if t < N and S[t] == 'R':
                        result -= 1
        elif S[i] == 'B':
            for j in range(i + 1, N):
                if S[j] == 'B':
                    continue
                elif S[j] == 'R':
                    result += gcs[N - 1] - gcs[j]
                    t = 2 * j - i
                    if t < N and S[t] == 'G':
                        result -= 1
                elif S[j] == 'G':
                    result += rcs[N - 1] - rcs[j]
                    t = 2 * j - i
                    if t < N and S[t] == 'R':
                        result -= 1
    print(result)


main()

Submission status: Accepted

Solution code:
import sys
import fractions

def in_ctrl():
    _input = [_.rstrip() for _ in sys.stdin.readlines()]
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    N = int(_input[0])
    S = _input[1]
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    return N,S


def out_ctrl(s):
    print(s)


if __name__ == "__main__":
    N,S = in_ctrl()
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    cnt = 0
    R = list(map(bool,list(map(int,list(S.replace('R','1').replace('G','0').replace('B','0'))))))
    G = list(map(bool,list(map(int,list(S.replace('R','0').replace('G','1').replace('B','0'))))))
    B = list(map(bool,list(map(int,list(S.replace('R','0').replace('G','0').replace('B','1'))))))
    R_cum, G_cum, B_cum = {}, {}, {}
    for i,r in enumerate(R):
        if r:
            for j,g in enumerate(G[i+1:]):
                if g:
                    if i+j+2 not in B_cum:
                        B_cum[i+j+2] = sum(B[i+j+2:])
                    if i+2*j+2 < N:
                        cnt += B_cum[i+j+2] - B[i+2*j+2]
                    else:
                        cnt += B_cum[i+j+2]
            for j,g in enumerate(B[i+1:]):
                if g:
                    if i+j+2 not in G_cum:
                        G_cum[i+j+2] = sum(G[i+j+2:])
                    if i+2*j+2 < N:
                        cnt += G_cum[i+j+2] - G[i+2*j+2]
                    else:
                        cnt += G_cum[i+j+2]
    for i,r in enumerate(G):
        if r:
            for j,g in enumerate(R[i+1:]):
                if g:
                    if i+j+2 not in B_cum:
                        B_cum[i+j+2] = sum(B[i+j+2:])
                    if i+2*j+2 < N:
                        cnt += B_cum[i+j+2] - B[i+2*j+2]
                    else:
                        cnt += B_cum[i+j+2]
            for j,b in enumerate(B[i+1:]):
                if b:
                    if i+j+2 not in R_cum:
                        R_cum[i+j+2] = sum(R[i+j+2:])
                    if i+2*j+2 < N:
                        cnt += R_cum[i+j+2] - R[i+2*j+2]
                    else:
                        cnt += R_cum[i+j+2]
    for i,r in enumerate(B):
        if r:
            for j,g in enumerate(R[i+1:]):
                if g:
                    if i+j+2 not in G_cum:
                        G_cum[i+j+2] = sum(G[i+j+2:])
                    if i+2*j+2 < N:
                        cnt += G_cum[i+j+2] - G[i+2*j+2]
                    else:
                        cnt += G_cum[i+j+2]
            for j,b in enumerate(G[i+1:]):
                if b:
                    if i+j+2 not in R_cum:
                        R_cum[i+j+2] = sum(R[i+j+2:])
                    if i+2*j+2 < N:
                        cnt += R_cum[i+j+2] - R[i+2*j+2]
                    else:
                        cnt += R_cum[i+j+2]
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    out_ctrl(cnt)

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

print(ans)

Submission status: Accepted

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

R_i = {}
G_i = {}
B_i = {}

ans = 0
for i in range(n) :
  if s[i] == 'R' :
    R_i[i] = 1
  if s[i] == 'G' :
    G_i[i] = 1
  if s[i] == 'B' :
    B_i[i] = 1

ans = len(R_i)*len(G_i)*len(B_i)

for ri in R_i :
  for gi in G_i :
    # Rが真ん中
    if 2*ri-gi in B_i :
      ans -= 1
    # Gが真ん中
    if 2*gi-ri in B_i :
      ans -= 1
    # Bが真ん中
    if (ri+gi)%2 == 0 :
      if (ri+gi)//2 in B_i :
        ans -= 1
    
print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

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 = len(r)*len(g)*len(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[i] != S[k] and S[j] != S[k]:
            cnt -= 1
print(cnt)

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

print(ans)

Submission status: Accepted

Solution code:
def main():
  n = int(input())
  s = input()
  r = g = b = d =0
  for i in range(n):
    if s[i] == "R":
      r += 1
    elif s[i] == "G":
      g += 1
    else:
      b += 1
  a = 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
      d += 1
  print(a - d)
  
if __name__ == "__main__":
  main()

Submission status: Accepted

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

cnts = [0, 0, 0]
for i in range(n):
    if s[i] == 'R':
        cnts[0] += 1
    elif s[i] == 'G':
        cnts[1] += 1
    else:
        cnts[2] += 1

ans = cnts[0] * cnts[1] * cnts[2]

for i in range(n):
    for j in range(i+1, n):
        k = j + (j - i)
        if k >= n:
            continue
        if len(set([s[i], s[j], s[k]])) == 3:
            ans -= 1
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
c=s.count
a=c('R')*c('G')*c('B')
for i in range(n):
  for j in range(i+2,n,2):
    if s[i]!=s[i+j>>1]!=s[j]!=s[i]:
      a-=1
print(a)

Submission status: Accepted

Solution code:
#!/usr/bin/env pypy3
# n,m = map(int,sys.stdin.readline().split())
# a = list(map(int,sys.stdin.readline().split()))
# a = [sys.stdin.readline() for s in range(n)]
# s = sys.stdin.readline().rstrip()
# n = int(sys.stdin.readline())
# d = collections.defaultdict(list)
# import math
# from functools import reduce
import sys
sys.setrecursionlimit(15000)

n = int(sys.stdin.readline())
s = sys.stdin.readline().rstrip()
d = {"R":[],"G":[],"B":[]}
sb = set()
for i in range(n):
    d[s[i]].append(i)
    if s[i]=="B":
        sb.add(i)
s = 0
#s = sum(1 for a in d["R"] for b in d["G"] for c in d["B"] if abs(a-b) != abs(b-c) and abs(a-c) != abs(c-b) and abs(b-a) != abs(a-c))
#s = len(d["R"])*len(d["G"])*len(d["B"])-sum(1 for a in d["R"] for b in d["G"] for c in d["B"] if abs(a-b) == abs(b-c) or abs(a-c) == abs(c-b) or abs(b-a) == abs(a-c))
for a in d["R"]:
    for b in d["G"]:
        s += len(d["B"])
        if 2*b-a in sb:
            s -= 1
        if 2*a-b in sb:
            s -= 1
        if (a+b)%2 == 0 and (a+b)//2 in sb:
            s -= 1
print(s)

Submission status: Accepted

Solution code:
def calc(i, j, B1, len_b):
    extra = 0
    d = j - i

    # i < j < k
    k1 = d + j
    if k1 < len_b:
        extra += B1[k1]

    # k < i < j
    k2 = i - d
    if 0 <= k2 and k2 < len_b:
        extra += B1[k2]

    # i < k < j
    s = i + j
    if s % 2 == 0:
        k3 = s // 2
        if k3 < len_b:
            extra += B1[k3]
    return extra

def main():
    N = int(input())
    S = input()
    R, G, B = [], [], []
    RGB = {'R': R, 'G': G, 'B': B}
    for i in range(N):
        RGB[S[i]].append(i)
    num_r, num_g, num_b = len(R), len(G), len(B)
    ans = num_r * num_g * num_b
    if ans == 0:
        print(0)
        return

    len_b = (B[-1] + 1)
    B1 = [0]*len_b
    for b in B:
        B1[b] = 1
    # print(R, G, B)
    for i in R:
        for j in G:
            if i < j:
                ans -= calc(i, j, B1, len_b)
            else:
                ans -= calc(j, i, B1, len_b)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def calc(i, j, B1, len_b):
    extra = 0
    d = j - i

    # i < j < k
    k1 = d + j
    if k1 < len_b:
        extra += B1[k1]

    # k < i < j
    k2 = i - d
    if 0 <= k2 and k2 < len_b:
        extra += B1[k2]

    # i < k < j
    s = i + j
    if s % 2 == 0:
        k3 = s // 2
        if k3 < len_b:
            extra += B1[k3]
    return extra

def main():
    N = int(input())
    S = input()
    R, G, B = [], [], []
    num_r, num_g, num_b = 0, 0, 0
    for i in range(N):
        s = S[i]
        if s == 'R':
            R.append(i)
            num_r += 1
        elif s == 'G':
            G.append(i)
            num_g += 1
        elif s == 'B':
            B.append(i)
            num_b += 1
    if len(R) == 0 or len(G) == 0 or len(B) == 0:
        print(0)
        return
    len_b = (B[-1] + 1)
    B1 = [0]*len_b
    for b in B:
        B1[b] = 1
    # print(R, G, B)
    ans = 0
    for i in R:
        for j in G:
            ans += num_b
            if i < j:
                ans -= calc(i, j, B1, len_b)
            else:
                ans -= calc(j, i, B1, len_b)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def calc(i, j, B1, len_b):
    extra = 0
    d = j - i

    # i < j < k
    k1 = d + j
    if k1 < len_b:
        extra += B1[k1]

    # k < i < j
    k2 = i - d
    if 0 <= k2 and k2 < len_b:
        extra += B1[k2]

    # i < k < j
    s = i + j
    if s % 2 == 0:
        k3 = s // 2
        if k3 < len_b:
            extra += B1[k3]
    return extra

def main():
    N = int(input())
    S = input()
    R, G, B = [], [], []
    RGB = {'R': R, 'G': G, 'B': B}
    for i in range(N):
        RGB[S[i]].append(i)
    num_r, num_g, num_b = len(R), len(G), len(B)
    if num_r == 0 or num_g == 0 or num_b == 0:
        print(0)
        return

    len_b = (B[-1] + 1)
    B1 = [0]*len_b
    for b in B:
        B1[b] = 1
    # print(R, G, B)
    ans = num_r * num_g * num_b
    for i in R:
        for j in G:
            ans -= calc(min(i, j), max(i, j), B1, len_b)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def calc(i, j, B1, len_b):
    extra = 0
    d = j - i

    # i < j < k
    k1 = d + j
    if k1 < len_b:
        extra += B1[k1]

    # k < i < j
    k2 = i - d
    if 0 <= k2 and k2 < len_b:
        extra += B1[k2]

    # i < k < j
    s = i + j
    if s % 2 == 0:
        k3 = s // 2
        if k3 < len_b:
            extra += B1[k3]
    return extra

def main():
    N = int(input())
    S = input()
    RGB = {'R': [], 'G': [], 'B': []}
    for i in range(N):
        RGB[S[i]].append(i)
    num_r = len(RGB['R'])
    num_g = len(RGB['G'])
    num_b = len(RGB['B'])
    if num_r == 0 or num_g == 0 or num_b == 0:
        print(0)
        return

    len_b = (RGB['B'][-1] + 1)
    B1 = [0]*len_b
    for b in RGB['B']:
        B1[b] = 1
    # print(R, G, B)
    ans = num_r * num_g * num_b
    for i in RGB['R']:
        for j in RGB['G']:
            ans -= calc(min(i, j), max(i, j), B1, len_b)
    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-2):
    for j in range(i+1,n-1):
        if 2*j-i <= n-1:
            if s[2*j-i] != s[i] and s[i] != s[j] and s[j] != s[2*j-i]:
                ans -= 1
print(ans)

Submission status: Accepted

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

# それぞれ違う文字の組み合わせの数
ans = S.count('R')*S.count('G')*S.count('B')

# j-i!=k-jの組み合わせ
for i in range(0, N-2):
    for j in range(i+1, N-1):
        k = j+j-i
        if k < N:
            if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                ans -= 1

print(ans)