Coprime
Time Limit: 1000ms
Memory Limit: 262144KB
This problem will be judged on HDU. Original ID: 507264-bit integer IO format: %I64d Java class name: Main
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.
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.
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
解题:容斥
单色三角形模型据说是,每当我选定一个点的时候,剩下的有互斥*不互斥的数量中选法,但是要除以2,因为这种选法,会使得同样的三角形出现两次
1 #include <bits/stdc++.h> 2 using namespace std; 3 using LL = long long; 4 const int maxn = 100010; 5 int p[maxn],num[maxn],tot; 6 bool np[maxn] = {true,true}; 7 vector<int>F; 8 void init(){ 9 for(int i = 2; i < maxn; ++i){ 10 if(!np[i]) p[tot++] = i; 11 for(int j = 0; j < tot && p[j]*i < maxn; ++j){ 12 np[p[j]*i] = true; 13 if(i%p[j] == 0) break; 14 } 15 } 16 } 17 void Fac(int val){ 18 F.clear(); 19 for(int i = 0; i < tot && p[i]*p[i] <= val; ++i){ 20 if(val % p[i] == 0){ 21 while(val%p[i] == 0) val /= p[i]; 22 F.push_back(p[i]); 23 } 24 } 25 if(val > 1) F.push_back(val); 26 } 27 int a[maxn]; 28 int main(){ 29 int kase,n; 30 init(); 31 scanf("%d",&kase); 32 while(kase--){ 33 memset(num,0,sizeof num); 34 scanf("%d",&n); 35 for(int i = 0; i < n; ++i){ 36 scanf("%d",a + i); 37 num[a[i]]++; 38 } 39 for(int i = 2; i < maxn; ++i) 40 for(int j = i + i; j < maxn; j += i) 41 num[i] += num[j]; 42 LL ret = 0; 43 for(int i = 0; i < n; ++i){ 44 Fac(a[i]); 45 LL ans = 0; 46 for(int j = 1,sz = (1<<F.size()); j < sz; ++j){ 47 int cnt = 0,tmp = 1; 48 for(int k = 0; k < F.size(); ++k){ 49 if((j>>k)&1){ 50 tmp *= F[k]; 51 ++cnt; 52 } 53 } 54 if(cnt&1) ans += num[tmp] - 1; 55 else ans -= num[tmp] - 1; 56 } 57 ret += ans*(n - ans - 1); 58 } 59 ret = (LL)n*(n-1)*(n-2)/6 - (ret>>1); 60 printf("%I64d ",ret); 61 } 62 return 0; 63 }