Noj 2079 Prime (Möbius inversion)

Source: Internet
Author: User


Prime

Time limit (normal/java): 1000ms/3000ms Run memory limit: 65536KByte
Total Submissions: 267 tested by: 11

Contest description
For a given number of N, 22 mutually exclusive logarithm is obtained. Mutual exclusion refers to the two number of greatest common divisor is 1

Input
First behavior Example number T (t<=5)
For each example, the first behavior is an integer n (2=<n<=10^5), which represents the number of numbers.
The next line contains the number of N, A1,a2,..., an (1<=ai<=10^5)

Output
For each sample, output the answer on one line.

Sample input
1
2
2 3

Sample output
1

Title Link: http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=2079


Title analysis: First preprocessing at the Möbius function of 1e5, because mob[i]=1 indicates that I is the product of an even number of factors, mob[i]=-1 that I is the product of the odd number of factors, mob[i]=0 represents other
Back to this problem, first for the total collection I arbitrarily take out two number of cases of C (n,2) that is n * (n-1)/2, and then subtract the case, that is, GCD 2 gcd 3, here is a problem, because if GCD 6, then it was reduced two times, so to let out, That is to take the total situation-gcd only by a factor of one element of the situation +GCD only two factors constitute the case ... For each set of GCD values we can record with a num array. Then we can see that the Möbius function is powerful, the corresponding positive and negative number is actually a mob array, such as the calculation of GCD to 2, mob[2]==-1, so minus num[2] * (num[2]-1)/2,3 time is also reduced, 6 is added, Coincides with the definition of the mob function.

Note: Because the Möbius function is an integrable function, it is possible to use a linear sieve to derive


#include <cstdio> #include <cstring> #include <algorithm> #define LL long longusing namespace Std;int    Const MAX = 1e5 + 5;int P[max], Cnt[max], Num[max], Mob[max];bool prime[max];int pnum, MA, n;void Mobius ()//solution Möbius function {    Pnum = 0;    MOB[1] = 1;    Memset (Prime, true, sizeof (prime));            for (int i = 2; i < MAX; i++) {if (Prime[i]) {p[pnum + +] = i;        Mob[i] =-1;            } for (int j = 0; J < pnum && I * p[j] < MAX; J + +) {Prime[i * P[j]] = false;                if (i% p[j] = = 0) {Mob[i * p[j]] = 0;            Break        } Mob[i * P[j]] =-mob[i];    }}}ll cal () {ll ans = (LL) n * (n-1)/2;        for (int i = 2; I <= ma; i++) {num[i] = 0;   for (int j = i; J <= Ma; j + = i) num[i] + = cnt[j]; Gets the number of elements of the set GCD to i} for (int i = 2; I <= ma; i++) ans + = (ll) mob[i] * num[i] * (Num[i]-1)/2; return ans;}    int main () {Mobius ();    int T;    scanf ("%d", &t);        while (t--) {memset (CNT, 0, sizeof (CNT));        Ma = 0;        scanf ("%d", &n);            for (int i = 0; i < n; i + +) {int tmp;            scanf ("%d", &tmp);            CNT[TMP] + +;        MA = max (MA, TMP);    } printf ("%i64d\n", Cal ()); }}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Noj 2079 Prime (Möbius inversion)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.