Mehta is a forever alone and desperate guy. He has a crush on N girls of his society. He…
- User
Mehta is a forever alone and desperate guy. He has a crush on N girls of his society. He wants to impress them all and so he needs to do their task collectively.All the girls give him a number which he stores in an array named A of size N. To do their task, he has to report the number of triplets (i,j,k) in the array A, with i < j < k such that the triplets have at least one prime digit in common. Input & Output: The first line of the input contains an integer N. The next N lines has a number on each, which denote the array A.
You need to print on one line, the number of triples with the condition mentioned in the problem statement.
Constraints:
1 ≤ N ≤ 10 ^ 5 0 ≤ A[i] ≤ 10 ^ {18} for all index i in the array A.
Sample Input:
5 21 22 23 24 25
Sample Output: 10
SAMPLE INPUT 5 21 22 23 24 25
SAMPLE OUTPUT 10
Explanation
In the given sample each i,j,k has one prime digit common that is 2. So, total triplets are 5C3 which is 10.
Assistant
n = int(input()) subset_counts = [0] * 16 # 0 to 15 for _ in range(n): s = input().strip() mask = 0 for c in s: if c == '2': mask |= 1 << 0 elif c == '3': mask |= 1 << 1 elif c == '5': mask |= 1 << 2 elif c == '7': mask |= 1 << 3 if mask == 0: continue current = mask while current != 0: subset_counts[current] += 1 current = (current - 1) & mask ans = 0 for s in range(1, 16): cnt = subset_counts[s] if cnt < 3: continue k = bin(s).count('1') sign = (-1) ** (k + 1) ans += sign * (cnt * (cnt - 1) * (cnt - 2) // 6) print(ans)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.