Megazords
InterviewTime limit2sMemory limit1024 MB
Count the ways to pick one green, one red, and one blue zord whose boundary digits match and whose three model numbers are all different.
- Level
Medium5 of 10
- Topics
- Hash map, Math, Combinatorics
- Solved
- No attempts yet
Statement
Megazords are essential for defending the Earth. Assembling one megazord takes exactly three zords: one green, one red, and one blue.
The green, red, and blue rangers want to know how many different ways they can assemble a megazord. Each ranger has a set of zords. The green ranger has green zords, the red ranger has red zords, and the blue ranger has blue zords.
Three rules apply when assembling a megazord.
- The first digit of the red zord's model number must equal the last digit of the green zord's model number.
- The last digit of the red zord's model number must equal the first digit of the blue zord's model number.
- The three zords used must all have different model numbers.
Two ways are different if at least one ranger uses a different zord, even when the model number is the same. A ranger may have several zords with the same model number.
Input
The first line contains the number of zords the green, red, and blue rangers have, , , and ().
The second line contains model numbers of the green zords (). The third line contains model numbers of the red zords, and the fourth line contains model numbers of the blue zords.
Output
Print one integer, the number of different megazords that can be assembled.
Hint
The first sample has three megazords: , , and . The last one is counted twice because the red ranger can use either the second or the third zord. cannot be assembled because it uses model 11 twice.