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)

use std::fs;
use std::io::{stdin, Read};

macro_rules! parse {
    ($it: ident) => {
        $it.next().unwrap().parse().unwrap()
    };
    ($it: ident,2) => {
        (parse!($it), parse!($it))
    };
    ($it: ident,3) => {
        (parse!($it), parse!($it), parse!($it))
    };
    ($it: ident,4) => {
        (parse!($it), parse!($it), parse!($it), parse!($it))
    };
    ($it: ident, $n: expr) => {
        (&mut $it).take($n).map(|e| e.parse().unwrap()).collect();
    };
}

fn comb(m: usize, n: usize) -> usize {
    if m < n {
        return 0;
    }
    let mut ret = 1;
    for i in 0..m {
        ret *= i + 1;
    }
    for i in 0..n {
        ret /= i + 1
    }
    for i in 0..(m - n) {
        ret /= i + 1
    }
    ret
}

fn combmodlucas(m: usize, n: usize, p: usize) -> usize {
    let mut m = m;
    let mut n = n;
    if m < n {
        return 0;
    }
    let mut ret = 1;
    loop {
        ret *= comb(m % p, n % p);
        if m == 0 {
            break;
        }
        m /= p;
        n /= p;
    }
    ret
}

fn solve(s: &str) {
    let mut it = s.split_whitespace();
    let mut n: usize = parse!(it);
    let a: Vec<usize> = it
        .next()
        .unwrap()
        .chars()
        .map(|i| i.to_digit(10).unwrap() as usize - 1)
        .collect();
    if a.iter().any(|&i| i == 1) {
        let i = a.iter().enumerate().fold(0, |s, (i, &x)| {
            (s + combmodlucas(n - 1, i, 2) * ((x == 1) as usize)) % 2
        });
        if i == 1 {
            println!("{}", 1);
        } else {
            println!("{}", 0);
        }
    } else {
        let i = a.iter().enumerate().fold(0, |s, (i, &x)| {
            (s + combmodlucas(n - 1, i, 2) * ((x == 2) as usize)) % 2
        });
        if i == 1 {
            println!("{}", 2);
        } else {
            println!("{}", 0);
        }
    }
}

fn test() {
    let mut f = fs::File::open("foo.txt").unwrap();
    let mut s = String::new();
    f.read_to_string(&mut s).unwrap();
    solve(&s);
}

fn main() {
    let mut s = String::new();
    stdin().read_to_string(&mut s).unwrap();
    solve(&s);
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_main() {
        test();
    }
    #[test]
    fn test_input() {
        let s = "
        ";
        solve(s);
    }
}