码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3904(容斥原理)

时间:2015-06-04 22:44:37      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:数学   容斥原理   

Sky Code
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1750   Accepted: 545

Description

Stancu likes space travels but he is a poor software developer and will never be able to buy his own spacecraft. That is why he is preparing to steal the spacecraft of Petru. There is only one problem – Petru has locked the spacecraft with a sophisticated cryptosystem based on the ID numbers of the stars from the Milky Way Galaxy. For breaking the system Stancu has to check each subset of four stars such that the only common divisor of their numbers is 1. Nasty, isn’t it? Fortunately, Stancu has succeeded to limit the number of the interesting stars to N but, any way, the possible subsets of four stars can be too many. Help him to find their number and to decide if there is a chance to break the system.

Input

In the input file several test cases are given. For each test case on the first line the number N of interesting stars is given (1 ≤ N ≤ 10000). The second line of the test case contains the list of ID numbers of the interesting stars, separated by spaces. Each ID is a positive integer which is no greater than 10000. The input data terminate with the end of file.

Output

For each test case the program should print one line with the number of subsets with the asked property.

Sample Input

4
2 3 4 5 
4
2 4 6 8 
7
2 3 4 5 7 6 8

Sample Output

1 
0 
34

Source

[Submit]   [Go Back]   [Status]   [Discuss]

技术分享Home Page   技术分享Go Back  技术分享To top


给出一个序列求gcd为1的四元组的个数,容斥搞一下。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#include <queue>
#define foreach(it,v) for(__typeof(v.begin()) it = v.begin(); it != v.end(); ++it)
using namespace std;
typedef long long ll;
const int maxn = 1e4 + 5;
bool check[maxn];
vector<int> v[maxn];
int g[maxn],a[maxn];//g[i] 表示i的倍数有多少个(只考虑素因数分解式中素数幂不超过1的i,其余为0)
void init(int n)
{
	memset(check, 0, sizeof check);
	for(int i = 2; i <= n; i++) {
		if(check[i])continue;
		for(int j = i; j <= n; j += i)
			v[j].push_back(i),check[j] = 1;
	}
}
void Modify(int x,int d)
{
	vector<int> &cur = v[x];
	int M,sz = cur.size();
	M = 1<<sz;
	for(int s = 0; s < M; s++) {
		int now = 1;
		for(int j = 0; j < sz; j++)if((s>>j)&1){
			now *= cur[j];
		}
		g[now] += d;
	}
}
ll gao(int x)
{
	ll t = g[x];
	if(t < 4)return 0;
	t = (t*(t-1)*(t-2)*(t-3))/24;
	if(v[x].size()&1) t = -t;
	return t;
}
int main(int argc, char const *argv[])
{
	init(maxn-5);
	int n;
	while(~scanf("%d",&n)) {
		memset(g,0,sizeof g);
		int M = 0;
		for(int i = 1; i <= n; i++) {
			scanf("%d",a + i);
			M = max(M,a[i]);
			Modify(a[i],1);
		}
		ll ans = 0;
		for(int i = 1; i <= M; i++) ans += gao(i);
		printf("%I64d\n", ans);
	}
	return 0;
}


POJ 3904(容斥原理)

标签:数学   容斥原理   

原文地址:http://blog.csdn.net/acvcla/article/details/46368523

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!