Hdu5072 Coprime 2014 Anshan field competition C-question count + reprimand

Source: Internet
Author: User
Tags greatest common divisor

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 5072

Coprime Time Limit: 2000/1000 MS (Java/others) memory limit: 262144/262144 K (Java/Others)
Total submission (s): 354 accepted submission (s): 154


Problem descriptionthere are n people standing in a line. Each of them has a unique ID number.

Now the Ragnarok is coming. we shoshould choose 3 people to defend the edevil. as a group, the 3 people shoshould be able to communicate. they are able to communicate if and only if their ID numbers are pairwise Coprime or pairwise not Coprime. in other words, if their ID numbers are a, B, c, then they can communicate if and only if [(a, B) = (B, c) = (, c) = 1] or [(a, B) =1 and (a, c) =1 and (B, c) =1], where (x, y) denotes the greatest common divisor of X and Y.

We want to know how many 3-people-groups can be chosen from the N people.
Inputthe first line contains an integer T (T ≤ 5), denoting the number of the test cases.

For each test case, the first line contains an integer N (3 ≤ n ≤105), denoting the number of people. the next line contains N distinct integers A1, A2 ,..., an (1 ≤ AI ≤ 105) separated by a single space, where Ai stands for the ID number of the I-th person.
Outputfor each test case, output the answer in a line.
Sample Input
151 3 9 10 2

Sample output
4

Source2014 Asia Anshan Regional Contest
Question: give n a number and ask how many types of three tuples are mutually qualitative or not mutually qualitative .. Analysis: At that time, we did not really see 233 of the results. Today, we found that this model was 233 million. (P105 question 6) is the classic monochrome triangle model. You can find a non-monochrome triangle from the opposite side. For each number, the number of mutual quality and non-mutual quality is how many, remember and their mutual quality is Ai, then the number of I contains ai * (n-1-ai, the final summation is noticed that each non-monochrome triangle appears twice, so we need to divide it by 2. in the end, subtract the total number C (n, 3. The question now is how to calculate the number of mutual quality with each number. For details about how to calculate the number of mutual quality between 1 to K and X, refer to poj1142. the number of mutual quality between N and X is also used. Since the number range is small and only 10 ^ 5 is used, we can pre-process each number from 1 to 10 ^ 5 as a factor for the number of N numbers. Then obtain the quality factor of X, and then let the calculation be done .... Alas .. There are still few short questions.
/** * @author neko01 *///#pragma comment(linker, "/STACK:102400000,102400000")#include <cstdio>#include <cstring>#include <string.h>#include <iostream>#include <algorithm>#include <queue>#include <vector>#include <cmath>#include <set>#include <map>using namespace std;typedef long long LL;#define min3(a,b,c) min(a,min(b,c))#define max3(a,b,c) max(a,max(b,c))#define pb push_back#define mp(a,b) make_pair(a,b)#define clr(a) memset(a,0,sizeof a)#define clr1(a) memset(a,-1,sizeof a)#define dbg(a) printf("%d\n",a)typedef pair<int,int> pp;const double eps=1e-9;const double pi=acos(-1.0);const int INF=0x7fffffff;const LL inf=(((LL)1)<<61)+5;const int N=100005;const int M=500;bool isprime[M];int prime[M];int tot=0,n;int fac[50];bool have[N];int a[N];int s[N];  //sum[i]表示a[i]中能整除i的个数void getprime(){    for(int i=2;i<=M;i++)    {        if(isprime[i]) continue;        prime[tot++]=i;        for(int j=i*i;j<=M;j+=i)            isprime[i]=true;    }}LL gao(){    LL ans=0;    for(int i=0;i<n;i++)    {        int m=a[i],num=0;        LL sum=0;        for(int j=0;j<tot&&prime[j]*prime[j]<=m;j++)        {            if(m%prime[j]==0)            {                fac[num++]=prime[j];                while(m%prime[j]==0)                    m/=prime[j];            }        }        if(m!=1) fac[num++]=m;        for(int j=1;j<(1<<num);j++)        {            int op=0,tmp=1;            for(int k=0;k<num;k++)            {                if((1<<k)&j)                {                    tmp*=fac[k];                    op++;                }            }            if(op&1) sum+=s[tmp];            else sum-=s[tmp];        }        if(sum==0) continue;        ans+=(sum-1)*(n-sum);  //sum不互质的数包括了a[i]自身故减1    }    return ans/2;}int main(){    int t;    scanf("%d",&t);    getprime();    while(t--)    {        scanf("%d",&n);        clr(have);        clr(s);        for(int i=0;i<n;i++)        {            scanf("%d",&a[i]);            have[a[i]]=true;        }        for(int i=2;i<N;i++)            for(int j=i;j<N;j+=i)                if(have[j]) s[i]++;        LL ans=(LL)n*(n-1)*(n-2)/6;        ans-=gao();        printf("%I64d\n",ans);    }    return 0;}




Hdu5072 Coprime 2014 Anshan field competition C-question count + reprimand

Related Article

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.