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

Solution in Julia (1.4.0) (judge verdict: Wrong Answer)

import Base: iterate, length

struct Combination{T}
    arr::Array{T,1}
    picks::Int
end

function length(combi::Combination)
    m = length(combi.arr)
    n = combi.picks
    
    result = 1
    for i in 1:n
        result *= (m - i + 1)
        result = div(result, i)
    end
    result
end

function nextstate(arraysize::Int, state::Array{Int})
    if state[1] == arraysize - length(state) + 1
        return [0]
    end

    if state[end] < arraysize
        state[end] += 1
        return state
    end

    targetindex = length(state) - 1

    while targetindex > 0
        if state[targetindex] + 1 === state[targetindex+1]
            targetindex -= 1
        else
            state[targetindex] += 1
            for i in (targetindex+1):length(state)
                state[i] = state[i-1] + 1
            end
            return state
        end
    end
    # ここに来ることは無いはず
    return state
end

function iterate(combi::Combination, state)
    if state == [0]
        return nothing
    end
    arraysize = length(combi.arr)
    value = combi.arr[state]
    state = nextstate(arraysize, state)
    return (value, state)
end

function iterate(combi::Combination)
    if combi.picks > length(combi.arr)
        return nothing
    end
    if combi.picks === 0
        return (typeof(combi.arr)[], [0])
    end
    state = collect(1:(combi.picks))
    value = combi.arr[state]
    arraysize = length(combi.arr)
    return (value, nextstate(arraysize, state))
end

function main()
    n = parse(Int, readline())
    s = readline()
    array = collect(1:n)
    count = 0
    counts = [Int[], Int[], Int[]] #RGB
    x,y,z = 0,0,0
    for i in 1:n
        c = s[i]
        if c == 'R'
            push!(counts[1], i)
            x += 1
        elseif c == 'G'
            push!(counts[2], i)
            y += 1
        else
            push!(counts[3], i)
            z += 1
        end
    end

    for i in x
        for j in y
            for k in z
                if j - i == k - j
                    #カウントしない
                else
                    count += 1
                end
            end
        end
    end     

    println(count)
end

main()