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
Font Size: + -
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

题意:给10^4个数字,最大数字不超过10^4. 求4元组 gcd(x,y,z,k)=1 ;
思路:莫比乌斯。 统计F【i】的数字的个数。 F(d) = Cnm(F[d],4)种方案。
 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<cstring>
 4 #include<cstdlib>
 5 using namespace std;
 6 const int N = 1e4+5;
 7 
 8 int vis[N];
 9 int mu[N];
10 int prime[N],cnt;
11 int num[N];
12 int Hash[N];
13 
14 void init()
15 {
16     memset(vis,0,sizeof(vis));
17     mu[1] = 1;
18     cnt = 0;
19     for(int i=2;i<N;i++)
20     {
21         if(!vis[i])
22         {
23             prime[cnt++] = i;
24             mu[i] = -1;
25         }
26         for(int j = 0;j<cnt&&i*prime[j]<N;j++)
27         {
28             vis[i*prime[j]] = 1;
29             if(i%prime[j]) mu[i*prime[j]] = -mu[i];
30             else
31             {
32                 mu [i *prime[j]] = 0;
33                 break;
34             }
35         }
36     }
37 }
38 long long Cnm(int n,int m)
39 {
40     if(n<m)return 0;
41     long long nn=n;
42     long long mm=m;
43     long long i,j;
44     long long sum = 1;
45     for(i=1,j=nn;i<=mm;i++,j--)
46     sum = sum *j/i;
47     return sum;
48 }
49 int main()
50 {
51     int n,x;
52     init();
53     while(scanf("%d",&n)>0)
54     {
55         memset(num,0,sizeof(num));
56         memset(Hash,0,sizeof(Hash));
57         int maxn = 0;
58         for(int i=1;i<=n;i++){
59             scanf("%d",&x);
60             Hash[x]++;
61             if(x>maxn) maxn=x;
62         }
63         for(int i=1;i<=maxn;i++)
64         {
65             for(int j=i;j<=maxn;j=j+i)
66             num[i]=num[i]+Hash[j];
67         }
68         long long sum = 0;
69         for(int i=1;i<=maxn;i++){
70             long long tmp = Cnm(num[i],4);
71             sum = sum+tmp*mu[i];
72         }
73         printf("%I64d\n",sum);
74     }
75     return 0;
76 }

 

转载于:https://www.cnblogs.com/tom987690183/p/4032694.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值