Susquehanna International GroupOA #4
Access Code Fragments — Concatenation Pairs
Estimated MediumJudge-ready
You are given positive integer fragments and a positive integer accessCode. Two fragments at different positions can be placed in either order, and their decimal representations are concatenated directly (not added). Count all ordered position pairs whose concatenation equals accessCode. Each fragment position can be used only once; swapping the positions of two fragments with the same value also counts as a different way.
Input
The first line contains N and accessCode (1≤N≤100000, 1≤accessCode≤10^18). The second line contains N positive integer fragments, each at most 10^9. Integers are written in canonical decimal form without leading zeros.
Output
Output the number of ordered position pairs that form accessCode.
Example 1
Input:
4 1212
1 212 12 12
Output:
3
Example 2
Input:
4 121
2 12 1 21
Output:
2
Example 3
Input:
2 1212
212 1
Output:
1
Example notes
Enumerate every split of accessCode into two non-empty parts. For each split, look up the occurrence counts of the left and right fragments; when both sides are the same, subtract the cases where the same position would be used twice.
Editorial
Loading solution…