poj3904 Sky Code------容斥复习

 

Sky Code
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 807 Accepted: 229

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

 

 

 

//这题很久以前做过了,好久没有接触过容斥了,拿来复习复习
//求得是n个数中,有多少组(a,b,c,d)的公约数为1,值得注意的是这四个数不一定两两互质。
//所以我们从它的反面考虑,先求出公约数不为1的个数。
//思路:把每个数素数分解,记录不重复素因子所能组成的因子,把这些因子的总数统计,并且统计每个因子是由多少个素因子组成
//如这n个数中含2的个数为a,含3的个数为b,含6的个数为c,那么公约数大于1的总数为p=c(a,4)+c(b,4)-c(c,4),总的个数为c(n,4)
//用c(n,4)-p即为所求
#include<iostream>
#include<cstdlib>
#include<stdio.h>
#include<memory.h>
using namespace std;
#define maxn 10005
long long count[maxn];
long long num[maxn];
long long p[maxn];
long long prime[maxn];
void init()
{
    memset(p,0,sizeof(p));
    memset(num,0,sizeof(num));
    for(long long i=4;i<maxn;i++)
    p[i]=i*(i-1)*(i-2)*(i-3)/24;
}
void solve(long long  n)
{
    long long  tol=0;
    for(long long  i=2;i*i<=n;i++)
    {
        if(n%i==0)
        {
            prime[tol++]=i;
        }
        while(n%i==0)
            n/=i;
    }
    if(n!=1)
    prime[tol++]=n;
    for(long long  i=1;i<(1<<tol);i++)
    {
        long long k=1;
        long long sum=0;
        for(long long  j=0;j<tol;j++)
        {
            if(i&(1<<j))
            {
                k*=prime[j];
                sum++;
            }
        }
        count[k]++;//记录当前因子的个数
        num[k]=sum;//记录当前因子是由多少个素因子组成
    }

}
int main()
{
   init();
   long long  n;
   long long  m;
   while(scanf("%lld",&n)!=EOF)
   {
       memset(count,0,sizeof(count));
       for(long long  i=0;i<n;i++)
       {
           scanf("%lld",&m);
           solve(m);
       }
       long long ans=0;
       for(long long  i=0;i<maxn;i++)
       {
           if(count[i])
           {
               if(num[i]%2)
               {
                   ans+=p[count[i]];
               }
               else
               ans-=p[count[i]];
           }
       }
       cout<<p[n]-ans<<endl;

   }

}


 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值