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: Time Limit Exceeded)

#![allow(bare_trait_objects)]
#![allow(unused_imports)]
#![allow(dead_code)]

use std::fmt::Debug;
use std::io::BufRead;
use std::io::Stdin;
use std::io::StdinLock;
use std::str::FromStr;

pub struct Scanner<'a> {
    buf_read: Box<BufRead + 'a>,
    tokens: Vec<String>,
}

impl<'a> Scanner<'a> {
    pub fn new(buf_read: StdinLock<'a>) -> Self {
        Scanner {
            buf_read: Box::new(buf_read),
            tokens: Vec::new(),
        }
    }

    // pub fn from_stdin() -> Self {
    //     let stdin = Box::leak(Box::new(std::io::stdin()));
    //     Self {
    //         buf_read: Box::new(stdin.lock()),
    //         tokens: Vec::new(),
    //     }
    // }

    pub fn next(&mut self) -> String {
        while self.tokens.is_empty() {
            let mut s = String::new();
            if self.buf_read.read_line(&mut s).is_err() {
                panic!()
            }
            self.tokens = s
                .split_whitespace()
                .filter(|s| !s.is_empty())
                .rev()
                .map(str::to_string)
                .collect();
        }
        self.tokens.pop().unwrap()
    }

    pub fn next_g<T: FromStr>(&mut self) -> T
    where
        T::Err: Debug,
    {
        self.next().parse().unwrap()
    }

    pub fn next_vec_g<T: FromStr>(&mut self, n: usize) -> Vec<T>
    where
        T::Err: Debug,
    {
        (0..n).map(|_| self.next_g()).collect()
    }

    pub fn usize(&mut self) -> usize {
        self.next_g()
    }

    pub fn i64(&mut self) -> i64 {
        self.next_g()
    }

    pub fn vec_i64(&mut self, n: usize) -> Vec<i64> {
        self.next_vec_g(n)
    }

    pub fn vec_usize(&mut self, n: usize) -> Vec<usize> {
        self.next_vec_g(n)
    }

    pub fn chars(&mut self) -> Vec<char> {
        self.next().chars().collect()
    }
}

fn step(x: &mut Vec<i32>) {
    for i in 0..x.len() - 1 {
        x[i] = (x[i] - x[i + 1]).abs();
    }
    x.pop();
}

fn simulate(x: &mut Vec<i32>) -> usize {
    while x.len() > 1 {
        step(x);
    }
    x[0] as usize
}

fn main() {
    let stdin = std::io::stdin();
    let mut sc = Scanner::new(stdin.lock());

    let n = sc.usize();
    let mut cs: Vec<_> = sc.chars().iter().map(|c| *c as i32 - '0' as i32).collect();

    let n = n - 1;
    step(&mut cs);

    let mut x = Vec::new();
    if cs.iter().all(|v| *v != 1) {
        let mut v = 0_u64;
        for i in 0..cs.len() {
            if i % 64 == 0 && i > 0 {
                x.push(v);
                v = 0;
            }
            if cs[i] == 2 {
                v |= 1 << (i % 64);
            }
        }
        x.push(v);
        x.push(0);
        for i in (1..n).rev() {
            for j in 0..x.len() - 1 {
                x[j] ^= (x[j] >> 1) | (x[j + 1] << 63);
            }
            x[i / 64] &= !(1 << i % 64);
        }
        println!("{}", x[0] * 2);
    } else {
        let mut v = 0_u64;
        for i in 0..cs.len() {
            if i % 64 == 0 && i > 0 {
                x.push(v);
                v = 0;
            }
            if cs[i] == 1 {
                v |= 1 << (i % 64);
            }
        }
        x.push(v);
        x.push(0);
        for i in (1..n).rev() {
            for j in 0..x.len() - 1 {
                x[j] ^= (x[j] >> 1) | (x[j + 1] << 63);
            }
            x[i / 64] &= !(1 << (i % 64));
            // eprintln!("{:?}", x);
        }
        println!("{}", x[0]);
    }

    // let mut cnt = vec![0; 3];
    // // let mut y = 0;
    // let m = 7;
    // let mut ans = Vec::new();
    // for mut i in 0..2_i32.pow(m) {
    //     let mut x = Vec::new();
    //     for _ in 0..m {
    //         x.push(i % 2);
    //         i /= 2;
    //     }
    //     let o = x.clone();
    //     let v = simulate(&mut x);
    //     cnt[v] += 1;
    //     ans.push(v);
    //     // if o.iter().all(|v| *v != 1) {
    //     //     // if y < 30 {
    //     //     eprintln!("{:?} {}", o, v);
    //     //     // }
    //     // }
    //     // y += 1;
    // }

    // eprintln!("{:?}", ans);

    // eprintln!("{:?}", cnt);

    // let mut cnt1 = 0;
    // let mut cnt1 = 0;
    // for c in cs {}

    // println!("{}", *daq(&cs).0.last().unwrap());
}