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 = (1e9 as usize) + 7;
const MAX_BIT: usize = 1 << 16;

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

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 mut dp = vec![0; MAX_BIT];
    dp[0] = 1;
    for _ in 0..n {
        let mut next = vec![0; MAX_BIT];
        for mask in 0..(MAX_BIT) {
            if dp[mask] == 0 {
                continue;
            }
            let mut v = mask2vec(mask);
            for a in 1..11 {
                v.push(a);
                if check(&v, x, y, z) {
                    let next_mask = vec2mask(&v);
                    next[next_mask] = (next[next_mask] + dp[mask]) % MOD;
                }
                v.pop();
            }
        }
        dp = next;
    }

    let mut ans = mod_pow(10, n);
    for mask in 0..MAX_BIT {
        ans = (ans + MOD - dp[mask]) % MOD;
    }
    println!("{}", ans);
}

fn check(v: &Vec<usize>, x: usize, y: usize, z: usize) -> bool {
    let mut check = vec![x, y, z];
    let mut pos = 2;
    for &v in v.iter().rev() {
        if check[pos] == v {
            if pos == 0 {
                return false;
            } else {
                pos -= 1;
            }
        } else if check[pos] < v {
            return true;
        } else {
            check[pos] -= v;
        }
    }
    return true;
}

fn vec2mask(v: &Vec<usize>) -> usize {
    let mut result = 0;
    for &a in v {
        result <<= a;
        result += (1 << (a - 1));
    }
    result & (MAX_BIT - 1)
}

fn mask2vec(mask: usize) -> Vec<usize> {
    let mut cur = mask;
    let mut result = vec![];
    let mut count = 1;
    while cur > 0 {
        if cur & 1 == 0 {
            count += 1;
        } else {
            result.push(count);
            count = 1;
        }
        cur >>= 1;
    }
    result.reverse();
    result
}

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();
    }
}