Given an array, determine the number of such three elements in the array: either the three elements are mutually exclusive, or the three elements are mutually exclusive.
Ans = C (n, 3)-three numbers have a pair of mutual elements and a pair of non-Mutual elements sum
How is sum calculated?
For num [I], to calculate the number of elements of the mutual element pnum, then the number of elements of the non-Mutual element is the n-1-pnum, for num [I, the number of tuples that do not meet the condition is pnum * (n-1-pnum ).
Note that in addition to 2, for example (2, 3, 4), it should be calculated once when processing 2, and once when processing 4, but when processing 3, certainly not, because for 3, 2 and 3 are also the same, so in general, every triple is counted once.
Complete code:
/* 1015 ms, 260KB */# include
Const int maxn = 805; int a [maxn]; int gcd (int a, int B) {return B? Gcd (B, a % B): a ;}int main () {int t; scanf (% d, & t); while (t --) {int n; scanf (% d, & n); for (int I = 0; I <n; I ++) scanf (% d, & a [I]); int ans = 0; for (int I = 0; I <n; I ++) {int num = 0; for (int j = 0; j <n; j ++) if (I! = J & gcd (a [I], a [j]) = 1) + + num; ans + = num * (n-num-1 );} printf (% d, n * (n-1) * (n-2)/6-ans/2);} return 0 ;}