#P8712. Concatenated Multiples
Concatenated Multiples
Concatenated Multiples
You are given an array of n integers \(A_1,A_2,\dots,A_n\). You are allowed to pick two distinct elements \(A_i\) and \(A_j\) (with \(i \neq j\)) and concatenate their decimal representations in order (i.e. forming either \(A_iA_j\) or \(A_jA_i\)). Note that even if \(A_i=A_j\) in value, exchanging the order is considered different. For example, concatenating 12
and 345
may yield either 12345
or 34512
.
Your task is to count how many ways to choose and order two different numbers so that the resulting integer is divisible by a given integer \(K\). The concatenated integer is formed as follows:
[ \text{result} = A_i \times 10^{d(A_j)} + A_j, ]
where \(d(A_j)\) denotes the number of digits of \(A_j\).
Input Constraints: It is guaranteed that the numbers and \(K\) are such that the answer fits in a 64-bit signed integer.
inputFormat
The first line contains two integers \(n\) and \(K\) separated by a space.
The second line contains \(n\) positive integers \(A_1, A_2, \dots, A_n\) separated by spaces.
outputFormat
Output a single integer, which is the number of valid concatenation ways such that the resulting integer is divisible by \(K\).
sample
4 3
1 2 3 4
4
</p>