Lucky Numbers
For the purposes of this exercise, we will say that a positive integer is lucky if the following conditions hold:
- It has an even number of digits, and
- the sum of the first half of its digits is equal to the sum of the second half of its digits.
For example:
- The number is not lucky because the number of its digits is 3, which is not an even number.
- The number is not lucky because the sum of the first half of its digits is , while the sum of the second half of its digits is .
- The number is lucky, because the sum of the first half of its digits is and the sum of the second half of its digits is .
You are given N cards, each of which has a number written on it. In how many different ways can you stick together two different cards (i.e., concatenate the digits of the numbers written on them) to form a lucky number?
Problem
Write a program in one of the following languages: PASCAL, C, C++, JAVA, which will read the value of N and the numbers written on the N cards, and will print the number of different ways lucky numbers can be formed by sticking two cards together.
For your submissions to DMOJ, use STDIN and STDOUT. The input and output format will be as described below.
Input Files
The input file named luckyagain.in is a text file consisting of two lines. The first line contains an integer N, the number of cards. The second line contains integers, separated in pairs by a single space.
Output Files
The output file named luckyagain.out is a text file containing a single line with an integer: the number of different ways lucky numbers can be formed by sticking two cards together.
Examples of Input - Output Files:
1st
input:
7
75038 92 1 728 83 5 423
output:
6
Explanation: Lucky numbers can be formed by sticking two cards together in 6 different ways:
- Sticking and together yields the lucky number ,
- Sticking and together yields the lucky number ,
- Sticking and together yields the lucky number ,
- Sticking and together yields the lucky number ,
- Sticking and together yields the lucky number , and
- Sticking and together yields the lucky number .
2nd
input:
6
265 10387 392 981 6986 74
output:
0
Explanation: No lucky number can be formed.
3d
input:
5
17 62 35 44 80
output:
20
Explanation: Sticking any two cards together always results in a lucky number.
Constraints
- The numbers will be at most nine digits long, and the first digit will always be nonzero.
- For test cases with a total value of , .
- For test cases with a total value of , the numbers will be either five or six digits long.
- In the output, all lines must terminate with a
newline
character. - Time Limit: sec.
- Maximum Available Memory: MB.
Attention: The answer may exceed . Also, make sure to read input and print output efficiently, especially if you're coding in C++ or Java.
Comments