POJ Sky Code 莫比乌斯反演
2014-10-18 10:46
218 查看
N. Sky Code
Time Limit: 1000msCase 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 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)种方案。
#include<iostream> #include<stdio.h> #include<cstring> #include<cstdlib> using namespace std; const int N = 1e4+5; int vis ; int mu ; int prime ,cnt; int num ; int Hash ; void init() { memset(vis,0,sizeof(vis)); mu[1] = 1; cnt = 0; for(int i=2;i<N;i++) { if(!vis[i]) { prime[cnt++] = i; mu[i] = -1; } for(int j = 0;j<cnt&&i*prime[j]<N;j++) { vis[i*prime[j]] = 1; if(i%prime[j]) mu[i*prime[j]] = -mu[i]; else { mu [i *prime[j]] = 0; break; } } } } long long Cnm(int n,int m) { if(n<m)return 0; long long nn=n; long long mm=m; long long i,j; long long sum = 1; for(i=1,j=nn;i<=mm;i++,j--) sum = sum *j/i; return sum; } int main() { int n,x; init(); while(scanf("%d",&n)>0) { memset(num,0,sizeof(num)); memset(Hash,0,sizeof(Hash)); int maxn = 0; for(int i=1;i<=n;i++){ scanf("%d",&x); Hash[x]++; if(x>maxn) maxn=x; } for(int i=1;i<=maxn;i++) { for(int j=i;j<=maxn;j=j+i) num[i]=num[i]+Hash[j]; } long long sum = 0; for(int i=1;i<=maxn;i++){ long long tmp = Cnm(num[i],4); sum = sum+tmp*mu[i]; } printf("%I64d\n",sum); } return 0; }
相关文章推荐
- Hdoj 5212 Code 【数论】【莫比乌斯 反演】
- Sky Code - POJ 3904 容斥原理
- POJ 3904:Sky Code _容斥原理
- HDU 5212 Code【莫比乌斯反演】
- HDU 5212 Code【莫比乌斯反演】
- poj 3904 Sky Code 莫比乌斯反演 或 容斥原理
- 解题报告:POJ_3904 Sky Code 莫比乌斯反演|容斥
- poj 3904 莫比乌斯反演 或 容斥原理
- POJ 3904 Sky Code 莫比乌斯反演 容斥原理
- 【莫比乌斯反演】关于Mobius反演与gcd的一些关系与问题简化(bzoj 2301 Problem b&amp;&amp;bzoj 2820 YY的GCD&amp;&amp;BZOJ 3529 数表)
- [莫比乌斯反演] BZOJ 4816 [Sdoi2017]数字表格
- 数学挖坑待填......(FFT/母函数/莫比乌斯反演...)
- POJ 2033 alpha code 的译码数目
- BZOJ 2301 - 莫比乌斯反演 + 前缀和 + 分块计算
- BZOJ 2693: jzptab [莫比乌斯反演 线性筛]
- poj 2567 code thr tree
- 莫比乌斯反演
- bzoj 2005 能量采集 莫比乌斯反演
- 【bzoj1101】[POI2007]Zap 莫比乌斯反演
- POJ 题目1850 Code(组合数学)