POJ 3904(容斥原理)

来源:互联网 发布:linux tcp ip 转发 编辑:程序博客网 时间:2024/05/20 22:26

Sky Code
Time Limit: 1000MS Memory Limit: 65536KTotal 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

42 3 4 5 42 4 6 8 72 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;}


0 0
原创粉丝点击