Coprime
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 142 Accepted Submission(s): 62
Problem Description
There are n people standing in a line. Each of them has a unique id number.
Now the Ragnarok is coming. We should choose 3 people to defend the evil. As a group, the 3 people should 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) = (a, 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.
Input
The 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 ≤ 10
5), denoting the number of people. The next line contains n distinct integers a
1, a
2, . . . , a
n(1 ≤ a
i ≤ 10
5) separated by a single space, where a
i stands for the id number of the i-th person.
Output
For each test case, output the answer in a line.
Sample Input
Sample Output
Source
2014 Asia AnShan Regional Contest
题目大意
给定n个数,求在其中选定三个数,三个数全部互质或者互相全部不互质的组数
解题思路
现场想到莫比乌斯没有出……模型没有建立好……
首先想到所有可能的出现的组数是C(n,3)组
而类似红蓝三角形的做法便是枚举出和特定的数互质与不互质的数的个数,乘积和cn待处理
与某一个特定的数互质的数的数量可以通过容斥或者莫比乌斯反演求出(所有其约数的倍数之个数*约数对应的mu值)
而每个数的倍数的个数可以通过nlgn的复杂度求出
而对于每一个不符合要求的三角形,三组数之中一定有两组互质一组不互质,或者一组互质两组不互质,会被重复计算两次,答案即C(n,3)-cn/2;
code:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include