zoukankan      html  css  js  c++  java
  • 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

    [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
    ", ans);
    	}
    	return 0;
    }


    版权声明:本文博主原创文章。博客,未经同意不得转载。

  • 相关阅读:
    Linux Shell编程入门
    vim 文件在linux不换行,只显示^M解决办法
    服务器高性能程序 磁盘I/O篇
    车牌识别_转自别人的博客
    ubuntu网络简单设置
    C++设计模式(转载)
    结构算法之道
    C++设计模式工厂方法
    二叉树的深度优先遍历、广度优先遍历和非递归遍历
    iptables
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4868872.html
Copyright © 2011-2022 走看看