POJ 3904 Sky Code (容斥+莫比乌斯反演)
2015-08-16 14:14
239 查看
Sky Code
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
Sample Output
Source
Southeastern European Regional Programming Contest 2008
题目链接:http://poj.org/problem?id=3904
题目大意:给n个不相同的数,从中任意选出4个,使得它们的最大公约数为1,问有多少种选法
题目分析:首先n小于4肯定是0了,C(n,4) = n * (n - 1) * (n - 2) * (n - 3) / 24,本题数据范围不大,long long即可,差不多是裸的莫比乌斯反演题了,和NOJ 2079几乎一样,ac代码在POJ上rank 7
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 1831 | Accepted: 570 |
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
Southeastern European Regional Programming Contest 2008
题目链接:http://poj.org/problem?id=3904
题目大意:给n个不相同的数,从中任意选出4个,使得它们的最大公约数为1,问有多少种选法
题目分析:首先n小于4肯定是0了,C(n,4) = n * (n - 1) * (n - 2) * (n - 3) / 24,本题数据范围不大,long long即可,差不多是裸的莫比乌斯反演题了,和NOJ 2079几乎一样,ac代码在POJ上rank 7
#include <cstdio> #include <cstring> #include <algorithm> #define ll long long using namespace std; int const MAX = 1e4 + 5; int mob[MAX], p[MAX], cnt[MAX], num[MAX]; bool prime[MAX]; int n, ma; void Mobius() { memset(prime, true, sizeof(prime)); int pnum = 0; mob[1] = 1; for(int i = 2; i < MAX; i++) { if(prime[i]) { p[pnum ++] = i; mob[i] = -1; } for(int j = 0; j < pnum && i * p[j] < MAX; j++) { prime[i * p[j]] = false; if(i % p[j] == 0) { mob[i * p[j]] = 0; break; } mob[i * p[j]] = -mob[i]; } } } ll cal() { for(int i = 1; i <= ma; i++) for(int j = i; j <= ma; j += i) num[i] += cnt[j]; ll ans = 0; for(int i = 1; i <= ma; i++) { int x = num[i]; if(x >= 4) ans += (ll) mob[i] * x * (x - 1) * (x - 2) * (x - 3) / 24; } return ans; } int main() { Mobius(); while(scanf("%d", &n) != EOF) { ma = 0; memset(cnt, 0, sizeof(cnt)); memset(num, 0, sizeof(num)); for(int i = 0; i < n; i++) { int tmp; scanf("%d", &tmp); cnt[tmp] ++; ma = max(ma, tmp); } if(n < 4) { printf("0\n"); continue; } printf("%lld\n", cal()); } }
相关文章推荐
- 图像处理算法回顾基本框架
- NUBT 1580 素数分解 母函数法解
- openstack中虚拟机怎么与物理机通信
- notify vs nofifyall
- SSH深度历险记(九) Struts2+DWZ+Uploadify多文件(文件和图片等。)上传
- oc对象方法
- svn网络服务器项目导入到Eclipse
- win 环境安装python 和 Py pip
- 8个DBA最常用的监控Oracle数据库的常用shell脚本
- 下拉刷新,在 ActionBar 上显示效果图
- Hadoop之MapReduce的两种任务模式
- oracle v$sqlarea 分析SQL语句使用资源情况 确认是否绑定变量
- LayoutInflater的原理分析【转载】
- POJ 3268 Silver Cow Party(dijkstra+矩阵转置)
- 1035. 插入与归并(25)
- 论函数的设计应如何才好,返回临时变量 | 返回引用 | 传递引用
- javascript函数
- yo angualr-fullstatck 项目打包部署
- git 两版本差异文件打包
- NGINX+TOMCAT负载均衡