SPOJ_4191 Sky Code
来源:互联网 发布:python使用c数据结构 编辑:程序博客网 时间:2024/06/08 12:08
http://www.spoj.pl/problems/MSKYCODE/
题意:
有N个数,让你从中选出4个,使得它们的最大公约数为1 。N<=10000
思路:
用容斥原理来统计数。我们考虑问题的反面,就是先求出所有可能4个数的取法,然后减去4个数的
最大公约数不为1的组合。
#include<stdio.h>#include<string.h>typedef long long LL ;const int MAXN = 10010 ;LL N ;LL d[MAXN] , num[MAXN] , cnt[MAXN];LL cal( LL n ){ LL res = n * (n - 1LL) * (n - 2LL) * (n - 3LL) / (24LL) ; return res ;}int main(){ LL a; while( scanf("%lld",&N) == 1){ for(int i=1;i<MAXN;i++){ d[i] = 0 ; //有多少个数能被i整除 num[i] = 0 ; //值为i的数的个数 cnt[i] = 0 ; //约数为i的数被计算了几次 } for(int i=1;i<=N;i++){ scanf("%lld",&a); num[a] ++ ; } if( N < 4 ){ printf("0\n"); continue ; } for(LL i=2;i<MAXN;i++){ for(LL j=i;j<MAXN;j+=i){ d[i] += num[j] ; } } LL tot = 0; for(LL i=2;i<MAXN;i++){ LL a = 1 - cnt[i] ; tot += a * cal( d[i] ) ; for(int j=2*i;j<MAXN;j+=i){ cnt[ j ] += a ; } } printf("%lld\n", cal(N) - tot ) ; } return 0 ;}
- SPOJ_4191 Sky Code
- POJ 3904 Sky Code
- poj3904 Sky Code
- POJ 3904 Sky Code
- poj3904 Sky Code------容斥复习
- POJ 3904 Sky Code 容斥原理
- poj 3904 sky code 容斥原理
- poj3904 Sky Code 容斥原理
- [容斥原理] poj 3094 Sky Code
- POJ 3904 Sky Code 容斥原理
- spoj 4191. Sky Code(容斥)
- poj 3904 Sky Code【容斥原理】
- 容斥原理:poj 3904 Sky Code
- Sky Code - POJ 3904 容斥原理
- Sky Code - POJ 3904 容斥原理
- poj3904 Sky Code(容斥原理)
- POJ3904 Sky Code【容斥原理】
- POJ 3904 Sky Code(容斥)
- 虚拟键盘焦点问题
- STL 查找算法
- offsetof
- 通过进程ID获得主窗口句柄
- Java中相对路径,绝对路径问题总结
- SPOJ_4191 Sky Code
- Ogre中的向量Vector3的成员方法
- ready method for jQuery
- 利用poi 生成多个excel sheet
- hdu1195
- makefile .PHONY 伪目标
- google checkout android/java 编程
- linux sudo 命令详解
- 析构函数的使用与调用