[容斥原理] 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 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

Southeastern European Regional Programming Contest 2008

[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

时间: 2024-10-05 07:06:44

[容斥原理] poj 3094 Sky Code的相关文章

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 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 3904 Sky Code (容斥原理)

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

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

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

【POJ 1850】 Code

[POJ 1850] Code 还是非常想说 数位dp真的非常方便! !. 数位dp真的非常方便!.! 数位dp真的非常方便! !! 重要的事说三遍 该题转换规则跟进制差点儿相同 到z时进一位 如az下位为bc 上位必须比下位小 依据这个规则搜出全部情况就可以 #include <iostream> #include <cstdio> #include <cstring> using namespace std; int dp[11][27]; int digit[11

[二分+容斥原理] poj 2773 Happy 2006

题目链接: http://poj.org/problem?id=2773 Happy 2006 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 9131   Accepted: 3073 Description Two positive integers are said to be relatively prime to each other if the Great Common Divisor (GCD) is 1.

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} \] 这样算会算重,比如枚举到