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 Rust (1.15.1) (judge verdict: Accepted)

#[allow(dead_code)]
fn main() {
    let n = i::u();
    let a: Vec<i32> = i::s()
        .iter()
        .map(|&c| (c as i32) - ('0' as i32) - 1)
        .collect();

    let mut b = false;
    for i in 0..n {
        let a_i = a[i] % 2;
        if a_i == 0 {
            continue;
        }
        let k = lucas_theorem(n - 1, i, 2);
        b = b ^ (k == 1);
    }

    if b {
        println!("{}", 1);
        return;
    }

    for i in 0..n {
        if a[i] == 1 {
            println!("{}", 0);
            return;
        }
    }

    let a: Vec<i32> = a.iter().map(|k| k / 2).collect();
    let mut b = false;
    for i in 0..n {
        let a_i = a[i] % 2;
        if a_i == 0 {
            continue;
        }
        let k = lucas_theorem(n - 1, i, 2);
        b = b ^ (k == 1);
    }

    println!("{}", if b { 2 } else { 0 });
}
#[allow(unused_imports)]
use lucas_theorem::*;

#[allow(dead_code)]
mod lucas_theorem {
    use super::*;
    /// nCr mod p を得る 計算量: O(log n)
    pub fn lucas_theorem(mut n: usize, mut r: usize, p: usize) -> usize {
        if p < 2 {
            return 0;
        }
        let mut ret = 1;
        while n != 0 || r != 0 {
            let (n_mod, r_mod) = (n % p, r % p);
            if n_mod >= r_mod {
                ret *= combination(n_mod, r_mod);
            } else {
                return 0;
            }
            n /= p;
            r /= p;
        }
        ret % p
    }

    fn combination(n: usize, k: usize) -> usize {
        if n < k {
            return 0;
        }
        if std::cmp::min(k, n - k) == 0 {
            1
        } else {
            combination(n - 1, k - 1) * n / k
        }
    }
}

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::{HashMap, HashSet, VecDeque};
#[allow(unused_imports)]
use std::io::*;
#[allow(unused_imports)]
use std::num::*;
#[allow(unused_imports)]
use std::str::*;

#[allow(dead_code)]
mod i {
    use super::*;

    pub fn read<T: FromStr>() -> T {
        stdin()
            .bytes()
            .map(|c| c.unwrap() as char)
            .skip_while(|c| c.is_whitespace())
            .take_while(|c| !c.is_whitespace())
            .collect::<String>()
            .parse::<T>()
            .ok()
            .unwrap()
    }

    pub fn str() -> String {
        read()
    }

    pub fn s() -> Vec<char> {
        str().chars().collect()
    }

    pub fn i() -> i64 {
        read()
    }

    pub fn u() -> usize {
        read()
    }

    pub fn u2() -> (usize, usize) {
        (read(), read())
    }

    pub fn u3() -> (usize, usize, usize) {
        (read(), read(), read())
    }

    pub fn f() -> f64 {
        read()
    }

    pub fn c() -> char {
        read::<String>().pop().unwrap()
    }

    pub fn iv(n: usize) -> Vec<i64> {
        (0..n).map(|_| i()).collect()
    }

    pub fn iv2(n: usize) -> Vec<(i64, i64)> {
        (0..n).map(|_| iv(2)).map(|a| (a[0], a[1])).collect()
    }

    pub fn uv(n: usize) -> Vec<usize> {
        (0..n).map(|_| u()).collect()
    }

    pub fn uv2(n: usize) -> Vec<(usize, usize)> {
        (0..n).map(|_| uv(2)).map(|a| (a[0], a[1])).collect()
    }

    pub fn uv3(n: usize) -> Vec<(usize, usize, usize)> {
        (0..n).map(|_| uv(3)).map(|a| (a[0], a[1], a[2])).collect()
    }

    pub fn fv(n: usize) -> Vec<f64> {
        (0..n).map(|_| f()).collect()
    }

    pub fn cmap(h: usize) -> Vec<Vec<char>> {
        (0..h).map(|_| s()).collect()
    }
}