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

2 篇文章 0 订阅

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 ≤ 10 5), denoting the number of people. The next line contains n distinct integers a 1, a 2, . . . , a n(1 ≤ a i ≤ 10 5) separated by a single space, where a i 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];  //s[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;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值