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 sys
input = sys.stdin.readline
import numpy as np

def main():
    N = int(input())
    S = input().strip()
    rgb = [list() 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)
    
    for i in range(3):
        rgb[i] = np.array(rgb[i])

    ans = 0
    for c in range(3):
        cm = c%3
        c1 = (c+1)%3
        c2 = (c+2)%3
        for m in rgb[cm]:
            tmp = len(set(np.abs(rgb[c1]-m)) & set(np.abs(rgb[c2]-m)))
            ans += tmp
    print(len(rgb[0])*len(rgb[1])*len(rgb[2])-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')

ans = r*g*b

cnt = 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]:
            ans -= 1
ans -= cnt

print(ans)

Submission status: Accepted

Solution code:
import re
import itertools
N = int(input())
S = input()
R = S.count('R')
G = S.count('G')
B = S.count('B')
#R = [m.start() for m in re.finditer('R', S)]
#G = [m.start() for m in re.finditer('G', S)]
#B = [m.start() for m in re.finditer('B', S)]
ans = 0
for i in range(N-2):
  for j in range(1, N-1):
    if i + 2*j >=N:
      break
    else:
      if not(S[i] == S[i + 2*j] or S[i + j] == S[i + 2*j] or S[i] == S[i + j]):
        ans += 1
print(R*G*B - ans)

Submission status: Accepted

Solution code:
from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    n = int(input())
    s = list(input()[:-1].decode())
    r = []
    g = []
    b = [0] * n
    len_b = 0
    for i in range(n):
        if s[i] == 'R':
            r.append(i)
        elif s[i] == 'G':
            g.append(i)
        elif s[i] == 'B':
            b[i] = 1
            len_b += 1
    ans = len(r) * len(g) * len_b

    def check(i):
        return 0 <= i < n and b[i] == 1

    for ri in r:
        for gi in g:
            if check(2 * gi - ri):
                ans -= 1
            if check(2 * ri - gi):
                ans -= 1
            if (ri + gi) % 2 == 0 and check((ri + gi) // 2):
                ans -= 1
    print(ans)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Submission status: Accepted

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

R_cnt, G_cnt, B_cnt = 0, 0, 0

for i in range(len(S)):
    if S[i] == "R":
        R_cnt += 1
    elif S[i] == "G":
        G_cnt += 1
    else:
        B_cnt += 1

All = R_cnt * G_cnt * B_cnt

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

print(All)

Submission status: Accepted

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


R = 0
G = 0
B = 0

for i in range(N):
    s = S[i]
    if (s == 'R'):
        R +=1
    if (s == 'G'):
        G +=1
    if (s == 'B'):
        B +=1

count = R * G * B


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




print(count)

Submission status: Accepted

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

Submission status: Accepted

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

R = [i+1 for i, x in enumerate(S) if x == 'R']
G = [i+1 for i, x in enumerate(S) if x == 'G']
B = [i+1 for i, x in enumerate(S) if x == 'B']

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

count = 0

for r in R:
    for g in G:
        a = r+g
        if a % 2 == 0:
            if S[int(a/2)-1] == 'B':
                count += 1

for r in R:
    for b in B:
        a = r+b
        if a % 2 == 0:
            if S[int(a/2)-1] == 'G':
                count += 1

for g in G:
    for b in B:
        a = g+b
        if a % 2 == 0:
            if S[int(a/2)-1] == 'R':
                count += 1

print(ans - count)

Submission status: Accepted

Solution code:
def main():
    from collections import Counter
    n, s = int(input()), input()
    c = Counter(s)
    d = list(c.values())
    if len(d) <3:
        print(0)
        exit()
    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: Accepted

Solution code:
def main():
    from itertools import product
    n, s = int(input()), input()

    r, g, b = 0, 0, 0
    for x in s:
        if x == "R":
            r += 1
        if x == "G":
            g += 1
        if x == "B":
            b += 1

    ans = r * g * b
    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: 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:
            break
        ans -= s[k] != s[i] != s[j] != s[k]

print(ans)

Submission status: Accepted

Solution code:
def f(a, b, c):
    from itertools import product
    return sum(i + j in a for i, j in product(b, c))


def solve(string):
    n, s = string.lower().split()
    r, g, b, r2, g2, b2 = [], [], [], set([]), set([]), set([])
    for i, _s in enumerate(s):
        eval(f"{_s}.append(i)")
        eval(f"{_s}2.add(2 * i)")
    return str(len(r) * len(g) * len(b) - f(r2, g, b) - f(g2, b, r) - f(b2, r, g))


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Submission status: Accepted

Solution code:
def f(a, b, c):
    from itertools import product
    return sum(i + j in a for i, j in product(b, c))


def solve(string):
    n, s = string.split()
    r, g, b, r2, g2, b2 = [], [], [], set([]), set([]), set([])
    for i, _s in enumerate(s):
        if _s == "R":
            r.append(i)
            r2.add(2 * i)
        if _s == "G":
            g.append(i)
            g2.add(2 * i)
        if _s == "B":
            b.append(i)
            b2.add(2 * i)
    return str(len(r) * len(g) * len(b) - f(r2, g, b) - f(g2, b, r) - f(b2, r, g))


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Submission status: Accepted

Solution code:
import collections
n = int(input())
a = input()
a = a.replace('R','1')
a = a.replace('G','2')
a = a.replace('B','3')
a = tuple(map(int,list(a)))
b = collections.Counter(a)
ans =b[3]*b[1]*b[2]
for i in range(n-2):
    for j in range(i+1,n):
        if a[i] == a[j]:
            continue
        c = j*2 -i
        if c < n:
            if a[i]+a[j]+a[c]==6:
                ans -= 1
        else:
            break
print(ans)

Submission status: Accepted

Solution code:
from itertools import product


def solve(string):
    n, s = string.split()
    r, g, b = [], [], []
    r2, g2, b2 = set([]), set([]), set([])
    for i, _s in enumerate(s):
        if _s == "R":
            r.append(i)
            r2.add(2 * i)
        if _s == "G":
            g.append(i)
            g2.add(2 * i)
        if _s == "B":
            b.append(i)
            b2.add(2 * i)
    ans = len(r) * len(g) * len(b)
    ans -= sum(i + j in r2 for i, j in product(g, b))
    ans -= sum(i + j in g2 for i, j in product(b, r))
    ans -= sum(i + j in b2 for i, j in product(r, g))
    return str(ans)


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Submission status: Accepted

Solution code:
from itertools import product


def solve(string):
    n, s = string.split()
    r, g, b = [], [], []
    r2, g2, b2 = set([]), set([]), set([])
    for i, _s in enumerate(s):
        if _s == "R":
            r.append(i)
            r2.add(2 * i)
        if _s == "G":
            g.append(i)
            g2.add(2 * i)
        if _s == "B":
            b.append(i)
            b2.add(2 * i)
    ans = len(r) * len(g) * len(b)
    for i, j in product(r, g):
        ans -= 1 if i + j in b2 else 0
    for i, j in product(g, b):
        ans -= 1 if i + j in r2 else 0
    for i, j in product(b, r):
        ans -= 1 if i + j in g2 else 0
    return str(ans)


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Submission status: Accepted

Solution code:
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

n=ni()
s = input()

rs=0
gs=0
bs=0

for i in range(n):
  if s[i]=='R':
    rs+=1
  elif s[i]=='G':
    gs+=1
  else:
    bs+=1

total = rs*gs*bs

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

print(total)

Submission status: Accepted

Solution code:
from collections import Counter


def resolve():
    n = int(input())
    s = input()
    cnt = Counter(s)
    ans = cnt['R'] * cnt['G'] * cnt['B']

    for i in range(n - 1):
        for j in range(i + 1, n):
            ken = j * 2 - i
            if (ken >= n):
                continue
            if (s[i] != s[j] and s[j] != s[ken] and s[i] != s[ken]):
                ans -= 1

    print(ans)

resolve()

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 i in range(n):
    for j in range(i, n):
        k = j + (j - i)
        if k < n:
            if s[i] == s[j]: continue
            if s[i] == s[k]: continue
            if s[k] == s[j]: continue
            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):
    if 2*j - i > n-1:
      break
    if 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:
def count_bigger(a, b):
    i = j = 0
    c = []
    while i < len(a) and j < len(b):
        if a[i] >= b[j]:
            j += 1
            continue
        c.append(len(b) - j)
        i += 1
    if len(c) < len(a):
        c += [0] * (len(a) - len(c))
    for i in range(len(c) - 1, 0, -1):
        c[i - 1] += c[i]
    return c


def f(a, b, c):
    bc = count_bigger(b, c)
    i = j = ans = 0
    while i < len(a) and j < len(b):
        if a[i] >= b[j]:
            j += 1
            continue
        ans += bc[j] - len(set(c) & set(2 * _b - a[i] for _b in b[j:]))
        i += 1
    return ans


def solve(string):
    n, s = string.split()
    r, g, b = [], [], []
    for i, _s in enumerate(s):
        if _s == "R":
            r.append(i)
        if _s == "G":
            g.append(i)
        if _s == "B":
            b.append(i)
    return str(f(r, g, b) + f(r, b, g) + f(b, r, g) + f(b, g, r) + f(g, r, b) + f(g, b, r))


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Submission status: Accepted

Solution code:
def count_bigger(a, b):
    i = j = 0
    c = []
    while i < len(a) and j < len(b):
        if a[i] >= b[j]:
            j += 1
            continue
        c.append(len(b) - j)
        i += 1
    if len(c) < len(a):
        c += [0] * (len(a) - len(c))
    return c


def f(a, b, c):
    bc = count_bigger(b, c)
    for i in range(len(bc) - 1, 0, -1):
        bc[i - 1] += bc[i]
    i = j = ans = 0
    while i < len(a) and j < len(b):
        if a[i] >= b[j]:
            j += 1
            continue
        ans += bc[j] - len(set(c) & set(2 * _b - a[i] for _b in b[j:]))
        i += 1
    return ans


def solve(string):
    n, s = string.split()
    r, g, b = [], [], []
    for i, _s in enumerate(s):
        if _s == "R":
            r.append(i)
        if _s == "G":
            g.append(i)
        if _s == "B":
            b.append(i)
    return str(f(r, g, b) + f(r, b, g) + f(b, r, g) + f(b, g, r) + f(g, r, b) + f(g, b, r))


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# abc162_d.py
import sys
import time
import unittest
from io import StringIO


def resolve():
    int(input())
    S = input()
    r = S.count("R")
    g = S.count("G")
    b = S.count("B")
    sum = r * g * b
    for i in range(len(S)):
        for j in range(i + 1, len(S)):
            k = j + (j - i)
            if k < len(S) and S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                sum -= 1
    print(sum)


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 checkTLE(self, input):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin

    def debugIO(self, input):
        stdin = sys.stdin
        sys.stdin = StringIO(input)
        resolve()
        sys.stdin = stdin

    def setUp(self):
        self.startTime = time.time()

    def tearDown(self):
        t = time.time() - self.startTime
        print('%s: %.3f' % (self.id(), t), flush=True)

    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)


if __name__ == "__main__":
    if "IS_LOCAL" in locals():
        suite = unittest.TestLoader().loadTestsFromTestCase(TestClass)
        unittest.TextTestRunner(verbosity=0).run(suite)
    else:
        resolve()

Submission status: Accepted

Solution code:
import sys
from collections import Counter


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(20000000)

MOD = 10 ** 9 + 7
INF = float("inf")


def main():
    N = int(input())
    S = input()
    C = Counter(S)
    r = C.get("R",0)
    g = C.get("G",0)
    b = C.get("B",0)
    answer = r * g * b
    for left in range(N):
        for mid in range(left + 1, N):
            right = mid * 2 - left
            if right < N:
                if S[right] != S[mid] and S[mid] != S[left] and S[right] != S[left]:
                    answer -= 1
    print(answer)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


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

    nr = 0
    ng = 0
    nb = 0
    for s in S:
        if s == "R":
            nr += 1
        elif s == "G":
            ng += 1
        else:
            nb += 1

    ans = nr * ng * nb
    for left in range(N - 2):
        for center in range(left + 1, N - 1):
            right = center + (center - left)
            if right < N:
                if (
                    S[right] != S[left]
                    and S[left] != S[center]
                    and S[center] != S[right]
                ):
                    ans -= 1

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

a = [0]*n

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

cnt = [0]*3

for i in range(n):
    cnt[a[i]] += 1

ans = 1

for i in range(3):
    ans *= cnt[i]

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

Submission status: Accepted

Solution code:
n=int(input())
s=input()
from collections import Counter
l=Counter()
l["R"]=[]
l["G"]=[]
l["B"]=[]
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: Accepted

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

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

all_comb = r*g*b

reduce_cnt = 0
for i in range(n):
    for k in range(i+2,n):
        if (i+k)%2 == 1: continue
        j = (i+k)//2
        if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]: reduce_cnt += 1

print(all_comb-reduce_cnt)

Submission status: Accepted

Solution code:
import collections
def main():
    N = int(input())
    S = input()
    # (1)Si,Sj,SkはRGB1つずつからなる
    # (2)j-i != k-j
    # (1) - (1)and!(2) を出す方針

    # (1)and!(2)を満たす場合の数
    # (1)を満たすパターン数r*g*b
    c1 = collections.Counter(S)
    meet1 = c1.get("R",0)*c1.get("G",0)*c1.get("B",0)
    # !(2)を満たすものを全探索して(1)を満たせばパターン数を+1する
    notmeet2 = 0
    for i in range(0,N-2):
        for j in range(i+1, N-1):
            k = 2*j-i
            if k<=N-1:
                if S[i]!=S[j] and S[i]!=S[k] and S[j]!=S[k]:
                    notmeet2 += 1
    print(meet1 - notmeet2)

main()

Submission status: Accepted

Solution code:
from math import ceil

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

r = 0
g = 0
b = 0

for s in S:
    if s == 'R':
        r += 1
    elif s == 'G':
        g += 1
    elif s == 'B':
        b += 1
res = r*g*b

for i in range(N):
    for j in range(1, ceil((N-i)/2)):
        if S[i] != S[i+j] and S[i+j] != S[i+2*j] and S[i] != S[i+2*j]:
            res -= 1
print(res)

Submission status: Accepted

Solution code:
#組み合わせ4000C3=10,658,668,000なので全探索は無理

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

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

paircount_without_regulation = r*g*b

#j−i ≠ k−jの条件に引っかかるペア数を数える。
i = 0
j = 1
k = 2
between_counter = 1
regulation_counter = 0

while k <= n-1:
    while k <= n-1:
        if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
            regulation_counter += 1
        i += 1
        j += 1
        k += 1

    i = 0
    j = 1
    k = 2
    j += between_counter
    k += between_counter*2
    between_counter += 1
    #print(between_counter)

result = paircount_without_regulation - regulation_counter

print(result)

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 - 1):
    for j in range(min(i + 1, n - i)):
        if s[i] != s[i + j] and s[i] != s[i - j] and s[i + j] != s[i - j]:
            ans -= 1

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
S=str(input())
S=list(S)
ans=S.count('R')*S.count('G')*S.count('B')
for i in range(N): #判定を簡単にするためR:=1,G:=2,B:=4と置き換えておく
    if S[i]=='R':
        S[i]=1
    elif S[i]=='G':
        S[i]=2
    elif S[i]=='B':
        S[i]=4
for i in range(0,N-2):
    for j in range(i+1,N-1):
        k=(j-i)+j
        if k>N-1:
            continue
        if S[i]+S[j]+S[k] == 7 :
            ans -= 1
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
l=list(input())
r=0
g=0
b=0
for i in range(N):
    if l[i]=="R":
        r+=1
    if l[i]=="G":
        g+=1
    if l[i]=="B":
        b+=1
m=r*g*b
n=0
for i in range(N-1):
    for k in range(i+2,N+1,2):
        if k==N:
            break
        if l[i]==l[k]:
            n+=0
        else:
             if k==N-1:
                if l[int((k+i)/2)]!=l[k] and l[int((k+i)/2)]!=l[i]:
                      n+=1
                      break
                else:
                    break
             else:
                if l[int((k+i)/2)]!=l[k] and l[int((k+i)/2)]!=l[i]:
                      n+=1
m=m-n
print(m)

Submission status: Accepted

Solution code:
n=int(input())
s=str(input())
r=0
g=0
b=0
for x in range (n):
    if s[x]=='R':
        r+=1
    elif s[x]=='G':
        g+=1
    else:
        b+=1
rgb=r*g*b

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
s=str(input())
r=0
g=0
b=0
for x in range (n):
    if s[x]=='R':
        r+=1
    elif s[x]=='G':
        g+=1
    else:
        b+=1
rgb=r*g*b

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

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:
            if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
                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")
 
 
count=R*G*B
for i in range(0,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]:
            count=count-1
 
print(count)

Submission status: Accepted

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

nR = [0]*n
nG = [0]*n
nB = [0]*n
if s[-1] == 'R':
    nR[-1] = 1
elif s[-1] == 'G':
    nG[-1] = 1
elif s[-1] == 'B':
    nB[-1] = 1
for i in range(n-2, -1, -1):
    if s[i] == 'R':
        nR[i] = nR[i+1] + 1
        nG[i] = nG[i+1]
        nB[i] = nB[i+1]
    elif s[i] == 'G':
        nR[i] = nR[i+1]
        nG[i] = nG[i+1] + 1
        nB[i] = nB[i+1]
    elif s[i] == 'B':
        nR[i] = nR[i+1]
        nG[i] = nG[i+1]
        nB[i] = nB[i+1] + 1

cnt = 0
for i in range(n):
    for j in range(i, n-1):
        if s[i] != s[j]:
            f = 0
            if 2*j - i < n:
                if s[i] != s[2*j-i] and s[j] != s[2*j-i]:
                    f = 1

            if s[i] != 'R' and s[j] != 'R':
                cnt += nR[j] - f
            elif s[i] != 'G' and s[j] != 'G':
                cnt += nG[j] - f
            elif s[i] != 'B' and s[j] != 'B':
                cnt += nB[j] - f

print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
S=input()
R=0
G=0
B=0
for s in S:
    if s=='R':
        R+=1
    elif s=='G':
        G+=1
    elif s=='B':
        B+=1
r=0
g=0
b=0
l=len(S)
mix=0
for i in range(l):
    for j in range(i,l):
        k = 2*j-i
        if k >= l:
            break
        #print(i,j,k)
        if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
            mix+=1
            
print(R*G*B-mix)

Submission status: Accepted

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

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

count = r * g * b

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

print(count)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
R=S.count("R")
G=S.count("G")
B=S.count("B")


count=R*G*B
for i in range(0,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]:
            count=count-1

print(count)

Submission status: Accepted

Solution code:

def main():
    import sys
    input = sys.stdin.readline
    #n = int(input())
    #l = list(map(int, input().split()))
    '''
    A=[]
    B=[]
    for i in range():
        a, b = map(int, input().split())
        A.append(a)
        B.append(b)'''

    n=int(input())
    s=input().rstrip('\n')
    a=0
    b=0
    c=0
    for i in range(n):
        if s[i]=="R":
            a+=1
        elif s[i]=="G":
            b+=1
        elif s[i]=="B":
            c+=1
    num=a*b*c
    #print(num)
    for i in range(n-2):
        for j in range(1,n//2+1):
            if i+2*j<n:
                if s[i]!=s[i+j] and s[i+j]!=s[i+2*j] and s[i]!=s[i+2*j]:
                    num-=1
    print(num)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def S(): return input()


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def debug(table, *args):
    ret = []
    for name, val in table.items():
        if val in args:
            ret.append('{}: {}'.format(name, val))
    print(' | '.join(ret), file=sys.stderr)


yn = {False: 'No', True: 'Yes'}
YN = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**19
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False
# show_flg = True


def c(i, j, k):
    return j - i != k - j


def main():
    N = I()
    s = S()
    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

    all_c = r * g * b
    sub = 0
    test = 0

    for i in range(N):
        for j in range(i+1, N):
            k = (j - i) + j
            if k >= N or s[i] == s[j] or s[k] == s[i] or s[j] == s[k]:
                continue
            # 全部異なる、等差である
            sub += 1

    # 全部異なる、等差でないを求める
    print(all_c - sub)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import collections
N = int(input())
S = list(input())
C = collections.Counter(S)
ans = C['R'] * C['G'] * C['B']
for i in range(1, N - 1):
    b = S[i]
    ans -= sum(
        a != b and b != c and c != a for a, c in zip(S[i - 1::-1], S[i + 1:]))
print(ans)

Submission status: Accepted

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

    sum_count = s.count('R') * s.count('G') * s.count('B')

    r_list = set()
    g_list = set()
    b_list = set()

    for i in range(n):
        if s[i] == 'R':
            r_list.add(i)
        elif s[i] == 'G':
            g_list.add(i)
        elif s[i] == 'B':
            b_list.add(i)

    for r in r_list:
        for g in g_list:
            L = g - r
            if g + L in b_list:
                sum_count -= 1
            if r - L in b_list:
                sum_count -= 1
            if L % 2 == 0 and L//2 + r in b_list:
                sum_count -= 1

    print(sum_count)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# i, j, k

# N * (N - 1) * (N - 2)
# 4000^3

# 4
# RRGB

# B (k=4)
# G (j=3)
# R (i=1)

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

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

    return count

def resolve_v2(N, S):
    count = 0

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

            for k in range(j+1, N):
                if S[i] == S[k] or S[j] == S[k] or k-j == j-i:
                    continue

                count += 1

    return count

# R GBR G BGRGRGRGR

# k - j = j - i
# k = 2 * j - i

def resolve_v3(N, S):
    r, g, b = 0, 0, 0
    for i in range(N):
        if S[i] == 'R':
            r += 1
        elif S[i] == 'G':
            g += 1
        elif S[i] == 'B':
            b += 1

    count = r * g * b

    for i in range(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[j] != S[k] and S[i] != S[k]:
                count -= 1

    return count


if __name__ == '__main__':
    
    count = resolve_v3(N, S)
    print(count)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
#RGBそれぞれのペアを1個ずつ取ったあと、等間隔なやつを排除する

R=[]
G=[]
B=[]
for n,s in enumerate(S):
    if s=="R":
        R.append(n)
    elif s=="G":
        G.append(n)
    else:
        B.append(n)

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

for i in range(1,N//2 + 1):
    for s in range(N -i*2):#N=100,i=3 93,96,99
        if S[s]!=S[s+i] and S[s+i]!=S[s+2*i] and S[s] != S[s+2*i]:
            ans -= 1
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
S = input()

cnt = defaultdict(int)
for i in range(N):
    s = S[i]
    cnt[s] += 1

ans = cnt["R"] * cnt["G"] * cnt["B"]

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

print(ans)