hdoj Coprime 5072 (容斥原理) 好题***

211 篇文章 1 订阅
10 篇文章 0 订阅

Coprime

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


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个数,然后让你从其中任意选出三个数满足其中三个数都互质或者都不互质,让你求满足这样选择条件的选择种数。

分析:首先我们从反面考虑这个问题,一个满足条件的选择{ a , b , c },题目要求[(a, b) = (b, c) = (a, c) = 1] or [(a, b) ≠ 1 and (a, c) ≠ 1 and (b, c) ≠ 1],其中(a,b)是a和b的最大公约数。

那么其反面就是,[(a, b) = (b, c) =1 and (a, c)1] or [(a, b) = 1 and (a, c) ≠ 1 and (b, c) ≠ 1],即其中三个元素的组合两种互质,一种不互质或者两种不互质,一种互质。那么当其中一个数 a 固定的时候,另外两个元素的选择只能是一个与 a 互质,一个不互质。

既然这样我们就可以转化为快速求序列中一个数 x 与其他数互质数的个数,不互质 = n - 互质 - 1(当前元素).

这时候就用到容斥原理,对于一个数 x ,我们首先分解质因子。比如30 ,质因子有2,3,5

那么与其互质的数的个数 = num【2】 + num【3】 + num【5】 - num【2*3】 - num【2*5】 - num【3*5】 + num【2*3*5】

其中num【x】 表示在整个序列中以 x 为因子的数的个数。

 
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#define N 100010
#define ll long long
using namespace std;
int num[N];
int f[N];
int vis[N];
bool test(int x)
{
	if(x==1)
		return false;
	for(int i=2;i*i<=x;i++)
	{
		if(x%i==0)
			return false;	
	}
	return true;
}
ll solve(int n)
{
	ll ans=0;
	for(int i=0;i<n;i++)
	{
		int res=0;
		int tmp=num[i];
		vector<int>v;
		for(int j=2;j*j<=tmp;j++)
		{
			if(tmp%j==0)
			{
				v.push_back(j);
				while(tmp%j==0)
					tmp/=j;
			}
		}
		if(test(tmp))
			v.push_back(tmp);
		for(int st=1;st<(1<<v.size());st++)
		{
			int pps=0;
			int kks=1;
			for(int j=0;j<v.size();j++)
			{
				if(st&(1<<j))
				{
					pps++;
					kks*=v[j];
				}
			}
			if(pps&1)
				res+=f[kks];
			else
				res-=f[kks];
		}
		if(res==0)
			continue;
		ans+=(ll)(res-1)*(n-res);
		v.clear();		
	}
	return ans/2;
}
int main()
{
	int n,T;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);
		memset(vis,0,sizeof(vis));
		memset(f,0,sizeof(f));
		for(int i=0;i<n;i++)
		{
			scanf("%d",&num[i]);
			vis[num[i]]=true;
		}
		for(int i=2;i<N;i++)
		{
			for(int j=i;j<N;j+=i)
			{
				if(vis[j])
					f[i]++;
			}
		}
		ll cnt=(ll)n*(n-1)*(n-2);
		cnt/=6;
		printf("%lld\n",cnt-solve(n));
	}
	return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值