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
import sys from collections import defaultdict def generate_subsets(mask): subsets = [] s = mask while True: if s != 0: subsets.append(s) if s == 0: break s = (s - 1) & mask return subsets def main(): prime_to_bit = {'2': 0, '3': 1, '5': 2, '7': 3} counts = defaultdict(int) n = int(sys.stdin.readline()) for _ in range(n): num_str = sys.stdin.readline().strip() primes_present = set() for c in num_str: if c in prime_to_bit: primes_present.add(c) if not primes_present: continue mask = 0 for c in primes_present: mask |= 1 << prime_to_bit[c] subsets = generate_subsets(mask) for s in subsets: counts[s] += 1 total = 0 for s in counts: m = bin(s).count('1') c = counts[s] if c < 3: continue comb = c * (c - 1) * (c - 2) // 6 sign = (-1) ** (m - 1) total += sign * comb print(total) if __name__ == '__main__': main()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.