Coprime
Time Limit: 2000/1000 MS (Java/others) memory limit: 262144/262144 K (Java/Others)
Total submission (s): 130 accepted submission (s): 59
Problem descriptionthere are n people standing in a line. Each of them has a unique ID number.
Now the Ragnarok is coming. we shoshould choose 3 people to defend the edevil. as a group, the 3 people shoshould be able to communicate. they are able to communicate if and only if their ID numbers are pairwise Coprime or pairwise not Coprime. in other words, if their ID numbers are a, B, c, then they can communicate if and only if [(a, B) = (B, c) = (, c) = 1] or [(a, B) =1 and (a, c) =1 and (B, c) =1], where (x, y) denotes the greatest common divisor of X and Y.
We want to know how many 3-people-groups can be chosen from the N people.
Inputthe first line contains an integer T (T ≤ 5), denoting the number of the test cases.
For each test case, the first line contains an integer N (3 ≤ n ≤105), denoting the number of people. the next line contains N distinct integers A1, A2 ,..., an (1 ≤ AI ≤ 105) separated by a single space, where Ai stands for the ID number of the I-th person.
Outputfor each test case, output the answer in a line.
Sample Input
151 3 9 10 2
Sample output
4
Question: Find the number of sets that satisfy the three numbers, so that the number in each set satisfies the mutual quality or the mutual quality.
Idea: Because N is large, it is necessary to seek an nlogn solution.
You can find the answer to this question by solving the opposite problem.
The three numbers in the set. The opposite of the Two or two non-mutual quality is at least one pair of non-mutual quality and at least one pair of mutual quality.
Set these three numbers to A, B, and C. Now, if A and B are mutually qualitative, and B and C are not mutually qualitative, set the set digit temp to meet such requirements, then the answer is C (n, 3)-temp/2, the reason for division of 2 is that B is considered as the intermediate number
Now the problem is solved as temp
For each B, set the number of mutual quality with it to cnt1, and the number of non-mutual quality with it to cnt2. Obviously, cnt1 + cnt2 + 1 = N
The temp value of B is CNT * cnt2, where you only need to find cnt2. Then, B is decomposed into quality factors, and the number of these quality factors will not exceed 6, obviously, it can be solved based on the principle of volume rejection after compression.
Now the problem is transformed into finding out how many numbers of X are multiples of X, which can be obtained by filtering by prime numbers, and the problem can be solved.
2014 Anshan field competition C-question hdu5072 (principle of screening and rejection)