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 ≤ 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.
Output
For each test case, output the answer in a line.
Sample Input
1 5 1 3 9 10 2
Sample Output
4
Source
题意:从一个数组中找出所有 3个数 都 相互互质 和 相互不互质 的总个数
思路:首先先转化为求 与一个数互质和不互质的个数,然后将互质和不互质相乘,然后总数减去即可。
接下来就是怎么求互质和不互质,用到了容斥原理来求,模板套一下就可以了
sum【i】数组表示i这个数是数组中的元素的因子的个数
该题还可以先求出所有的素数,范围可以自己确定,然后在分解质因数的时候可以起到优化的作用,如注释掉的部分
1 #include2 #include 3 #include 4 using namespace std; 5 #define N 100006 6 #define ll long long 7 ll n; 8 ll a[N]; 9 //ll prime[N];10 //ll num[N];11 //ll k=0;12 ll fac[N];//分解质因数的质因数13 ll sum[N];14 ll have[N];15 /*void init()16 {17 memset(num,0,sizeof(num));18 for(int i=2;i 1) fac[num++]=m;49 for(ll j=1;j<(1<