POJ 3904(容斥原理)


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

Southeastern European Regional Programming Contest 2008

[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;
}
时间: 2025-01-31 07:53:36

POJ 3904(容斥原理)的相关文章

POJ 3904 Sky Code (容斥原理)

题意:给定n个数,从n个数找出四个数,使这四个数的最大公约数为1,找出有多少对这样的组合. 找最大公约数不是1的有多少对. 题解:四个数的公约数为1,并不代表四个数两两互质.比如(2,3,4,5)公约数为1,但是 2和4并不互质.从反面考虑,先求出四个数公约数不为1的情况个数,用总的方案个数 减去四个数公约数不为1的情况个数就是所求. 求四个数公约数不为1的情况个数,需要将N个数每个数质因数分解,纪录下所有不同 的素因子所能组成的因子(就是4个数的公约数),并统计构成每种因子的素因子个数, 和因

poj 2773(容斥原理)

容斥原理入门题吧. Happy 2006 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 9798   Accepted: 3341 Description Two positive integers are said to be relatively prime to each other if the Great Common Divisor (GCD) is 1. For instance, 1, 3, 5, 7,

POJ 3904 Sky Code (容斥+莫比乌斯反演)

Sky Code Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1831   Accepted: 570 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 ste

poj 3904 Sky Code

点击打开链接 Sky Code Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1562   Accepted: 478 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

POJ 1091 容斥原理

链接: http://poj.org/problem?id=1091 题意: 给你两个正整数n,m,让你求长度为n+1的满足条件的一个等式:a[1]*x1+a[2]*x2+a[3]*x3+...+a[n]*xn+a[n+1]*x(n+1)=1 (0<=a[i]<=m&&a[n+1]=m) 让你求一共有多少种情况满足这个条件. 要使得 a[1]*x1+a[2]*x2+a[3]*x3+...+a[n]*xn+a[n+1]*m=1 (0<=a[i]<=m),那么a[1],

Find a multiple POJ - 2356 容斥原理(鸠巢原理)

1 /* 2 这道题用到了鸠巢原理又名容斥原理,我的参考链接:https://blog.csdn.net/guoyangfan_/article/details/102559097 3 4 题意: 5 这道题给你了n个数,让你找这n个数中有没有几个数的和是n的倍数 6 7 题解: 8 你循环遍历一遍这n个数,如果某个数是n的倍数,那就输出一个1再输出这个数 9 如果没有的话,那就对这n个数求一下求前缀和. 10 1.在循环遍历一遍这个前缀和,如果某个数是n的倍数,那就输出i,再循环打印出1到i的

B - 抽屉 POJ - 2356 (容斥原理)

The input contains N natural (i.e. positive integer) numbers ( N <= 10000 ). Each of that numbers is not greater than 15000. This numbers are not necessarily different (so it may happen that two or more of them will be equal). Your task is to choose

C - 抽屉 POJ - 3370 (容斥原理)

Every year there is the same problem at Halloween: Each neighbour is only willing to give a certain total number of sweets on that day, no matter how many children call on him, so it may happen that a child will get nothing if it is too late. To avoi

POJ 3904 (莫比乌斯反演)

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 sophisti