首页 > 代码库 > POJ Sky Code 莫比乌斯反演
POJ Sky Code 莫比乌斯反演
N. Sky Code
1000ms
1000ms
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
42 3 4 5 42 4 6 8 72 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 else31 {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 }
POJ Sky Code 莫比乌斯反演
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。