Problem p02714 (python)

Fixed submission:
import math
import queue
from collections import defaultdict
 
def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()
def factorization(n):
	res = []
	if n%2==0:
		res.append(2)
	for i in range(3,math.floor(n//2)+1,2):
		if n%i==0:
			c = 0
			for j in res:
				if i%j==0:
					c=1
			if c==0:
				res.append(i)
	return res
def fact2(n):
	p = factorization(n)
	res = []
	for i in p:
		c=0
		z=n
		while 1:
			if z%i==0:
				c+=1
				z/=i
			else:
				break
		res.append([i,c])
	return res
def fact(n):#階乗
	ans = 1
	m=n
	for _i in range(n-1):
		ans*=m
		m-=1
	return ans
def comb(n,r):#コンビネーション
	l = min(r,n-r)
	m=n
	u=1
	for _i in range(l):
		u*=m
		m-=1
	return u//fact(l)
def printQueue(q):
	r=q
	ans=[0]*r.qsize()
	for i in range(r.qsize()-1,-1,-1):
		ans[i] = r.get()
	print(ans)
def binary_search(li,item):#二分探索
	left,right = 0, len(li)
	while left<=right:
		mid = (left+right)//2
		if li[mid]==item:
			return mid
		elif li[mid]<item:
			left = mid+1
		else:
			right = mid-1
	return -1

N = readInt()
S = readChar()
rgb = {"R":[],"G":[],"B":[]}
for i in range(N):
	rgb[S[i]].append(i)

c=s=0
c=len(rgb["R"])*len(rgb["G"])*len(rgb["B"])
for i in range(N):
	for j in range(i+1,N):
		k = 2*j-i
		if k>=N:
			continue
		if S[i]!=S[j] and S[i]!=S[k] and S[k]!=S[j]:
			s+=1

ans=c-s
print(ans)

Buggy submission (verdict: Runtime Error):
import math
import queue
from collections import defaultdict
 
def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()
def factorization(n):
	res = []
	if n%2==0:
		res.append(2)
	for i in range(3,math.floor(n//2)+1,2):
		if n%i==0:
			c = 0
			for j in res:
				if i%j==0:
					c=1
			if c==0:
				res.append(i)
	return res
def fact2(n):
	p = factorization(n)
	res = []
	for i in p:
		c=0
		z=n
		while 1:
			if z%i==0:
				c+=1
				z/=i
			else:
				break
		res.append([i,c])
	return res
def fact(n):#階乗
	ans = 1
	m=n
	for _i in range(n-1):
		ans*=m
		m-=1
	return ans
def comb(n,r):#コンビネーション
	l = min(r,n-r)
	m=n
	u=1
	for _i in range(l):
		u*=m
		m-=1
	return u//fact(l)
def printQueue(q):
	r=q
	ans=[0]*r.qsize()
	for i in range(r.qsize()-1,-1,-1):
		ans[i] = r.get()
	print(ans)
def binary_search(li,item):#二分探索
	left,right = 0, len(li)
	while left<=right:
		mid = (left+right)//2
		if li[mid]==item:
			return mid
		elif li[mid]<item:
			left = mid+1
		else:
			right = mid-1
	return -1

N = readInt()
S = readChar()
rgb = {"R":[],"G":[],"B":[]}
for i in range(N):
	rgb[S[i]].append(i)

c=s=0
c=len(rgb["R"])*len(rgb["G"])*len(rgb["B"])
for i in range(1,N):
	for j in range(i+1,N):
		k = 2*j-i
		if k<=N-1:
			continue
		if S[i]==S[j] and S[i]==S[k]:
			s+=1

ans=c-s
print(ans)

Buggy submission (verdict: Runtime Error):
import math
import queue
from collections import defaultdict
 
def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()
def factorization(n):
	res = []
	if n%2==0:
		res.append(2)
	for i in range(3,math.floor(n//2)+1,2):
		if n%i==0:
			c = 0
			for j in res:
				if i%j==0:
					c=1
			if c==0:
				res.append(i)
	return res
def fact2(n):
	p = factorization(n)
	res = []
	for i in p:
		c=0
		z=n
		while 1:
			if z%i==0:
				c+=1
				z/=i
			else:
				break
		res.append([i,c])
	return res
def fact(n):#階乗
	ans = 1
	m=n
	for _i in range(n-1):
		ans*=m
		m-=1
	return ans
def comb(n,r):#コンビネーション
	l = min(r,n-r)
	m=n
	u=1
	for _i in range(l):
		u*=m
		m-=1
	return u//fact(l)
def printQueue(q):
	r=q
	ans=[0]*r.qsize()
	for i in range(r.qsize()-1,-1,-1):
		ans[i] = r.get()
	print(ans)
def binary_search(li,item):#二分探索
	left,right = 0, len(li)
	while left<=right:
		mid = (left+right)//2
		if li[mid]==item:
			return mid
		elif li[mid]<item:
			left = mid+1
		else:
			right = mid-1
	return -1

N = readInt()
S = readChar()
rgb = {"R":[],"G":[],"B":[]}
for i in range(N):
	rgb[S[i]].append(i)

c=s=0
c=len(rgb["R"])*len(rgb["G"])*len(rgb["B"])
for i in range(1,N):
	for j in range(i+1,N):
		k = 2*j-i
		if S[i]==s[j] and s[i]==s[k]:
			s+=1

ans=c-s
print(ans)