hdu5072 Coprime 2014鞍山現場賽C題 計數+容斥,hdu5072coprime

來源:互聯網
上載者:User

hdu5072 Coprime 2014鞍山現場賽C題 計數+容斥,hdu5072coprime

http://acm.hdu.edu.cn/showproblem.php?pid=5072

CoprimeTime 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 should choose 3 people to defend the evil. As a group, the 3 people should 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) = (a, 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 
題意:給n個數,問三個數兩兩互質或者兩兩不互質的三元組有多少種。。分析:當時做重現的真心不會233,今天翻大白髮現竟然有這個模型233。(p105 問題6)就是經典的單色三角形模型,從反面考慮,求出所以非單色三角形即可。對於每一個數,求與其互質和不互質的個數是多少個,記與其互質的是ai個,那麼包括第i個數的就有ai*(n-1-ai)種,最後求和,注意到每個非單色三角形出現了兩次,所以還要除2.最後用總的情況C(n,3)減去就好。現在的問題是如何求與每個數互質的個數。求1到k與x互質的數的個數用容斥搞,詳見poj1142.那麼這道題求n個數與x互質的數個數也用容斥。由於數的範圍不大,只有10^5,所以我們可以預先處理出1到10^5每個數作為是這n個數中多少個數的因子。然後求出x的質因子,然後再容斥計算就好。。。。唉。。還是見識短淺做題少。
/** * @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;}







聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.