Nyoj 1066 co-prime (number theory)

Source: Internet
Author: User

Co-Prime Time limit: +Ms | Memory Limit:65535KB Difficulty:3
Descriptive narrative

This problem are so easy! Can You solve it?

Given a sequence which contains n integers a1,a2......an, your task is to find how many pair (AI, AJ) (I < j) that AI and AJ is co-prime.

Input
There is multiple test cases.
Each test case conatains the Line,the first line contains a single integer n,the second line contains n integers.
All the integers are not greater than 10^5.
Output
for each test case, you should output one of the contains the answer.
Example input
31 2 3
Example output
3

Test instructions: give n positive integers to find out how many of these n numbers are in the number of interconnects.

Analysis:

Fr=aladdin "> The inverse of the mo.

f (d) " Span style= "Font-family:tahoma" >n number of GCD d f (d) n GCD happens to be d

the F (d) =∑f (n) (n d = = 0)

F (d) =∑mu[n/d] * f (n) (n%d = = 0)

The above two formulas are the formulas in Möbius inversion.

So there's a number of pairs that require a mutual-vegetarian. is to ask f (1).

And according to the above formula can be obtained f (1) =∑mu[n] * f (n).

So get mu[] and enumerate n . Mu[i] is the Möbius function of I.

#include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int maxn = 1e5 + 10; typedef long Long Ll;int CNT[MAXN], PRI[MAXN], NUM[MAXN], Pri_num, MU[MAXN], VIS[MAXN], a[maxn];void Mobius (int n)//sieve method to find Mo    Biousse function {pri_num = 0;    memset (Vis, 0, sizeof (VIS));    VIS[1] = mu[1] = 1;            for (int i = 2; I <= n; i++) {if (!vis[i]) {pri[pri_num++] = i;        Mu[i] =-1;            } for (int j = 0; J < Pri_num; J + +) {if (i * pri[j] > N) break;            VIS[I*PRI[J]] = 1;                if (i% pri[j] = = 0) {Mu[i*pri[j]] = 0;            Break        } Mu[i*pri[j]] =-mu[i]; }}}ll get (int x) {return (LL) x * (x-1)/2;}    int main () {Mobius (100005);    int n;        while (~SCANF ("%d", &n)) {int Mmax = 0;            for (int i = 1; I <= n; i++) {scanf ("%d", &a[i]);        Mmax = Max (Mmax, A[i]);        } memset (CNT, 0, sizeof (CNT)); MEmset (num, 0, sizeof (num));        for (int i = 1; I <= n; i++) num[a[i]]++;        for (int i = 1, i <= Mmax; i++) for (int j = i; J <= Mmax; j + = i) cnt[i] + = num[j];        LL ans = 0;        for (int i = 1; I <= Mmax; i++) ans + = Get (Cnt[i]) * Mu[i];    printf ("%lld\n", ans); } return 0;}

Nyoj 1066 co-prime (number theory)

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.