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 C (Clang 10.0.0) (judge verdict: Accepted)

#include <stdio.h>

int check(char a, char b, char c)
{
    if (a != b && a != c && b != c) {
        return 1;
    }
    return 0;
}

int main(void)
{
    int N;
    char S[4001];
    scanf("%d", &N);
    scanf("%s", S);

    // 全てのRGBの順列
    int i;
    int r = 0, g = 0, b = 0;
    for (i = 0; i < N; i++) {
        if (S[i] == 'R') r++;
        if (S[i] == 'G') g++;
        if (S[i] == 'B') b++;
    }
    long long result = r * g * b;

    // 距離が同じでRGBなら除外
    // long long exc = 0;
    // int head, center, tail;
    // for (head = 0; head < N - 2; head++) {
    //     for (center = head + 1; center < N - 1; center++) {
    //         tail = center * 2 - head;
    //         if (tail > N - 1) break;
    //         if (check(S[head], S[center], S[tail])) {
    //             exc++;
    //         }
    //     }
    // }
    int head = 0;
    int center = 1;
    int tail = 2;
    while (head < N - 2) {
        if (check(S[head], S[center], S[tail])) {
            // exc++;
            result--;
        }
        center += 1;
        tail += 2;
        if (tail > N - 1) {
            head++;
            center = head + 1;
            tail = head + 2;
        }
    }

    // printf("%lld\n", exc);
    printf("%lld\n", result);
    return 0;
}