Problem:
Score : 200 points

Problem Statement

Snuke has come to a store that sells boxes containing balls. The store sells the following three kinds of boxes:

Red boxes, each containing R red balls

Green boxes, each containing G green balls

Blue boxes, each containing B blue balls

Snuke wants to get a total of exactly N balls by buying r red boxes, g green boxes and b blue boxes.
How many triples of non-negative integers (r,g,b) achieve this?

Constraints

All values in input are integers.

1 \leq R,G,B,N \leq 3000

Input

Input is given from Standard Input in the following format:

R G B N

Output

Print the answer.

Sample Input 1

1 2 3 4

Sample Output 1

4

Four triples achieve the objective, as follows:

(4,0,0)

(2,1,0)

(1,0,1)

(0,2,0)

Sample Input 2

13 1 4 3000

Sample Output 2

87058

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

use std::collections::HashSet;

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

fn main() {
    input!{
        r: usize,
        g: usize,
        b: usize,
        n: usize
    }
    let mut vs = vec![r, g, b];
    vs.sort();
    let (b, g, r) = (vs[0], vs[1], vs[2]);
    let mut ns = HashSet::new();
    ns.insert(n);
    for i in 0 .. n / r {
        ns.insert(n - r * (i +1));
    }
    let mut cnt = 0;
    for gg in (0..(n / g) + 1).rev() {
        let gk = gg * g;
        let n_g = n - gk;
        for bb in (0..(n_g / b) + 1).rev() {
            let bk = bb * b;
            if ns.contains(&(gk + bk)) {
                cnt += 1;
            }
        }
    }
    // let mut cnt = 0;
    // for rr in (0..(n / r) + 1).rev() {
    //     let rk = rr * r;
    //     let n_r = n - rk;
    //     for gg in (0..(n_r / g) + 1).rev() {
    //         let gk = gg * g;
    //         let n_r_g = n_r - gk;
    //         for bb in (0..(n_r_g / b) + 1).rev() {
    //             let bk = bb * b;
    //             if rk + gk + bk == n {
    //                 cnt += 1;
    //             }
    //         }
    //     }
    // }
    println!("{}", cnt);
}