HDU 5072 Coprime (单色三角形+容斥原理)

原创 2015年07月09日 13:10:08

题目链接:Coprime


题面:

Coprime

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


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
2014 Asia AnShan Regional Contest

解题:
    题意求找出三数互质或都不互质的组数,直接肯定不行。此题原型为单色三角形,结果为C(3,n)-res,其中res为每个与每个数互质和不互质数量的乘积的累加。求与每个数不互质的数量,用到了容斥原理。看似简单的原理应用却这么广泛,如果没做过容斥原理的题目,可以先试一下HDU 1796。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
bool status[100010];
int factor[100010][8];
int store[100010],cnt[100010];
long long ans[100010],res;
int one_amount[300];
int refl[300][8];
int cal[8];
void prep()
{
    memset(refl,0,sizeof(refl));
    int cont=0,temp;
   for(int i=0;i<256;i++)
   {
       temp=i;
       one_amount[i]=cont=0;
       while(temp)
       {
         if(temp%2)
         {
             one_amount[i]++;
             refl[i][cont]=1;
         }
         cont++;
         temp/=2;
       }
   }
}
bool is_prime(int a)
{
	if(a<=3)return true;
	int x=sqrt(1.0*a);
	for(int i=2;i<=x;i++)
	{
		if(a%i==0)
			return false;
	}
	return true;
}
long long C(int x,int y)
{
	long long res=1;
	for(int i=1;i<=x;i++)
	{
		res=res*(y-i+1)/i;
	}
	return res;
}
int main()
{
	int t,n,p,tmp;
	long long temp;
	scanf("%d",&t);
	prep();
	while(t--)
	{
		res=0;
		memset(status,0,sizeof(status));
		memset(factor,0,sizeof(factor));
		memset(cnt,0,sizeof(cnt));
		memset(ans,0,sizeof(ans));
		scanf("%d",&n);
		for(int i=0;i<n;i++)
			scanf("%d",&store[i]);
		for(int i=0;i<n;i++)
			status[store[i]]=1;
        for(int i=2;i<=100000;i++)
		{
	       if(is_prime(i))
		   {
			   for(int j=i;j<=100000;j+=i)
			   {
				   if(status[j])
				   {
					   cnt[i]++;
				   }
				   p=0;
				   while(factor[j][p])
				   {
					   p++;
				   }
				   factor[j][p]=i;
			   }
		   }
		   else
		   {
             for(int j=i;j<=100000;j+=i)
			   {
				   if(status[j])
				   {
					   cnt[i]++;
				   }
			   }

		   }
		}
        for(int i=0;i<n;i++)
		{
           tmp=store[i];
           p=0;
		   while(factor[tmp][p])
		   {
			   cal[p]=factor[tmp][p];
			   p++;
		   }
		   if(p==0)
		   {
			   ans[i]=0;
			   continue;
		   }
           tmp=1<<p;
		   for(int j=1;j<tmp;j++)
           {
              temp=1;
              for(int k=0;k<p;k++)
              {
                  if(refl[j][k])
                 {
                    temp=temp*cal[k];
                 }
			   }
              if(one_amount[j]%2)
              ans[i]+=cnt[temp];
              else
              ans[i]-=cnt[temp];
          }
		   ans[i]-=1;
		}
		for(int i=0;i<n;i++)
		{
			res+=(ans[i]*(n-ans[i]-1));
		}
		res/=2;
		printf("%I64d\n",C(3,n)-res);
	}
	return 0;
}

总结:
用好容斥原理的关键在于,搞清楚集合的交的含义。
 

版权声明:欢迎交流!

相关文章推荐

hdu 5072 Coprime(单色三角形问题+容斥原理)

Coprime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Tota...

HDU 5072 Coprime (单色三角形问题+容斥原理)

单色三角形问题,HDU5072 Coprime

hdu 5072 Coprime 2014 Asia AnShan Regional Contest 单色三角形模型+容斥 好题!

题意:给了n个不同的数,要求有多少个三元组,两两互质 或者 两两不互质。 思路:         单色三角形模型:给定空间的n个点,其中没有三点共线。每两个点之间都用红色或者黑色线段连接,求3条边...

HDU5072-Coprime-2014鞍山站C题-单色三角形+容斥

题目链接题意:是给你n个数,然后问你有多少组数(每组3个数)满足两两互质或者两两不互质。思路:问题可以转换成一张完全图中有n个点,每两个点间有一条线,互质是黑色,不互质是白色。问图种有多少个单色三角形...

hdu5072 Coprime 2014鞍山现场赛C题 容斥原理+单色三角

鞍山赛区比赛的时候这题没有任何思路,比赛结束后clj提到了单色三角,后来在做白书的训练指南时候看到了这点,然后就把这题写一写。 用到的几个知识点: 1.单色三角 平面中n个点没有三点共线,每两个...

hdu 5072 Coprime(同色三角形+容斥)

http://acm.hdu.edu.cn/showproblem.php?pid=5072 单色三角形模型 现场赛和队友想了3个小时,最后发现想跑偏了。感觉好可惜的一道题,要...

HDU 5072 Coprime (莫比乌斯反演+容斥+同色三角形)

HDU 5072 Coprime (莫比乌斯反演+容斥+同色三角形)

HDU 5072 - Coprime(容斥原理+素筛)

Problem Description There are n people standing in a line. Each of them has a unique id number. ...

hdu5072 Coprime | 2014鞍山赛区C题 | 容斥原理

容斥原理

hdu 5072 Coprime 容斥原理

Coprime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:HDU 5072 Coprime (单色三角形+容斥原理)
举报原因:
原因补充:

(最多只允许输入30个字)