poj3904 Sky Code

Sky Code

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1694   Accepted: 523

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

题意:给定n个数a1,a2,a3,…,an,从中选出四个数,使得他们之间的最大公约数为1,问有多少种取法?

解题思路:对每一个数ai,我们对它进行质因数分解,然后利用2^k算法找出所有由质因数组成的数,并记录组成该数的质因数个数,最后运用容斥原理进行求解;

参考代码:

#include <iostream>
#include <string.h>
using namespace std;
#define MAX_N 10000+5
#define MAX_FACTOR 16
typedef long long ll;
int n,f[MAX_N],count[MAX_N],factor[MAX_FACTOR],factor_num[MAX_N];
void solve(int a){
	int k=0;
	for (int i=2;i*i<=a;i++){	//质因数分解
		if (a%i==0){
			factor[k++]=i;
			while (a%i==0)
				a/=i;
		}
	}
	if (a>1)
		factor[k++]=a;
	for (int i=1;i<(1<<k);i++){	//2^k算法
		int mul=1,bits=0;
		for (int j=0;j<k;j++){
			if (i&(1<<j)){
				bits++;
				mul*=factor[j];
			}
		}
		count[mul]++;
		factor_num[mul]=bits;
	}
}
ll cal(ll a){
	return a*(a-1)*(a-2)*(a-3)/24;
}
int main(){
	while (cin>>n){
		for (int i=0;i<n;i++)
			cin>>f[i];
		memset(count,0,sizeof(count));
		memset(factor_num,0,sizeof(factor_num));
		for (int i=0;i<n;i++)
			solve(f[i]);
		ll ans=cal(n);
		for (int i=2;i<=10000;i++){
			if (factor_num[i]&1==0){
				ans+=cal(count[i]);
			}
			else{
				ans-=cal(count[i]);
			}
		}
		cout<<ans<<endl;
	}

	return 0;
}
时间: 2024-10-19 18:23:07

poj3904 Sky Code的相关文章

POJ3904 Sky Code【容斥原理】

题目链接: http://poj.org/problem?id=3904 题目大意: 给你N个整数.从这N个数中选择4个数,使得这四个数的公约数为1.求满足条件的 四元组个数. 解题思路: 四个数的公约数为1.并不代表四个数两两互质.比方(2,3,4,5)公约数为1,可是 2和4并不互质. 从反面考虑.先求出四个数公约数不为1的情况个数,用总的方案个数 减去四个数公约数不为1的情况个数就是所求. 求四个数公约数不为1的情况个数,须要将N个数每一个数质因数分解,纪录下全部不同 的素因子所能组成的因

Sky Code(poj3904)

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

[poj3904]Sky Code_状态压缩_容斥原理

Sky Code poj-3904 题目大意:给你n个数,问能选出多少满足题意的组数. 注释:如果一个组数满足题意当且仅当这个组中有且只有4个数,且这4个数的最大公约数是1,$1\le n\le 10^4$. 想法:我们显然可以知道4个数是可以不用两两互质的,所以正面计算难度较大,我们考虑从反面考虑.我们通过计算所有gcd不为1的组数,用总组数相减即可.然后,我们发现一个不为0的gcd显然可以被组中的任意一个数整除,所以我们可以进行容斥.只需要枚举gcd的约数个即可.计算的过程我们用状态压缩实现

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 Sky Code 莫比乌斯反演

N. Sky Code Time Limit: 1000ms Case Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class name: Main Submit Status Font Size: + - Stancu likes space travels but he is a poor software developer and will never be able

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 3094 Sky Code

题目链接: http://poj.org/problem?id=3904 Sky Code Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1334   Accepted: 405 Description Stancu likes space travels but he is a poor software developer and will never be able to buy his own spacecraf

POJ3094 Sky Code(莫比乌斯反演)

POJ3094 Sky Code(莫比乌斯反演) Sky Code 题意 给你\(n\le 10^5\)个数,这些数\(\le 10^5\),问这些这些数组成的互不相同的无序四元组(a,b,c,d)使得gcd(a,b,c,d)=1的四元组有多少? 解法 枚举一个约数\(k\),看看总共有多少个数\(S_k=\{x\}\)满足\(k|x\).那么可以保证(a,b,c,d)有的一个共同的因子是k,这样的四元组的个数就是 \[ F(k)={|S_k|\choose 4} \] 这样算会算重,比如枚举到

POJ 3904 Sky Code (容斥原理)

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