Problem:
Score : 200 points

Problem Statement

You have A 500-yen coins, B 100-yen coins and C 50-yen coins (yen is the currency of Japan).
In how many ways can we select some of these coins so that they are X yen in total?

Coins of the same kind cannot be distinguished. Two ways to select coins are distinguished when, for some kind of coin, the numbers of that coin are different.

Constraints

0 \leq A, B, C \leq 50

A + B + C \geq 1

50 \leq X \leq 20 000

A, B and C are integers.

X is a multiple of 50.

Input

Input is given from Standard Input in the following format:

A
B
C
X

Output

Print the number of ways to select coins.

Sample Input 1

2
2
2
100

Sample Output 1

2

There are two ways to satisfy the condition:

Select zero 500-yen coins, one 100-yen coin and zero 50-yen coins.

Select zero 500-yen coins, zero 100-yen coins and two 50-yen coins.

Sample Input 2

5
1
0
150

Sample Output 2

0

Note that the total must be exactly X yen.

Sample Input 3

30
40
50
6000

Sample Output 3

213

Solution in Perl (v5.18.2) (judge verdict: Accepted)

#! /usr/bin/perl

use strict;
use warnings;

chomp(my $n500 = <>);
chomp(my $n100 = <>);
chomp(my $n50 = <>);
chomp(my $total = <>);

my $c50 = $total / 50;
my $c100 = 0;
my $c500 = 0;
while($n50 < $c50) {
    $c50 -= 2;
    $c100 ++;
}
while($n100 < $c100) {
    $c100 -= 5;
    $c500 ++;
}
my %seen;
xxx([ $c50, $c100, $c500]);

print scalar(keys %seen), "\n";

sub xxx {
    my $list = shift;
    my($c50, $c100, $c500) = @$list;

    return unless (0 <= $c50  && $c50 <= $n50 &&
       0 <= $c100 && $c100 <= $n100 &&
       0 <= $c500 && $c500 <= $n500);

    my $key = "$c50,$c100,$c500";
    return if $seen{$key};

    $seen{$key} = 1;
    if(2 <= $c50) {
        xxx([ $c50-2, $c100+1, $c500]);
    }
    if(10 <= $c50) {
        xxx([ $c50-10, $c100, $c500+1]);
    }
    if(5 <= $c100) {
        xxx([ $c50, $c100-5, $c500+1]);
    }
}