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++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <fstream>
#include <cassert>
#include <cstring>
#include <unordered_set>
#include <unordered_map>
#include <numeric>
#include <ctime>
#include <bitset>
#include <complex>
#include <random>

using namespace std;

#define int long long

int two_in_factorial(int x) {
	int ans = 0;
	for (int i = 2; i <= x; i *= 2) {
		ans += x / i;
	}
	return ans;
}

int get_c(int n, int k) {
	int all = 0;
	all = two_in_factorial(n);
	all -= two_in_factorial(k);
	all -= two_in_factorial(n - k);
	if (all > 0) return 0;
	else return 1;
}

int solve(int n, vector<int> a) {
	int hv = 0;
	for (int i = 0; i < n; i++) {
		if (a[i] == 2) hv = 1;
	}
	if (hv) {
		int ans = 0;
		for (int i = 0; i < n; i++) {
			ans += (a[i]) * (get_c(n - 1, i));
		}
		return ans % 2;
	} else {
		vector<int> bn;
		for (auto t : a) {
			bn.push_back(t == 3);
		}
		int ans = 0;
		for (int i = 0; i < n; i++) {
			ans += (bn[i]) * (get_c(n - 1, i));
		}
		return 2 * (ans % 2);
	}
}

// void proc(vector<int> &a) {
// 	int n = (int)a.size();
// 	vector<int> na(n - 1);
// 	for (int i = 0; i + 1 < n; i++) {
// 		na[i] = abs(a[i] - a[i + 1]);
// 	}
// 	a = na;
// }

// int solves(int n, vector<int> a) {
// 	for (int i = 1; i < n; i++) {
// 		proc(a);
// 	}
// 	return a[0];
// }


signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	int n;
	cin >> n;
	vector<int> a;
	for (int i = 0; i < n; i++) {
		char c;
		cin >> c;
		int z = (c - '0');
		a.push_back(z);
	}
	cout << solve(n, a) << endl;

	// mt19937 rng(3);
	// int n = 20;
	// vector<int> a(n);
	// while (true) {
	// 	int hv = 0;
	// 	for (auto &t : a) {
	// 		t = rng() % 3 + 1;
	// 		hv |= (t == 2);
	// 	}
	// 	// if (!hv) continue;
	// 	if (solve(n, a) != solves(n, a)) {
	// 		cout << "WA" << endl;
	// 		for (auto t : a) {
	// 			cout << t;
	// 		}
	// 		cout << endl;
	// 		exit(0);
	// 	} else {
	// 		// cout << "." << endl;
	// 	}
	// }
}