Problem:
Score : 700 points

Problem Statement

Haiku is a short form of Japanese poetry. A Haiku consists of three phrases with 5, 7 and 5 syllables, in this order.

Iroha is looking for X,Y,Z-Haiku (defined below) in integer sequences.

Consider all integer sequences of length N whose elements are between 1 and 10, inclusive. Out of those 10^N sequences, how many contain an X,Y,Z-Haiku?

Here, an integer sequence a_0, a_1, ..., a_{N-1} is said to contain an X,Y,Z-Haiku if and only if there exist four indices x, y, z, w (0 ≦ x < y < z < w ≦ N) such that all of the following are satisfied:

a_x + a_{x+1} + ... + a_{y-1} = X

a_y + a_{y+1} + ... + a_{z-1} = Y

a_z + a_{z+1} + ... + a_{w-1} = Z

Since the answer can be extremely large, print the number modulo 10^9+7.

Constraints

3 ≦ N ≦ 40

1 ≦ X ≦ 5

1 ≦ Y ≦ 7

1 ≦ Z ≦ 5

Input

The input is given from Standard Input in the following format:

N X Y Z

Output

Print the number of the sequences that contain an X,Y,Z-Haiku, modulo 10^9+7.

Sample Input 1

3 5 7 5

Sample Output 1

1

Here, the only sequence that contains a 5,7,5-Haiku is [5, 7, 5].

Sample Input 2

4 5 7 5

Sample Output 2

34

Sample Input 3

37 4 2 3

Sample Output 3

863912418

Sample Input 4

40 5 7 5

Sample Output 4

562805100

Solution in Rust (1.15.1) (judge verdict: Accepted)

const MOD: usize = 1_000_000_007;
const MAX: usize = 1 << 17;

fn mod_pow(x: usize, e: usize) -> usize {
    let mut cur = x;
    let mut res = 1;
    let mut e = e;
    while e > 0 {
        if e & 1 == 1 {
            res = (res * cur) % MOD;
        }
        e >>= 1;
        cur = (cur * cur) % MOD;
    }
    res
}

fn main() {
    let mut sc = Scanner::new();
    let n = sc.usize_read();
    let x = sc.usize_read();
    let y = sc.usize_read();
    let z = sc.usize_read();
    let v_xyz = vec![x, y, z];

    let xyz = x + y + z;
    let mut dp = vec![0; MAX];
    dp[0] = 1;
    for i in 0..n {
        let mut next = vec![0; MAX];
        for mask in 0..MAX {
            if dp[mask] == 0 {
                continue;
            }
            let mut prev_numbers = mask2nums(mask);

            for a in 1..11 {
                let mut check = false;
                let mut tmp_sum = 0;
                let mut cur_pos = 2;
                prev_numbers.push(a);
                for &prev in prev_numbers.iter().rev() {
                    tmp_sum += prev;
                    if tmp_sum == v_xyz[cur_pos] {
                        if cur_pos > 0 {
                            cur_pos -= 1;
                            tmp_sum = 0;
                        } else {
                            check = true;
                            break;
                        }
                    } else if tmp_sum > v_xyz[cur_pos] {
                        break;
                    }
                }

                if !check {
                    let mut next_mask = 0;
                    for &prev in &prev_numbers {
                        next_mask <<= prev;
                        next_mask += (1 << (prev - 1));
                    }
                    next_mask &= (1 << xyz) - 1;
                    next[next_mask] = (next[next_mask] + dp[mask]) % MOD;
                }
                prev_numbers.pop();
            }
        }
        dp = next;
    }

    let mut sum = 0;
    for &dp in &dp {
        sum = (sum + dp) % MOD;
    }
    let ans = mod_pow(10, n) + MOD - sum;
    println!("{}", ans % MOD);
}

fn mask2nums(mask: usize) -> Vec<usize> {
    let mut prev_numbers = vec![];
    let mut mask = mask;
    while mask > 0 {
        let zeros = trailing_zeros(mask);
        prev_numbers.push(zeros + 1);
        mask >>= zeros + 1;
    }
    prev_numbers.reverse();
    prev_numbers
}

fn trailing_zeros(x: usize) -> usize {
    assert!(x > 0);
    for i in 0..64 {
        if (x >> i) & 1 != 0 {
            return i;
        }
    }
    panic!();
}

struct Scanner {
    ptr: usize,
    length: usize,
    buf: Vec<u8>,
    small_cache: Vec<u8>,
}

#[allow(dead_code)]
impl Scanner {
    fn new() -> Scanner {
        Scanner {
            ptr: 0,
            length: 0,
            buf: vec![0; 1024],
            small_cache: vec![0; 1024],
        }
    }

    fn load(&mut self) {
        use std::io::Read;
        let mut s = std::io::stdin();
        self.length = s.read(&mut self.buf).unwrap();
    }

    fn byte(&mut self) -> u8 {
        if self.ptr >= self.length {
            self.ptr = 0;
            self.load();
            if self.length == 0 {
                self.buf[0] = b'\n';
                self.length = 1;
            }
        }

        self.ptr += 1;
        return self.buf[self.ptr - 1];
    }

    fn is_space(b: u8) -> bool {
        b == b'\n' || b == b'\r' || b == b'\t' || b == b' '
    }

    fn read_vec<T>(&mut self, n: usize) -> Vec<T>
    where
        T: std::str::FromStr,
        T::Err: std::fmt::Debug,
    {
        (0..n).map(|_| self.read()).collect()
    }

    fn usize_read(&mut self) -> usize {
        self.read()
    }

    fn read<T>(&mut self) -> T
    where
        T: std::str::FromStr,
        T::Err: std::fmt::Debug,
    {
        let mut b = self.byte();
        while Scanner::is_space(b) {
            b = self.byte();
        }

        for pos in 0..self.small_cache.len() {
            self.small_cache[pos] = b;
            b = self.byte();
            if Scanner::is_space(b) {
                return String::from_utf8_lossy(&self.small_cache[0..(pos + 1)])
                    .parse()
                    .unwrap();
            }
        }

        let mut v = self.small_cache.clone();
        while !Scanner::is_space(b) {
            v.push(b);
            b = self.byte();
        }
        return String::from_utf8_lossy(&v).parse().unwrap();
    }
}