码迷,mamicode.com
首页 > 其他好文 > 详细

hdu5072 Coprime 2014鞍山现场赛C题 计数+容斥

时间:2014-10-28 00:51:09      阅读:360      评论:0      收藏:0      [点我收藏+]

标签:hdu

http://acm.hdu.edu.cn/showproblem.php?pid=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 Description
There 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.
 

Input
The 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.
 

Output
For each test case, output the answer in a line.
 

Sample Input
1 5 1 3 9 10 2
 

Sample Output
4
 

Source

题意:给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;
}




hdu5072 Coprime 2014鞍山现场赛C题 计数+容斥

标签:hdu

原文地址:http://blog.csdn.net/neko01/article/details/40518947

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!