poj 3904 莫比乌斯函数灵活运用
2017-08-19 09:45
267 查看
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
Sample Output
题意:
给出n个数字,求其四个数字的子集的最大公约数是1的选法有多少种
题解:
利用莫比乌斯函数进行处理
最大公约数为1 的倍数
最大公约数为2的倍数
最大公约数为3的倍数
最大公约数为5的倍数这样处理下去
很明显就是莫比乌斯函数的运用了
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
题意:
给出n个数字,求其四个数字的子集的最大公约数是1的选法有多少种
题解:
利用莫比乌斯函数进行处理
最大公约数为1 的倍数
最大公约数为2的倍数
最大公约数为3的倍数
最大公约数为5的倍数这样处理下去
很明显就是莫比乌斯函数的运用了
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; #define LL long long #define MAXN 10000 int cnt[MAXN+10],num[MAXN+10]; bool check[MAXN+10]; int primer[MAXN+10]; int mu[MAXN+10],n; void Moblus() { memset(check,0,sizeof(check)); mu[1]=1; int tot=0; for(int i=2;i<=MAXN;i++){ if(!check[i]){ primer[tot++]=i; mu[i]=-1; } for(int j=0;j<tot&&i*primer[j]<=MAXN;j++){ check[i*primer[j]]=true; if(i%primer[j]==0){ mu[i*primer[j]]=0; break; } mu[i*primer[j]]-=mu[i]; } } } LL deal() { if(n<4) return 0; LL ans=0; for(int i=1;i<=MAXN;i++){ for(int j=1;j*i<=MAXN;j++) num[i]+=cnt[j*i]; } for(int i=1;i<=MAXN;i++){ if(num[i]>=4) ans+=(LL)mu[i]*num[i]*(num[i]-1)*(num[i]-2)*(num[i]-3)/24; } return ans; } int main() { Moblus(); //freopen("in.txt","r",stdin); while(scanf("%d",&n)!=EOF) { int t; memset(cnt,0,sizeof(cnt)); memset(num,0,sizeof(num)); for(int i=1;i<=n;i++){ scanf("%d",&t); cnt[t]++; } printf("%lld\n",deal()); } return 0; }
相关文章推荐
- poj 3662 Telephone Lines spfa算法灵活运用
- poj 2366 Sacrament of the sum 尺取法的灵活运用
- poj 2623 Sequence Median 堆的灵活运用
- POJ - 1338 Ugly Numbers-STL的灵活运用
- poj Cow Exhibition 01背包的灵活运用
- poj 2406 Power Strings 【kmp(len/(len-p[len])的灵活运用)】
- poj 3662 Telephone Lines spfa算法的灵活运用
- poj 3904 Sky Code(莫比乌斯函数)
- POJ - 3254 Corn Fields(灵活运用状态压缩)
- ROADS+dijkstra的灵活运用+POJ
- ROADS+dijkstra的灵活运用+POJ
- STL有限队列的灵活运用+IO优化+区间最大最小——POJ Sliding Window
- [MSSQL]灵活运用FOR XML PATH
- 下坡灵活运用刹车的技术要点
- javascript “||”、“&&”的灵活运用
- POJ 2638 筛选法的经典运用
- hdu1171 灵活的运用背包问题咯。。。 还有!!!! 合理的计算数组的范围!! wa了好多次!
- PAT (Advanced Level)1034. Head of a Gang (30) map的灵活运用
- 灵活运用PHP stdClass
- 灵活运用shell的for循环