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# (Mono-mcs 6.8.0.105) (judge verdict: Accepted)

using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;

namespace atcorder2
{
    class Program
    {
        public static long Read()
        {
            return long.Parse(Console.ReadLine());
        }

        public static long[] Reads()
        {
            return Console.ReadLine().Split().Select(long.Parse).ToArray();
        }


        public static long gcd(long x,long y)
        {
            if (y < x) gcd(y, x);
            while (y != 0)
            {
                var z = x % y;
                x = y;
                y = z;
            }
            return x;
        }
        static void Main(string[] args)
        {
            var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
            Console.SetOut(sw);

            var n = Read();
            var s = Console.ReadLine();
            long r = 0;
            long g = 0;
            long b = 0;
            long ans = 0;
            for (int i = 0; i < n; i++)
            {
                if (s[i] == 'R') r++;
                if (s[i] == 'G') g++;
                if (s[i] == 'B') b++;
            }
            for (int i = 0; i < n; i++)
            {
                if (s[i] == 'R') { r--; ans += b * g; }
                if (s[i] == 'G') { g--; ans += r * b; }
                if (s[i] == 'B') { b--; ans += r * g; }
            }
            for (int i = 0; i <n; i++)
            {
                for (int j = 1; j <n/2.0; j++)
                {
                    if (i + 2 * j < n && ((s[i] == 'R' && s[i + j] == 'G' && s[i + 2 * j] == 'B') || (s[i] == 'R' && s[i + j] == 'B' && s[i + 2 * j] == 'G')))
                        ans--;
                    if (i + 2 * j < n&&((s[i] == 'B' && s[i + j] == 'R' && s[i + 2 * j] == 'G') || (s[i] == 'B' && s[i + j] == 'G' && s[i + 2 * j] == 'R')))
                        ans--;
                    if (i + 2 * j < n&&((s[i] == 'G' && s[i + j] == 'R' && s[i + 2 * j] == 'B') || (s[i] == 'G' && s[i + j] == 'B' && s[i + 2 * j] == 'R')))
                        ans--;
                }
            }
            
            Console.WriteLine(ans);





            Console.Out.Flush();
        }
    }
}