Problem:


Score : 400 points

Problem Statement

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

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

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

j - i \neq k - j.

Constraints

1 \leq N \leq 4000

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

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of triplets in question.

Sample Input 1

4
RRGB

Sample Output 1

1

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

Sample Input 2

39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB

Sample Output 2

1800



Sample input:
4
RRGB


Sample output:
1

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
typedef long long ll;
#define mod 1000000007
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vll vector <ll>
#define vii vector <int>
#define vpii vector <pii>
#define vpll vector <pll>
#define ff first
#define ss second
#define matrix vector <vll>
#define all(v) v.begin(),v.end()
#define PQ priority_queue
using namespace std;
int main()
{
	//ios::sync_with_stdio(0);
	//cin.tie(0);
	int n; string s;
	cin >> n;
	cin >> s;
	vii v(n);
	for(int i = 0; i < n; i++){
		if(s[i] == 'R') v[i] = 0;
		if(s[i] == 'G') v[i] = 1;
		if(s[i] == 'B') v[i] = 2;
	}
	vii cnt(3);
	ll ans = 0;
	for(int i = 0; i < n-2; i++){
		vii tcnt = cnt;
		cnt[v[i]]++;
		cnt[v[i+1]]++;
		for(int k = i+2; k < n; k++){
			cnt[v[k]]++;
			if(v[i] != v[k]){
				int col = 3- v[i] - v[k];
				ans+=cnt[col];
				if((k+i)%2 == 0 && v[(k+i)/2] == col) ans--;
			}
		}
		cnt = tcnt;
	}
	cout << ans << "\n";
	return 0;
}

Submission status: Runtime Error

Solution code:
import itertools as it

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

r_indeces = [i for i in s if i == 'R']
g_indeces = [i for i in s if i == 'G']
b_indeces = [i for i in s if i == 'B']

indeces = [sorted(list(i))
           for i in it.product(r_indeces, g_indeces, b_indeces)]
indeces = [i for i in indeces if i[1] - i[0] != i[2] - i[1]]
print(len(indeces))

Submission status: Runtime Error

Solution code:
from collections import Counter
from operator import mul
n = int(input())
s = input()
c = Counter(s)
if n < 3:
    print(0)
    exit()

ans = 0
for i in range(n):
    x = s[i]
    ans += mul(*[v for k, v in c.items()if k != x])
    c[x] -= 1
    for j in range(i+1, n):
        y = s[j]
        if x == y:
            continue
        idx_diff = j-i
        if n <= j+idx_diff:
            continue
        z = s[j+idx_diff]
        if x != z and y != z:
            ans -= 1
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
RGB = ['R', 'G', 'B']

import collections
dct = dict(collections.Counter(S))
#print(dct)

ans = 0
for i in range(0, N-1):
    for j in range(i+1, N):
        if S[i] != S[j]:
            for c in RGB:
                if c != S[i] and c != S[j]:
                    cnt = 0
                    if 0 <= 2 * j - i < N and S[2 * j - i] == c:
                        cnt += 1
                    if N > 2 * i - j >= 0 and S[2 * i - j] == c:
                        cnt += 1
                    if i % 2 == j % 2:
                        if 0 <= (i + j) // 2 < N and S[(i + j) // 2] == c:
                            cnt += 1
                    ans += dct[c] - cnt
                    #print(i, j, dct[c]-cnt)
#print(ans)
print(ans // 3)

Submission status: Runtime Error

Solution code:
a = int(input())
b = input()

import math

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


R = 0
G = 0
B = 0
for i in b:
    if i == "R":
        R += 1
    elif i == "G":
        G += 1
    else:
        B += 1

res = 0
for i in range(0, len(b)):
    for k in range(i + 1, len(b)):
        if (i + k) % 2 == 0:
            if b[i] != b[k] and b[i] != b[(i + k) //2] and b[k] != b[(i + k) //2]:
                res += 1

print(nCr(R, 1) * nCr(G, 1) * nCr(B, 1) - res)

Submission status: Runtime Error

Solution code:
from collections import Counter
N = int(input())
S = input()
c = Counter(S)
#print (c.most_common())
ls = list(c.values())
ans = ls[0] * ls[1] * ls[2]


for i in range(N):
    for j in range(1,N):
        if (i-j<0 or i+j==N):
            break
        #print (i,j)
        s = set([S[i-j],S[i],S[i+j]])
        if len(s)==3:
            ans -= 1
print (ans)

Submission status: Runtime Error

Solution code:
N,S =input().split()
N = int(N)
A = 0
R = []
G = []
B = []
for i in range(len(S)):
  if S[i] == "R":
    R.append(i+1)
  elif S[i] == "G":
    G.append(i+1)
  else:
    B.append(i+1)
for r in R:
  for g in G:
    for b in B:
      C = sorted([r,g,b])
      if C[1] - C[0] != C[2] - C[1]:
        A += 1
print(A)

Submission status: Runtime Error

Solution code:
n=int(input())
s=list(input())
rlist=[]
glist=[]
blist=[]
ans=0
for i in range(n):
    if s[i]=="R":
        rlist.append(i)
    elif s[i]=="G":
        glist.append(i)
    else:
        blist.append(i)
if rlist[0] < glist[0] and glist[0]< blist[0]:
    for i in rlist:
        for j in glist:
            for k in blist:
                tmp=[]
                tmp.append(i)
                tmp.append(j)
                tmp.append(k)
                tmp.sort()
                if tmp[1]-tmp[0] != tmp[2]-tmp[1]:
                    ans+=1
                else:
                    continue
elif glist[0] < rlist[0] and rlist[0]< blist[0]:
    for i in glist:
        for j in rlist:
            for k in blist:
                tmp=[]
                tmp.append(i)
                tmp.append(j)
                tmp.append(k)
                tmp.sort()
                if tmp[1]-tmp[0] != tmp[2]-tmp[1]:
                    ans+=1
                else:
                    continue
elif glist[0] < blist[0] and blist[0]< rlist[0]:
    for i in glist:
        for j in blist:
            for k in rlist:
                tmp=[]
                tmp.append(i)
                tmp.append(j)
                tmp.append(k)
                tmp.sort()
                if tmp[1]-tmp[0] != tmp[2]-tmp[1]:
                    ans+=1
                else:
                    continue
elif blist[0] < glist[0] and glist[0]< rlist[0]:
    for i in blist:
        for j in glist:
            for k in rlist:
                tmp=[]
                tmp.append(i)
                tmp.append(j)
                tmp.append(k)
                tmp.sort()
                if tmp[1]-tmp[0] != tmp[2]-tmp[1]:
                    ans+=1
                else:
                    continue
elif blist[0] < rlist[0] and rlist[0]< glist[0]:
    for i in blist:
        for j in rlist:
            for k in glist:
                tmp=[]
                tmp.append(i)
                tmp.append(j)
                tmp.append(k)
                tmp.sort()
                if tmp[1]-tmp[0] != tmp[2]-tmp[1]:
                    ans+=1
                else:
                    continue
else:
    for i in rlist:
        for j in blist:
            for k in glist:
                tmp=[]
                tmp.append(i)
                tmp.append(j)
                tmp.append(k)
                tmp.sort()
                if tmp[1]-tmp[0] != tmp[2]-tmp[1]:
                    ans+=1
                else:
                    continue
print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
s=input()
r=[]
g=[]
b=[]
for i in range(n):
  if s[i]=='R':
    r.append(i)
  elif s[i]=='G':
    g.append(i)
  else:
    b.append(i)
ans=len(r)*len(g)*len(b)
for i in g:
  for j in b:
    i+j=a
    if a%2==0 and (a//2 in r):
      ans-=1
for i in r:
  for j in b:
    i+j=a
    if a%2==0 and (a//2 in g):
      ans-=1
for i in g:
  for j in r:
    i+j=a
    if a%2==0 and (a//2 in b):
      ans-=1
print(ans)

Submission status: Runtime Error

Solution code:
from collections import Counter


def main(N=None, S=None):
    if not N or not S:
        N = int(input())
        S = int(input())
    c = Counter(S)
    ans = c["R"]*c["G"]*c["B"]
    for hop in range(1, N//2):
        for start in range(N):
            i = start
            j = start + hop
            k = start + hop*2
            if k >= N:
                break
            if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                ans -= 1
            # print(i, j, k, ans)
    print(ans)
    return ans


if __name__ == "__main__":
    # assert main(4, "RRGB") == 1
    # assert main(39, "RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB") == 1800
    main()

Submission status: Runtime Error

Solution code:
import itertools

x1 = input()
x2 = input()
RGB = {"R", "G", "B"}
_sum = 0
for i, rgb in zip(itertools.combinations(range(x1), 3), itertools.combinations(x2, 3)):
    if set(rgb) == RGB and (i[1] - i[0]) != (i[2] - i[1]):
        _sum += 1
print(_sum)

Submission status: Runtime Error

Solution code:
def solve(n, s):

    r_list = [i for i in range(n) if s[i] == "R"]
    g_list = [i for i in range(n) if s[i] == "G"]
    # b_list = [i for i in range(n) if s[i] == "B"]
    b_dict = {i: 1 for i in range(n) if s[i] == "B"}

    res = len(r_list) * len(g_list) * len(b_list)
    for i in r_list:
        for j in g_list:
            # candidate
            m, n = min(i, j), max(i, j)
            if n + (n - m) in b_dict.keys():
                res -= 1
            if m - (n - m) in b_dict.keys():
                res -= 1
            if (n - m) % 2 == 0 and (n + m) // 2 in b_dict.keys():
                res -= 1

    return res


def main():
    n = int(input())
    s = input()
    res = solve(n, s)
    print(res)


def test():
    assert solve(4, "RRGB") == 1
    assert solve(39, "RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB") == 1800


if __name__ == "__main__":
    test()
    main()

Submission status: Runtime Error

Solution code:
x1 = input()
x2 = input()
RGB = {"R", "G", "B"}
_sum = 0
for i, rgb in zip(itertools.combinations(range(x1), 3), itertools.combinations(x2, 3)):
    if set(rgb) == RGB and (i[1] - i[0]) != (i[2] - i[1]):
        _sum += 1
print(_sum)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
from collections import Counter
from operator import mul
n = int(input())
s = input()
c = Counter(s)
ans = 0
for i in range(n):
    ans += mul(*[v for k, v in c.items()if k != s[i]])
    c[s[i]] -= 1
    for j in range(i+1, n):
        x = s[i]
        y = s[j]
        if x == y:
            continue
        idx_diff = j-i
        if n <= j+idx_diff:
            continue
        z = s[j+idx_diff]
        if x != z and y != z:
            ans -= 1
print(ans)

Submission status: Runtime Error

Solution code:
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)
ans = 0
for i in range(K):
    for j in range(K):
        for k in range(K):
            ans += gcd(i+1,j+1,k+1)
            #1-Kまで
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
s = str(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)
c = len(r)*len(g)*len(b)
for ri in r:
  for gi in g:
    for bi in b:
      if 3*(max(ri,gi,bi)+min(ri,gi,bi)) == 2*sum(ri,gi,bi):
        c -= 1
print(c)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)  # 再帰関数の再帰の深さを設定
to_index = lambda x: int(x) - 1  # 入力した数字に1を引いたものを返す
print_list_in_2D = lambda x: print(*x, sep="\n")  # リストの要素を改行を挟んで表示する関数

# 入力を整数に変換して受け取る
def input_int():
    return int(input())

def map_int_input():
    return map(int, input())

MII = map_int_input

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

def MII_to_index():
    return map(to_index, input())

def MII_split_to_index():
    return map(to_index, input().split())

# 入力全てを整数に変換したものの配列を受け取る
def list_int_inputs():
    return list(map(int, input()))

LII = list_int_inputs

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

# 2次元リスト化
def LII_2D(rows_number):
    return [LII() for _ in range(rows_number)]

def LII_split_2D(rows_number):
    return [LII_split() for _ in range(rows_number)]

N = input_int()
S = input()

if N < 3:
    print(0)
    return
count = 0
for i in range(N - 2):
    for j in range(i + 1, N - 1):

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

Submission status: Runtime Error

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

sum = 0

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

Submission status: Runtime Error

Solution code:
import collections

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

c = collections.Counter(S)
if 'R' in c.keys() and 'G' in c.keys() and 'B' in c.keys():
    ans = c['R'] * c['G'] * c['B']

    for x in range(N//2 - 1):
        for i in range(N - 2*(x+1)):
            if S[i] != S[i+x+1] and S[i] != S[i+2*(x+1)] and S[i+x+1] != S[i+2*(x+1)]:
                ans -= 1
print(ans)

Submission status: Runtime Error

Solution code:
n= int(input())
s = input()
ans = 0
li_r = []
li_g = []
li_b = []
for i in range(n):
  if li[i] == "R":
    li_r.append(i+1)
  elif li[i] == "G":
    li_g.append(i+1)
  elif li[i] == "B":
    li_b.append(i+1)
for i in li_r:
  for j in li_g:
    if 2*j - i in li_b:
      ans += len(li_b) -1
    else:
      ans += len(li_b)
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
S = str(input())
RGB = [0] * 3
for i in range(N):
  if S[i] == "R":
    RGB[0] += 1
  elif S[i] == "G":
    RGB[1] += 1:
  else:
    RGB[2] += 1
    
ans = RGB[0] * RGB[1] * RGB[2]

for i in range(1, 2000):
  for j in range(1, N + 1):
    if (j + 2 * i) <= N:
      if S[j - 1] != S[j + i - 1]:
        if S[j + 2 * i - 1] != S[j + i - 1]:
          if S[j - 1] != S[j + 2 * i - 1]:
            ans -= 1
            
print(max(ans, 0))

Submission status: Runtime Error

Solution code:
N = int(input())
S = str(input())
cnt = 1
for i in range(1,N+1):
  for j in range(1,N+1):
    if S[i]==S[j]:
      break
    for k in range(1,N+1):
      if S[i]==S[k] or S[j]==S[k]:
        break
      elif j-i == k-j:
        break
      else:
        cnt = cnt+1
print(cnt)

Submission status: Runtime Error

Solution code:
num=int(input())
txt=input()
count=0
for i in range(len(txt)-2):
  a=txt[i]
  for j in range(i+1,len(txt)-1):
    b=txt[j]
    if a==b:
      countinue
    for k in range(j+1,len(txt)):
      c=txt[k]
      if c!=a and c!=b:
        if k-j != j-i:
          count+=1
        else:
          pass
print(count)

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
R = set()
G = set()
B = set()
for i in range(s):
    if s[i] == 'R':
        R.add(i)
    elif s[i] == 'B':
        B.add(i)
    else:
        G.add(i)
ans = 0
for r in R:
    for g in G:
        ans += len(B)
        if 2*r - g in B:
            ans -= 1
        if 2*g - r in B:
            ans -= 1
        if not (r+g)&1 and (r+g)//2 in B:
            ans -= 1
print(ans)

Submission status: Runtime Error

Solution code:
import sys
from collections import *
import heapq
import math
from math import gcd
import bisect
import copy
from itertools import permutations,accumulate,combinations,product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

n=int(input())
s=input()
dic={}
for i in range(n):
    if not s[i] in dic:
        dic[s[i]]=[i]
    else:
        dic[s[i]].append(i)
# print(dic)
se=set(dic["B"])
# print(se)
ans=len(dic["R"])*len(dic["G"])*len(dic["B"])
for i in range(len(dic["R"])):
    for j in range(len(dic["G"])):
        l,r=min(dic["R"][i],dic["G"][j]),max(dic["R"][i],dic["G"][j])
        if r+(r-l) in se:
            ans-=1
        if l-(r-l) in se:
            ans-=1
        if (r-l)%2==0 and l+(r-l)//2 in se:
            ans-=1
print(ans)

Submission status: Runtime Error

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

from collections import Counter
c = Counter(s)
c = list(c.values())
ans = c[0] * c[1] * c[2]
# print(ans)
for k in range(n):
    for a in range(1, n//2 + 1):
        if k + 2 * a >= n:
            continue
        if (s[k] != s[k+a]) and (s[k+a] != s[k + 2 * a]) and (s[k] != s[k + 2*a]):
            ans -= 1

print(ans)

Submission status: Runtime Error

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

sum = 0

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

Submission status: Runtime Error

Solution code:
import sys
input=sys.stdin.readline
n=int(input())
s=list(input())
r=[]
g=[]
b=[]
for i in s:
    if i=='R':
        r.append(i)
    elif i=='G':
        g.append(i)
    else:
        b.append(i)
ans=len(r)*len(g)*len(b)
r=set(r)
g=set(g)
b=set(b)
for i in r:
    for j in g:
        if (i+j)%2==0 and (i+j)//2 in b:
            ans-=1
for i in r:
    for j in b:
        if (i+j)%2==0 and (i+j)//2 in g:
            ans-=1
for i in b:
    for j in g:
        if (i+j)%2==0 and (i+j)//2 in r:
            ans-=1
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
lis = str(input())
r = []
g = []
b = []
for i in range(n):
  if lis[i] == "R":
    r.append(i)
  elif lis[i] == "G":
    g.append(i)
  else:
    b.append(i)
ans = len(r) * len(g) * len(b)
for i in range(n-2):
  for j in range(i+1,n-1):
    k = j + (j-i)
    if (lis[i] != lis[j]) and (lis[j] != lis[k]) and (lis[k] != lis[i]):
      ans -= 1
print(ans)