首页 > 其他 > 详细

[容斥原理] poj 3094 Sky Code

时间:2014-05-09 01:40:53      阅读:315      评论:0      收藏:0      [点我收藏+]

题目链接:

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 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]


题目意思:

给n个元素,求选出含四个元素且这四个数的最大公约数是1的集合的个数。

解题思路:

容斥原理

逆向考虑,如果这四个元素的最大公约数不为1,则一定有一个数a能整除这四个数。

如果求出不互质的集合个数,用总数减,就可以得到答案了。

所以现在问题就转化为对于最大公约数a,有多少个数能被a整除。这个可以预处理,对每个数,先分解质因数,枚举质因数组合,可以出统计myp[i]:能被数i整除的数的个数。

把n个数包含的质因数也统计出来,枚举质因数的组合,容斥原理求解。

也可以直接枚举1~Max(ai),这样比较直接。

代码:

#include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#include<cmath>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define LL long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#define M 1000000007
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;

#define N 10000

bool isp[N+100];
int pri[N+100],save[N+100],n,cnt,Max,lim;
int pp[N+100],cnt0;
ll ans;
int num[N+100],myp[N+100];
set<int>mys;


void init()
{
    cnt=0;

    for(int i=1;i<=N;i++)
        isp[i]=true;
    for(int i=2;i<=N;i++)
    {
        if(isp[i])
        {
            pri[++cnt]=i;
            for(int j=i*2;j<=N;j+=i)
                isp[j]=false;
        }
    }
    //printf("%d\n",cnt);
}
void dfs0(int hav,int cur,int cn)
{
    if(cur>cnt0)
        return ;
    for(int i=cur;i<=cnt0;i++)
    {
        myp[hav*pp[i]]++;
        num[hav*pp[i]]=cn;
        dfs0(hav*pp[i],i+1,cn+1);
    }
}


void dis(int cur)
{
    cnt0=0;

    for(int i=1;pri[i]*pri[i]<=cur;i++)
    {
        if(!(cur%pri[i]))
        {
            pp[++cnt0]=pri[i];
            mys.insert(pri[i]);
            while(!(cur%pri[i]))
                cur/=pri[i];
        }
    }
    if(cur!=1)
    {
         pp[++cnt0]=cur;
         mys.insert(cur);
    }

    for(int i=1;i<=cnt0;i++) //dfs出所有质因数组合
    {
        myp[pp[i]]++;
        num[pp[i]]=1;
        //printf("pp[i]:%d myp:%d\n",pp[i],myp[pp[i]]);
        //system("pause");
        dfs0(pp[i],i+1,2);
    }

}
ll cal(ll cur)
{
    if(cur<4)
        return 0;
    return cur*(cur-1)/2*(cur-2)*(cur-3)/12;
}
void dfs(ll hav,int cur,int num)
{
    if(cur>lim||hav>N)
        return ;
    for(int i=cur;i<=lim;i++)
    {
        ll temp=hav*pp[i];

        if(temp>N)
            continue;
        int tt=myp[temp];
        if(myp[pp[i]]==0) //后面肯定为0
                continue;

        if(num&1)
            ans-=cal(tt);
        else
            ans+=cal(tt);

        dfs(temp,i+1,num+1);
    }
}
int main()
{
   //freopen("in.txt","r",stdin);
   //freopen("out.txt","w",stdout);
   init();
   while(~scanf("%d",&n))
   {
       //memset(myp,0,sizeof(myp));
       memset(num,0,sizeof(num));
       mys.clear();
       myp.clear();

       Max=-1;
       for(int i=1;i<=n;i++)
       {
           scanf("%d",&save[i]);
           dis(save[i]);
           Max=max(Max,save[i]);
       }
       ans=cal(n);
       /*for(int i=1;i<=Max;i++)  //这样处理更快,更简单
            if(num[i]&1)
                ans-=cal(myp[i]);
            else
                ans+=cal(myp[i]);*/


       lim=mys.size();
       set<int>:: iterator it=mys.begin();
       int ii=0;

       for(;it!=mys.end();it++)
           pp[++ii]=*it;

       ans=cal(n);
       for(int i=1;i<=lim;i++)
       {
           ll temp=pp[i];
           if(myp[temp]==0)
                continue;
           ans-=cal(myp[temp]);
           dfs(pp[i],i+1,2);
       }
       printf("%I64d\n",ans);

   }
   return 0;
}


[容斥原理] poj 3094 Sky Code,布布扣,bubuko.com

[容斥原理] poj 3094 Sky Code

原文:http://blog.csdn.net/cc_again/article/details/25323655

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!