Problem:
Score : 700 points

Problem Statement

Given is a sequence of N digits a_1a_2\ldots a_N, where each element is 1, 2, or 3.
Let x_{i,j} defined as follows:

x_{1,j} := a_j \quad (1 \leq j \leq N)

x_{i,j} := | x_{i-1,j} - x_{i-1,j+1} | \quad (2 \leq i \leq N and 1 \leq j \leq N+1-i)

Find x_{N,1}.

Constraints

2 \leq N \leq 10^6

a_i = 1,2,3 (1 \leq i \leq N)

Input

Input is given from Standard Input in the following format:

N
a_1a_2\ldotsa_N

Output

Print x_{N,1}.

Sample Input 1

4
1231

Sample Output 1

1

x_{1,1},x_{1,2},x_{1,3},x_{1,4} are respectively 1,2,3,1.

x_{2,1},x_{2,2},x_{2,3} are respectively |1-2| = 1,|2-3| = 1,|3-1| = 2.

x_{3,1},x_{3,2} are respectively |1-1| = 0,|1-2| = 1.

Finally, x_{4,1} = |0-1| = 1, so the answer is 1.

Sample Input 2

10
2311312312

Sample Output 2

0

Solution in C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Linq;
using CompLib.Util;

public class Program
{
    private int N;
    private int[] A;


    public void Solve()
    {
        var sc = new Scanner();
        N = sc.NextInt();
        A = sc.NextCharArray().Select(c => c - '0' - 1).ToArray();

        // 答えが奇数になるか?
        // いっこしたが奇数になる
        // 隣合う数の偶奇が違う

        if (IsOne())
        {
            Console.WriteLine(1);
            return;
        }

        // 答えは 0 or 2
        // 1がある 最後まで2を残せない 0

        if (A.Contains(1))
        {
            Console.WriteLine(0);
            return;
        }

        // 答えが2になるか
        // いっこしたが2になる
        // 隣合う2数が 0,2 or 2,0

        if (IsTwo())
        {
            Console.WriteLine(2);
            return;
        }

        Console.WriteLine(0);
    }

    bool IsTwo()
    {
        int[] two = new int[N];
        two[0] = 0;
        for (int i = 1; i < N; i++)
        {
            int cnt = 0;
            int t = i;
            while (t % 2 == 0)
            {
                cnt++;
                t /= 2;
            }

            two[i] = two[i - 1] + cnt;
        }

        int ans = 0;
        for (int i = 0; i < N; i++)
        {
            int cnt = two[N - 1] - (two[i] + two[N - 1 - i]);
            if (A[i] == 2 && cnt == 0) ans++;
            ans %= 2;
        }

        return ans == 1;
    }

    // 答えが1になるか?
    bool IsOne()
    {
        // cnt[i] = N-1 C i = (N-1)! / (N-i-1)! i!
        // N-1に偶数

        // i!は2でn回割り切れる
        int[] two = new int[N];
        two[0] = 0;
        for (int i = 1; i < N; i++)
        {
            int cnt = 0;
            int t = i;
            while (t % 2 == 0)
            {
                cnt++;
                t /= 2;
            }

            two[i] = two[i - 1] + cnt;
        }

        int ans = 0;
        for (int i = 0; i < N; i++)
        {
            int cnt = two[N - 1] - (two[i] + two[N - 1 - i]);
            if (A[i] % 2 == 1 && cnt == 0) ans++;
            ans %= 2;
        }

        return ans == 1;
    }

    public static void Main(string[] args) => new Program().Solve();
}

namespace CompLib.Util
{
    using System;
    using System.Linq;

    class Scanner
    {
        private string[] _line;
        private int _index;
        private const char Separator = ' ';

        public Scanner()
        {
            _line = new string[0];
            _index = 0;
        }

        public string Next()
        {
            while (_index >= _line.Length)
            {
                _line = Console.ReadLine().Split(Separator);
                _index = 0;
            }

            return _line[_index++];
        }

        public int NextInt() => int.Parse(Next());
        public long NextLong() => long.Parse(Next());
        public double NextDouble() => double.Parse(Next());
        public decimal NextDecimal() => decimal.Parse(Next());
        public char NextChar() => Next()[0];
        public char[] NextCharArray() => Next().ToCharArray();

        public string[] Array()
        {
            _line = Console.ReadLine().Split(Separator);
            _index = _line.Length;
            return _line;
        }

        public int[] IntArray() => Array().Select(int.Parse).ToArray();
        public long[] LongArray() => Array().Select(long.Parse).ToArray();
        public double[] DoubleArray() => Array().Select(double.Parse).ToArray();
        public decimal[] DecimalArray() => Array().Select(decimal.Parse).ToArray();
    }
}