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;
    }


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

  • 相关阅读:
    两个页面相同js方法兼容
    两个页面js方法兼容
    jQuery Chosen 使用
    我写的一个抓取基金净值的工具
    自己做的一个固定大小对象内存池,效率大概为原始的new/delete的2倍
    用libevent实现的echo服务器及telnet客户端
    共享一个防止脚本重复启动的shell脚本
    神煞排盘软件
    一个在字符串中查找多个关键字的函数strstrs(三种不同算法实现及效率分析)
    写了一个时间处理的类,能将人类时间转换成距离公元零年一月一日秒数(时间戳),同时支持时间戳转换成日期时间
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4868872.html
Copyright © 2011-2022 走看看