2014鞍山现场赛C题HDU5072(素筛+容斥原理)

Coprime

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


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

题意:求出满足三个数的集合个数,使得每个集合里的数满足两两互质或两两不互质

思路:由于n很大,一定是得寻求一种nlogn的解法

            注意到这题可以通过求解反面来求得答案

            集合里的三个数,两两互质或两两不互质的反面是 至少有一对不互质和至少有一对互质

            设这三个数为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值为cnt*cnt2,其中只需要求出cnt2即可,将b分解质因子,然后这些质因子的个数不会超过6个,显然可以状压以后容斥原理求解

            现在问题进而转化为求出一个数x有多少个数是x的倍数,这个用素数筛选可以求得,问题得以解决

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
       
         #include 
        
          #include 
         
           using namespace std; typedef __int64 ll; const int MAXN = 100010; int isprime[MAXN]; int prime[MAXN]; int have[MAXN]; int cnt; int is[MAXN]; int a[MAXN]; void init() { memset(have,0,sizeof(have)); memset(isprime,0,sizeof(isprime)); memset(is,0,sizeof(is)); } void getprime() { cnt=0; for(int i=2;i<=100000;i++){ if(!isprime[i])prime[cnt++]=i; int res=0; for(int j=i+i;j<=100000;j+=i){ if(is[j])res++; isprime[j]=1; } if(is[i])res++; have[i]=res; } } ll calc(int n,int m) { int res=(int)sqrt(n+0.5); int stk[8]; int top=0; for(int i=0;prime[i]<=res;i++){ if(n%prime[i])continue; stk[top++]=prime[i]; while(n%prime[i]==0)n/=prime[i]; } if(n>1)stk[top++]=n; int ans=0; for(int s=1;s<(1< 
          
            >=1; printf("%I64d\n",(ll)n*(n-1)*(n-2)/6-ans); } return 0; } 
           
          
         
       
      
      
     
     
    
    
   
   
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值