3
Idea: http://blog.csdn.net/lyhvoyage/article/details/38455415should be the answer.
Analysis: Mobius Inversion.
In this question, F (d) indicates the number of pairs in the N number where GCD is a multiple of D, F (d) indicates the number of pairs in which the number of N is exactly D,
Then f (d) = Σ F (N) (N % d = 0)
F (d) = Sigma Mu [N/d] * F (N) (N % d = 0)
The above two formulas are the formulas in the Mobius Inversion.
Therefore, F (1) is used to calculate the number of mutual elements ).
According to the formula above, we can obtain F (1) = Σ Mu [N] * F (n ).
So we can find out Mu [] and enumerate n, where Mu [I] is the Mobius function of I.
1 # include <iostream> 2 # include <stdio. h> 3 # include <cstring> 4 # include <cstdlib> 5 using namespace STD; 6 const int n = 1e5 + 1; 7 8 int vis [N]; 9 int Mu [N]; 10 int prime [N], CNT; 11 int date [N]; 12 long ys [N]; 13 int num [N]; 14 void Init () 15 {16 memset (VIS, 0, sizeof (VIS); 17 Mu [1] = 1; 18 CNT = 0; 19 For (INT I = 2; I <n; I ++) 20 {21 if (! Vis [I]) 22 {23 prime [CNT ++] = I; 24 Mu [I] =-1; 25} 26 for (Int J = 0; j <CNT & I * prime [J] <n; j ++) 27 {28 vis [I * prime [J] = 1; 29 if (I % prime [J]) Mu [I * prime [J] =-mu [I]; 30 else31 {32 Mu [I * prime [J] = 0; 33 break; 34} 35} 36} 37} 38 int main () 39 {40 int N, maxn; 41 Init (); 42 while (scanf ("% d", & N)> 0) 43 {44 memset (Num, 0, sizeof (Num )); 45 memset (ys, 0, sizeof (YS); 46 maxn =-1; 47 for (INT I = 1; I <= N; I ++) {48 scanf ("% d", & date [I]); 49 num [date [I] ++; 50 if (date [I]> maxn) maxn = date [I]; 51} 52/*** calculate F (n) */53 for (INT I = 1; I <= maxn; I ++) 54 {55 for (Int J = I; j <= maxn; j = J + I) 56 {57 ys [I] = ys [I] + num [J]; 58} 59} 60 long sum = 0; 61 for (INT I = 1; I <= maxn; I ++) {62 long TMP = (long) YS [I] * (YS [I]-1)/2; 63 sum = sum + Mu [I] * TMP; 64} 65 66 printf ("% i64d \ n", sum); 67} 68 return 0; 69}