Under construction

Still being built. Things may change

Wonderful Fours

Time limit1sMemory limit128 MB

Summary
Given five digits, count unordered triples of distinct permutations (no leading zero) whose sum is a different valid permutation of the same digits.
Level

Medium5 of 10

Topics
Brute force, Combinatorics, Math, Implementation
Solved
No attempts yet

Problem

Let us call a set of five decimal digits K5K_5. (Note that a digit may appear more than once in this set.)

We say that a five-digit natural number is properly formed from K5K_5 if it is obtained by writing all of the digits of K5K_5 in a row in some order (using each of them exactly once) and it does not start with 00.

For example, if K5K_5 contains the digits 1,1,7,0,41, 1, 7, 0, 4, then 1714017140 and 4701147011 are properly formed from K5K_5, while 1774017740 is not.

We call four five-digit natural numbers s1,s2,s3,s4s_1, s_2, s_3, s_4 a wonderful four of K5K_5 if all of the following hold:

  1. s1s_1 is properly formed from K5K_5.
  2. s2s_2 is properly formed from K5K_5.
  3. s3s_3 is properly formed from K5K_5.
  4. s4s_4 is properly formed from K5K_5.
  5. s1,s2,s3,s4s_1, s_2, s_3, s_4 are all different numbers.
  6. s1+s2+s3=s4s_1 + s_2 + s_3 = s_4

Count how many different wonderful fours can be formed from the five digits of the set K5K_5 given in the input. (Reordering the numbers within a wonderful four does not create a new wonderful four.)

Input

A single line containing five decimal digits separated by spaces. Each digit is between 00 and 99 inclusive; together the five digits form the set K5K_5.

Output

Print, on a single line, the number of different wonderful fours that can be formed from K5K_5.

Examples2

  1. Example 1

    Input
    0 2 6 2 8
    
    Expected output
    4
    
  2. Example 2

    Input
    9 2 3 3 9
    
    Expected output
    0