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: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AtCoders {
	class Program {
		static void Main(string[] args) {
			var inputArray = retreiveLongArray();
			var inputCount = 1;

			// 不正な入力は処理しない
			if (inputArray.Length != inputCount) {
				Console.WriteLine("invalid input");
				return;
			}
			var N = (int)inputArray[0];
			var inputString = Console.ReadLine();

			var numList = new List<int>();

			foreach (var item in inputString) {
				numList.Add(item);
			}
			//numListの数を1,2,3ではなく0,1,2にする
			numList = numList.Select(x => x - 1).ToList();


			//まず偶奇を判断する
			var countComninationMod = new CountComninationMod(N);

			var oddCount = 0;
			for (int i = 0; i < N; i++) {

				//偶数の場合は何もしない
				var number = numList[i];
				if (number % 2 ==0) {
					continue;
				}

				var toujoukaisu = countComninationMod.GetCombinatnionMod(i);
				oddCount += toujoukaisu;
			}

			//奇数の場合は1で終了
			if(oddCount %2 == 1) {
				Console.WriteLine(1);
				return;
			}

			//入力に1が含まれている場合は、2にならない。
			if (numList.Contains(1)) {
				Console.WriteLine(0);
				return;
			}

			//偶奇判断と同じ考えで処理する
			var num2Count = 0;
			for (int i = 0; i < N; i++) {

				//0の場合は何もしない
				var number = numList[i];
				if (number == 0) {
					continue;
				}

				var toujoukaisu = countComninationMod.GetCombinatnionMod(i);
				num2Count += toujoukaisu;
			}

			if(num2Count % 2 == 0) {
				Console.WriteLine(0);
				return;
			}

			Console.WriteLine(2);
			return;
		}

		/// <summary>
		/// スペースで区切られた文字列配列の入力を受け取る
		/// </summary>
		/// <returns></returns>
		private static string[] retreiveStringArray() {
			var inputArray =  Console.ReadLine().Split(' ');
			return inputArray;
		}

		/// <summary>
		/// スペースで区切られた整数配列の入力を受け取る
		/// </summary>
		/// <returns></returns>
		private static long[] retreiveLongArray() {
			var inputArray = Console.ReadLine().Split(' ');
			var longArray = inputArray.Select(s => long.Parse(s));
			return longArray.ToArray();
		}
	}

	/// <summary>
	/// n-1Cp を求めるためのクラス
	/// </summary>
	public class CountComninationMod {

		/// <summary>
		/// Nこの組み合わせ
		/// </summary>
		public int _N;
		
		/// <summary>
		/// i!が2で何回割り切れるかを保持する
		/// </summary>
		public readonly int[] _two;

		/// <summary>
		/// コンストラクタ
		/// </summary>
		/// <param name="n">n個の要素</param>
		public CountComninationMod(int n) {
			_N = n;

			_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;
			}

		}


		/// <summary>
		/// N-1こからp個を選んだ場合の組み合わせの数を,2で割ったあまりを返す
		/// </summary>
		/// <param name="p"></param>
		/// <returns></returns>
		public int GetCombinatnionMod(int p) {

			var cnt = _two[_N-1] - (_two[p] + _two[_N -1- p]);

			//分母と分子の2で割れる数が同じ=奇数
			if (cnt == 0) {
				return 1;
			}
			else {//分子の2で割れる数が分母より大きい=偶数
				return 0;
			}

		}


	}
}