We need to solve: Given N numbers (each up to 10^18), count number of triples (i<j<k) such that there exists at least one digit which is prime and that digit appears in all three numbers (i.e., a common digit that is prime). Digits are decimal digits (0-9). Prime digits are 2,3,5,7. So we need to count triples where the intersection of the sets of digits present in the numbers includes at least one prime digit.
We want to avoid O(N^3). N up to 1e5. We need combinatorial counting using inclusion-exclusion or complementary counting. Equivalent: Count total triples minus triples where there is no common prime digit. i.e., for every triple, if the intersection of their digit sets (which are subsets of digits 0-9) has no prime digit. That means that the common digits among the three numbers contain only non-prime digits (0,1,4,6,8,9). But careful: The condition "triplets have at least one prime digit in common" means there exists at least one digit d that is prime and appears in all three numbers. So the triple is good if the intersection of the digit sets (the set of digits that appear in all three numbers) contains at least one prime digit. So the complement: triple is bad if the intersection contains no prime digit, i.e., intersection ⊆ {0,1,4,6,8,9}. But also intersection could be empty (no common digit at all), which also counts as bad because it has no prime digit.
Thus we want to count total triples = C(N,3). Subtract number of bad triples. Bad triples are those where for every prime digit p∈{2,3,5,7}, at least one of the numbers does not contain p. That is, for the triple to be bad, there is no prime digit that appears in all three. Equivalently, for each prime digit p, the set of numbers that contain p, the triple must not be wholly inside that set for any p. So the triple is bad iff it is not contained in any of the sets S_p (p prime digit). But it could be contained in the union of some sets? Actually the condition "has at least one prime digit in common" means ∃p such that all three numbers are in S_p. So the good triples are exactly the union over p prime of the sets of triples where all three numbers are in S_p. However, triples that have two different prime digits common (i.e., all three numbers contain both 2 and 3) would be counted multiple times, so we need inclusion-exclusion to count good triples directly, or count bad triples via complement of union.
Since number of prime digits is only 4, inclusion-exclusion over 4 sets is manageable. Count good triples = |∪_{p∈P} T_p|, where T_p = set of triples where all three numbers contain prime digit p. Then using inclusion-exclusion:
Good = Σ|T_p| - Σ|T_{p∩q}| + Σ|T_{p∩q∩r}| - |T_{all four}|.
Where T_{p∩q} means triples where all three numbers contain both primes p and q, i.e., all three numbers are in S_p ∩ S_q, etc.
Then answer = Good.
We need to compute for each subset of prime digits (nonempty) the count of numbers that contain all those digits (i.e., whose digit set includes that subset). For each subset, let count = number of numbers that contain all primes in that subset. Then number of triples from those numbers = C(count, 3). But careful: In inclusion-exclusion, the intersection of T_p and T_q is T_{p∧q}? Actually T_p ∩ T_q = set of triples where all three numbers contain p AND all three contain q = triples where all three contain both p and q. So indeed it is T_{p,q} i.e., set of triples whose numbers are in the intersection S_p ∩ S_q. So |T_{p,q}| = C(|S_p ∩ S_q|, 3). However, be careful: For inclusion-exclusion we need cardinalities of intersections of the sets of triples. For two primes p and q, the intersection T_p ∩ T_q is triples where each number is in both S_p and S_q => they all contain both p and q. So yes.
Thus we can precompute for each subset of the 4 primes (2,3,5,7) the number of numbers that contain all digits in that subset. Since there are 2^4 = 16 subsets (including empty). Then compute the sum over nonempty subsets with sign = (-1)^{size+1} * C(cnt,3). Then answer = that sum.
But caution: If a number contains multiple prime digits, it contributes to many subsets. That's fine; inclusion-exclusion will handle overlaps. Since we are counting triples, the counts are based on numbers' membership in subsets.
We need to compute cnt[S] for each subset S (S ⊆ {2,3,5,7}) where cnt[S] = number of numbers in A such that for each prime digit p∈S, the number contains p; it may or may not contain other primes outside S. So we need to compute for each number, the set of prime digits it contains, say P_i ⊆ primes. Then for each subset S, we want to count numbers whose P_i includes S, i.e., S ⊆ P_i. That's essentially a superset count. This can be done by iterating over numbers, determining its prime-digit set P_i, then incrementing cnt[superset] for all supersets of P_i? Actually we want for each subset S, the count of numbers where P_i ⊇ S. That is the number of numbers that have at least the primes in S. We can compute by first counting f[Q] = number of numbers with exactly set Q (i.e., P_i = Q). Then cnt[S] = sum_{Q ⊇ S} f[Q]. Since there are only 16 subsets, we can iterate all numbers, compute P_i, increment f[P_i]. Then do SOS DP (sum over supersets) to compute cnt[S] = sum_{Q ⊇ S} f[Q]. Or simply for each S, iterate over all Q ⊇ S; 16*16 = 256 trivial.
Complexities: N up to 1e5, each number up to 10^18, so extracting digits is cheap. So we can compute.
But careful: The condition "has at least one prime digit in common" means there exists a digit that is prime and appears in all three numbers. But the digit could be the same prime digit, but also could there be a common digit that is prime? Actually the primes are only 2,3,5,7. So any common prime digit is one of these four. So our approach works.
But is it sufficient? Could a number have a prime digit but also other digits. That's fine.
One nuance: If a triple has two numbers that share digit 2, and the third also contains 2, then they have common prime digit 2. So triple belongs to T_2. So all good triples are captured.
Now we need to check if any triple could be counted multiple times, but inclusion-exclusion subtracts overlaps.
Thus answer = Σ_{∅≠S⊆P} (-1)^{|S|+1} * C(cnt[S], 3).
However, wait: Should we consider also the possibility of a triple having a common digit that is not prime? That's not needed for good. So only prime digits matter.
Edge cases: Numbers like 0 or 1-digit numbers. For numbers, we need to extract digits. For number 0, its digit set is {0}. For number like 100, digits are {0,1}. So they have no prime digits. They contribute to f[∅] (since they contain no prime digits). Then cnt[S] for S nonempty will count only numbers that contain at least those primes.
Now the inclusion-exclusion formula should be correct.
But let's test with sample: N=5, numbers: 21,22,23,24,25. Extract prime digits:
- 21: digits {2,1} -> prime set = {2}
- 22: digits {2} -> {2}
- 23: digits {2,3} -> {2,3}
- 24: digits {2,4} -> {2}
- 25: digits {2,5} -> {2,5}
Compute f[∅] = 0, f[{2}] = count numbers with exactly {2}? Actually 21,22,24 are exactly? 21 has {2}, but also 1 which is non-prime so yes exactly {2} (since we only care about prime digits). However, 23 has {2,3} so not exactly {2}. So numbers with exactly prime set = {2} are: 21,22,24 -> 3. f[{2,3}] = 1 (23), f[{2,5}] = 1 (25). Others zero.
Now cnt[S] = sum_{Q ⊇ S} f[Q]:
For S={2}: includes Q={2}, {2,3}, {2,5}, {2,3,5}, {2,3,7}, {2,5,7}, {2,3,5,7} but only those with nonzero f: {2}->3, {2,3}->1, {2,5}->1 => total 5. So cnt[{2}] = 5.
S={3}: Q containing 3: {3}? f[{3}]? none, {2,3}->1, {3,5}? none, etc. So cnt[{3}] = 1 (from 23). Actually check: 23 has 3, so yes. So cnt[{3}] = 1.
S={5}: cnt[{5}] = 1 (25).
S={7}: cnt[{7}] = 0.
S={2,3}: Q containing both: {2,3}->1, {2,3,5}? none, etc. So cnt[{2,3}] = 1.
S={2,5}: cnt[{2,5}] = 1 (25).
S={2,7}: =0.
S={3,5}: none (0)
S={3,7}:0
S={5,7}:0
S={2,3,5}:0
S={2,3,7}:0
S={2,5,7}:0
S={3,5,7}:0
S={2,3,5,7}:0
Now compute inclusion-exclusion: Good = sum over nonempty S (-1)^{|S|+1} C(cnt[S],3)
-
S size 1:
{2}: C(5,3)=10, sign + =>10
{3}: C(1,3)=0 =>0
{5}: 0
{7}: 0
Sum size1 =10
-
S size 2:
{2,3}: C(1,3)=0, sign - =>0
{2,5}: C(1,3)=0, sign - =>0
{2,7}: C(0,3)=0
{3,5}: 0
{3,7}:0
{5,7}:0
Sum size2 =0
-
S size 3: all zero.
-
S size 4: zero.
Total good =10, which matches sample output. So correct.
Thus algorithm: For each number, compute the set of prime digits present (as a bitmask of 4 bits, e.g., bit0->2, bit1->3, bit2->5, bit3->7). Let f[mask] count numbers with exactly that prime set. Then compute cnt[mask] = number of numbers whose prime set superset of mask = sum_{superset m' ⊇ mask} f[m']. Then apply inclusion-exclusion.
But note: C(cnt,3) may be large up to C(1e5,3) ~ 1.66e15, which fits in 64-bit? 1e5 choose 3 = 1e59999999998/6 ≈ (1e5)^3/6 ≈ 1.666e15, fits within 64-bit signed (max 9e18). So use 64-bit integer (long long in C++, int64). The answer could be up to total triples = C(N,3) ≤ about 1.66e15, so 64-bit safe.
Now complexity: O(N * d) where d is number of digits (max 19). Then O(2^4 * 2^4) for superset sum = constant. So O(N) essentially.
But wait: Are there any pitfalls? Let's verify the logic thoroughly: Is it possible that a triple has a common prime digit, but that digit is not necessarily part of the prime digit set? No.
But is there any scenario where the common digit is prime but not one of {2,3,5,7}? The only prime digits in decimal are 2,3,5,7. So yes.
Thus answer good.
But also consider: Could there be triple that has a common digit that is prime, but that digit is, say, '2' appears in all three numbers, but the numbers also might have other digits; but that's fine.
Thus answer computed by inclusion-exclusion over sets of numbers containing each prime.
But wait: The problem states "triplets have at least one prime digit in common." Could it be that they mean at least one digit that is prime (i.e., the digit itself is prime) is common among all three numbers, but it could be multiple prime digits? Our interpretation is correct.
Check for triple where numbers have common prime digit 2, but also maybe also 3, but that's okay.
Now confirm via alternative method: Good triples = total - triples with no common prime digit. Could there be a triple with no common prime digit but still intersection of digit sets might contain a non-prime digit? Yes, that's bad.
We could also compute bad triples by counting triples where for each prime digit p, at least one number missing p. That's like the complement of the union. That's same complexity.
Thus inclusion-exclusion method is straightforward.
Now we must careful: The sets T_p are defined as triples where all three numbers contain prime p. That's correct.
But what about triple where numbers contain prime p but also q? It's counted in both T_p and T_q, inclusion-exclusion handles.
Thus algorithm:
-
Read N.
-
Initialize array f[16] = 0.
-
For each number, read as string or integer. If integer, we can extract digits by repeated modulo 10, but careful with 0 case. Since N up to 1e5, it's fine. Could read as string to avoid overflow? A[i] up to 10^18 fits in 64-bit, but in Python it's fine as int.
-
Compute mask: For each digit, if digit in {2,3,5,7}, set corresponding bit. Bit mapping: 2->0, 3->1, 5->2, 7->3. Or any order.
-
f[mask] += 1.
-
After processing all numbers, compute cnt[mask] = sum_{superset m' of mask} f[m']. Since only 4 bits, we can for each mask from 0 to 15, for each m' that is superset (i.e., (m' & mask) == mask), sum f[m']. That's O(16*16)=256, trivial.
-
Then compute answer = 0.
-
For each mask from 1 to 15 (non-empty), sign = 1 if popcount(mask) odd else -1? Actually formula: good = Σ_{∅≠S} (-1)^{|S|+1} * C(cnt[S], 3). So for each mask, if bit count = k, contribution = (+1 if k is odd? Actually (-1)^(k+1) = -(-1)^k. So for k=1: (-1)^2=+1; k=2: (-1)^3=-1; k=3: (-1)^4=+1; k=4: (-1)^5=-1. So sign = +1 for odd sizes, -1 for even sizes? Let's compute: k=1 odd -> sign +1; k=2 even -> sign -1; k=3 odd -> +1; k=4 even -> -1. So sign = +1 when popcount odd, -1 when popcount even. But careful: The standard inclusion-exclusion for union of sets: |∪ A_i| = Σ|A_i| - Σ|A_i∩A_j| + Σ|A_i∩A_j∩A_k| - ... So sign is (+) for odd number of sets, (-) for even. Since each T_p corresponds to a set of triples, yes sign for intersection of k sets is (-1)^{k-1}? Actually for union we sum over non-empty subsets, with coefficient (-1)^{|S|-1} |∩_{i∈S} A_i|. So that's exactly what we have: contribution = (-1)^{|S|-1} * C(cnt[S],3). So sign = (-1)^{|S|-1}. So if |S| is odd, |S|-1 even, sign positive; if |S| even, sign negative. So indeed sign positive for odd |S|, negative for even |S|. So we can compute popcount(mask); if popcount%2==1 => add C(cnt,3); else subtract.
-
Answer as integer.
Test sample: Already works.
Edge Cases:
-
If N<3, then answer should be 0, as no triple. Our formula will compute C(cnt,3) which may be 0 for any cnt<3. And sum will be 0.
-
If numbers contain no prime digits (mask=0 for all), then f[0] = N, all other f = 0. Then cnt[mask] for any nonempty mask is 0. So inclusion-exclusion sum = 0, answer=0. Good.
-
If numbers contain only prime digits but mixed sets, inclusion-exclusion handles.
Thus the solution is straightforward.
But wait: Should we consider the condition "i<j<k" only index order? Since we are just counting combinations of numbers irrespective of order, using combination counts is fine because we count triples of distinct indices. Since numbers can be equal, but indices are distinct, combination counts still works: for a given set of indices, we choose any 3 from the N, regardless of values.
Thus answer = number of index triples satisfying condition. The property only depends on the numbers themselves. So combinatorial count based on membership in sets is valid because the property is about the three numbers, not about order. However, we must ensure that we are counting triples of distinct indices, not combinations of values. But using C(cnt,3) for a subset of indices that satisfy a condition is correct because we are selecting 3 distinct indices from the subset of indices with that property