Title Link: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=71738
Test instructions: give you an integer sequence a1, A2, A3, ..., an. GCD (AI, aj) = 1 and the logarithm of I < J.
Idea: The formula can be obtained quickly using the inverse of the A1, but we need to know the number of multiples of 1, 2, 3, ..., Max (A2, ..., an) in the sequence. We use Num[i]=k to indicate that the number of k in the sequence is a multiple of I, then the effect of this part on the result is mu[i]* (k-1) * K/2. The final result is Sigma (mu[i]* (k-1) * K/2).
Code
1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5typedefLong LongLL;6 Const intMAXN =250000;7 intNUM[MAXN];//Num[i] Indicates the number of satisfied (I|ak)8 intTMP[MAXN];//mark which numbers have9 BOOLCHECK[MAXN];Ten intPRIMES[MAXN]; One intMU[MAXN]; A - voidMoblus () - { thememset (check,false,sizeof(check)); -mu[1] =1; - intCNT =0; - for(inti =2; i < MAXN; ++i) { + if(!Check[i]) { -primes[cnt++] =i; +Mu[i] =-1; A } at for(intj =0; J < CNT; ++j) { - if(i * primes[j] > MAXN) Break; -Check[i * Primes[j]] =true; - if(i% primes[j] = =0) { -Mu[i * Primes[j]] =0; - Break; in}Else { -Mu[i * Primes[j]] =-Mu[i]; to } + } - } the } * $ intMain ()Panax Notoginseng { - Moblus (); the intN; + while(SCANF ("%d", &n)! =EOF) { Amemset (NUM,0,sizeof(num)); thememset (TMP,0,sizeof(TMP)); + intTmax =0; - for(inti =0; I < n; ++i) { $ intx; $scanf"%d", &x); -++Tmp[x]; -Tmax =Max (Tmax, x); the } - for(inti =1; I <= Tmax; ++i) {Wuyi for(intj = i; J <= Tmax; J + =i) { theNum[i] + =Tmp[j]; - } Wu } -LL ans =0; About for(inti =1; I <= Tmax; ++i) { $Ans + = (LL) mu[i] * num[i] * (Num[i]-1) /2; - } -printf"%lld\n", ans); - } A return 0; +}
Bzoj 1114 number theory (Möbius inversion + preprocessing)