数学hdu5072

F.A.Q
Hand In Hand
Online Acmers
Forum | Discuss
Statistical Charts
Problem Archive
Realtime Judge Status
Authors Ranklist
 
      C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
Virtual Contests 
    DIY | Web-DIY beta
Recent Contests

Coprime

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 109    Accepted Submission(s): 51


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个数问有多少个三元组(a,b,c),满足要么两两互质,要么两两不互质

思路:设这三个数为a,b,c,现在假设a与b互质,b与c不互质,设满足这样的集合数位temp,那么答案为C(n,3)-temp/2,之所以除2是因为b作为中间的数算重了一次。 现在问题转为求解temp。  对于每个b,设与它互质的数个数为cnt1,与它不互质的个数为cnt2,显然cnt1+cnt2+1=n。 那么对于b的temp值为cnt1*cnt2,其中只需要求出cnt2即可,将b分解质因子,然后这些质因子的个数不会超过6个,显然可以状压以后容斥原理求解。 现在问题进而转化为求出一个数x有多少个数是x的倍数,这个用素数筛选可以求得,问题得以解决。

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=100010;
typedef long long LL;
int N,a[maxn],num;
int prime[maxn];
bool is_prime[maxn];
int have[maxn];
void getprime()
{
    num=0;
    memset(is_prime,0,sizeof(is_prime));
    is_prime[1]=1;
    for(int i=2;i<maxn;i++)
    {
        if(!is_prime[i])
            prime[num++]=i;
        for(int j=0;j<num;j++)
        {
            if(i*prime[j]>maxn)break;
            is_prime[i*prime[j]]=1;
            if(i%prime[j]==0)break;
        }
    }
}
LL cal(int x)
{
    vector<int>  q;
    for(int i=0;i<num&&x>=prime[i];i++)
    {
        if(x%prime[i]==0)q.push_back(prime[i]);
        while(x%prime[i]==0)x/=prime[i];
        if(!is_prime[x]){q.push_back(x);break;}
    }
    int len=q.size();
    LL ans=0;
    for(int s=1;s<(1<<len);s++)
    {
        int cnt=0;
        int u=1;
        for(int i=0;i<len;i++)
            if(s&(1<<i))
                cnt++,u*=q[i];
        if(cnt&1)ans=ans+have[u];
        else ans=ans-have[u];
    }
    if(ans)ans--;
    return ans*(N-1-ans);
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&N);
        for(int i=1;i<=N;i++)scanf("%d",&a[i]);
        getprime();
        memset(have,0,sizeof(have));
        for(int i=1;i<=N;i++)
        {
            int x=a[i];
            for(int j=1;j*j<=a[i];j++)
                if(a[i]%j==0)
                {
                    have[j]++;
                    if(a[i]/j!=j)have[a[i]/j]++;
                }
        }
        LL ans=0;
        for(int i=1;i<=N;i++)
            ans+=cal(a[i]);
        printf("%I64d\n",(LL)N*(N-1)*(N-2)/6-ans/2);
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值