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)

use self::mod_int::ModInt;
const MOD: u64 = 1e9 as u64 + 7;

fn main() {
    let (r, w) = (std::io::stdin(), std::io::stdout());
    let mut sc = IO::new(r.lock(), w.lock());
    let n: u64 = sc.read();
    let x: usize = sc.read();
    let y: usize = sc.read();
    let z: usize = sc.read();

    let sum = x + y + z;
    let mut dp = vec![m(0); 1 << sum];
    dp[0] = m(1);
    for _ in 0..n {
        let mut next = vec![m(0); 1 << sum];
        for mask in 0..(1 << sum) {
            if dp[mask].v == 0 {
                continue;
            }

            let prev_list = mask_to_vec(mask);
            for next_d in 1..11 {
                let mut next_list = prev_list.clone();
                next_list.push(next_d);
                if !is_valid(&next_list, [x, y, z]) {
                    continue;
                }

                let next_mask = vec_to_mask(next_list, sum);
                next[next_mask] += dp[mask];
            }
        }
        dp = next;
    }

    let mut sum = m(0);
    for v in dp.into_iter() {
        sum += v;
    }
    let ans = m(10).pow(n) - sum;
    println!("{}", ans.v);
}

fn is_valid(list: &Vec<usize>, xyz: [usize; 3]) -> bool {
    let mut cur_i = 2;
    let mut remain = xyz[cur_i];
    for &a in list.iter().rev() {
        if remain == a {
            if cur_i == 0 {
                return false;
            }
            cur_i -= 1;
            remain = xyz[cur_i];
        } else if remain > a {
            remain -= a;
        } else {
            return true;
        }
    }
    true
}

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

fn vec_to_mask(v: Vec<usize>, sum: usize) -> usize {
    let mut remove = v.len();
    let mut cur = 0;
    for &v in v.iter().rev() {
        if cur + v < sum {
            cur += v;
            remove -= 1;
        } else {
            break;
        }
    }

    let mut result = 0;
    for v in v.into_iter().skip(remove) {
        let add = 1 << (v - 1);
        result <<= v;
        result += add;
    }
    result
}

fn m(v: u64) -> ModInt<u64> {
    ModInt::new(v, MOD)
}

pub mod mod_int {
    use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, Sub, SubAssign};

    pub struct ModInt<T> {
        pub v: T,
        pub modulo: T,
    }

    impl<T> Copy for ModInt<T> where T: Copy {}
    impl<T> Clone for ModInt<T>
    where
        T: Copy,
    {
        fn clone(&self) -> Self {
            ModInt {
                v: self.v,
                modulo: self.modulo,
            }
        }
    }

    impl<T> Add<T> for ModInt<T>
    where
        T: Add<Output = T> + Sub<Output = T> + Rem<Output = T> + Copy + PartialOrd,
    {
        type Output = ModInt<T>;
        fn add(self, mut rhs: T) -> ModInt<T> {
            if rhs >= self.modulo {
                rhs = rhs % self.modulo;
            }
            let mut t = rhs + self.v;
            if t >= self.modulo {
                t = t - self.modulo;
            }
            ModInt {
                v: t,
                modulo: self.modulo,
            }
        }
    }

    impl<T> Add<ModInt<T>> for ModInt<T>
    where
        T: Copy,
        ModInt<T>: Add<T, Output = ModInt<T>>,
    {
        type Output = ModInt<T>;
        fn add(self, rhs: ModInt<T>) -> ModInt<T> {
            self + rhs.v
        }
    }

    impl<T> Sub<T> for ModInt<T>
    where
        T: Add<Output = T> + Sub<Output = T> + Rem<Output = T> + Copy + PartialOrd,
    {
        type Output = ModInt<T>;
        fn sub(self, rhs: T) -> ModInt<T> {
            let rhs = if rhs >= self.modulo {
                rhs % self.modulo
            } else {
                rhs
            };
            let value = if self.v < rhs {
                self.v + self.modulo
            } else {
                self.v
            };
            ModInt {
                v: value - rhs,
                modulo: self.modulo,
            }
        }
    }

    impl<T> Sub<ModInt<T>> for ModInt<T>
    where
        T: Copy,
        ModInt<T>: Sub<T, Output = ModInt<T>>,
    {
        type Output = ModInt<T>;
        fn sub(self, rhs: ModInt<T>) -> ModInt<T> {
            self - rhs.v
        }
    }

    impl<T> AddAssign<T> for ModInt<T>
    where
        T: Copy,
        ModInt<T>: Add<T, Output = ModInt<T>>,
    {
        fn add_assign(&mut self, other: T) {
            *self = *self + other;
        }
    }
    impl<T> AddAssign<ModInt<T>> for ModInt<T>
    where
        T: Copy,
        ModInt<T>: Add<ModInt<T>, Output = ModInt<T>>,
    {
        fn add_assign(&mut self, other: ModInt<T>) {
            *self = *self + other;
        }
    }

    impl<T> SubAssign<T> for ModInt<T>
    where
        T: Copy,
        ModInt<T>: Sub<T, Output = ModInt<T>>,
    {
        fn sub_assign(&mut self, other: T) {
            *self = *self - other;
        }
    }

    impl<T> SubAssign<ModInt<T>> for ModInt<T>
    where
        T: Copy,
        ModInt<T>: Sub<ModInt<T>, Output = ModInt<T>>,
    {
        fn sub_assign(&mut self, other: ModInt<T>) {
            *self = *self - other;
        }
    }

    impl<T> Div<ModInt<T>> for ModInt<T>
    where
        T: Copy,
        ModInt<T>: Div<T, Output = ModInt<T>>,
    {
        type Output = ModInt<T>;
        fn div(self, rhs: ModInt<T>) -> ModInt<T> {
            self / rhs.v
        }
    }

    impl<T> DivAssign<T> for ModInt<T>
    where
        T: Copy,
        ModInt<T>: Div<T, Output = ModInt<T>>,
    {
        fn div_assign(&mut self, rhs: T) {
            *self = *self / rhs
        }
    }
    impl<T> DivAssign<ModInt<T>> for ModInt<T>
    where
        T: Copy,
        ModInt<T>: Div<ModInt<T>, Output = ModInt<T>>,
    {
        fn div_assign(&mut self, rhs: ModInt<T>) {
            *self = *self / rhs
        }
    }

    impl<T> Mul<T> for ModInt<T>
    where
        T: Mul<Output = T> + Rem<Output = T> + Copy + PartialOrd,
    {
        type Output = ModInt<T>;

        fn mul(self, mut rhs: T) -> ModInt<T> {
            if rhs >= self.modulo {
                rhs = rhs % self.modulo;
            }
            let t = (self.v * rhs) % self.modulo;
            ModInt {
                v: t,
                modulo: self.modulo,
            }
        }
    }
    impl<T> Mul<ModInt<T>> for ModInt<T>
    where
        T: Copy,
        ModInt<T>: Mul<T, Output = ModInt<T>>,
    {
        type Output = ModInt<T>;
        fn mul(self, rhs: ModInt<T>) -> ModInt<T> {
            self * rhs.v
        }
    }

    impl<T> MulAssign<T> for ModInt<T>
    where
        T: Copy,
        ModInt<T>: Mul<T, Output = ModInt<T>>,
    {
        fn mul_assign(&mut self, rhs: T) {
            *self = *self * rhs;
        }
    }

    impl<T> MulAssign<ModInt<T>> for ModInt<T>
    where
        T: Copy,
        ModInt<T>: Mul<ModInt<T>, Output = ModInt<T>>,
    {
        fn mul_assign(&mut self, rhs: ModInt<T>) {
            *self = *self * rhs;
        }
    }

    impl<T> ModInt<T>
    where
        T: Copy,
        ModInt<T>: Mul<ModInt<T>, Output = ModInt<T>>,
    {
        pub fn general_pow(self, e: u64, one: ModInt<T>) -> ModInt<T> {
            let mut result = one;
            let mut cur = self;
            let mut e = e;
            while e > 0 {
                if e & 1 == 1 {
                    result *= cur;
                }
                e >>= 1;
                cur *= cur;
            }
            result
        }
    }

    impl Div<u64> for ModInt<u64> {
        type Output = ModInt<u64>;
        fn div(self, mut rhs: u64) -> ModInt<u64> {
            if rhs >= self.modulo {
                rhs %= self.modulo;
            }
            self * ModInt {
                v: rhs,
                modulo: self.modulo,
            }
            .pow(self.modulo - 2)
        }
    }

    impl ModInt<u64> {
        pub fn new(v: u64, modulo: u64) -> Self {
            ModInt {
                v: v % modulo,
                modulo: modulo,
            }
        }

        pub fn pow(self, e: u64) -> ModInt<u64> {
            self.general_pow(e, ModInt::new(1, self.modulo))
        }
    }
}

pub struct IO<R, W: std::io::Write>(R, std::io::BufWriter<W>);

impl<R: std::io::Read, W: std::io::Write> IO<R, W> {
    pub fn new(r: R, w: W) -> Self {
        IO(r, std::io::BufWriter::new(w))
    }
    pub fn write<S: ToString>(&mut self, s: S) {
        use std::io::Write;
        self.1.write(s.to_string().as_bytes()).unwrap();
    }
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .0
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n' || b == b'\r' || b == b'\t')
            .take_while(|&b| b != b' ' && b != b'\n' && b != b'\r' && b != b'\t')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }
    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}