题目链接:http://poj.org/problem?id=3904

Sky Code
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2968   Accepted: 998

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

4
2 3 4 5
4
2 4 6 8
7
2 3 4 5 7 6 8

Sample Output

1
0
34

Source

题意:

给出n个数, 随便挑4个, 使得这四个数的最大公约数为1, 问有多少种组合?

题解:

思路:先用容斥原理计算出四个数的最大公约数>=1的组合数, 然后再用总数C(n,4)减之。

1.将每个数进行分解质因数, 然后再根据这些质因数组合出不同的因子,并记录这个因子出现的次数以及由多少个质因数构成。

2.容斥原理:比如因子2的个数为a,则四个数公约数为2的个数 为C(a,4),因子3的个数为b,则四个数公约数为3的个数为C(b,4),因子6(2*3)的个 数为c,则四个数公约数的个数为C(c,4)。 但是公约数为2的情况中或者公约数为3的情况中可能包括公约数为6的情况,相当于几个集合求并集,这就需要容斥定理来做。

3.如果这个因子出现的次数>=4, 则表明这个因子可以作为某四个数的最大公约数的因子。

4.根据容斥原理:当这个因子的由奇数个质因数构成时, 加; 当这个因子由偶数个质因子构成时, 减。

5. ans = C(n,4) - gcd(a,b,c,d)!=1的组合数。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <sstream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define ms(a, b) memset((a), (b), sizeof(a))
#define eps 0.0000001
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int maxn = 1e4+; LL pri[maxn], fac_num[maxn], fac_pri[maxn];
LL n, cnt; LL C(LL x)
{
return x*(x-)*(x-)*(x-)/;
} void Divide(LL x)
{
cnt = ;
for(int i = ; i*i<=x; i++)
{
if(x%i==)
{
pri[cnt++] = i;
while(x%i==) x /= i;
}
}
if(x!=) pri[cnt++] = x;
} void Unit()
{
for(LL s = ; s < (<<cnt); s++) //用二进制, 亦可用递归
{
LL tmp = , sum = ;
for(int j = ; j<cnt; j++)
if(s&(<<j))
{
tmp *= pri[j];
sum++;
} fac_num[tmp]++;
fac_pri[tmp] = sum;
}
} void init()
{
ms(fac_num, );
ms(fac_pri, ); LL x;
for(int i = ; i<=n; i++)
{
scanf("%lld",&x);
Divide(x); //分解质因数
Unit(); //质因数可以组成哪些因子(这些因子就是四个数的约数)
}
} void solve()
{
LL tmp = ;
for(int i = ; i<=1e4; i++) //容斥, 计算gcd(a,b,c,d)!=1的个数
{
if(fac_num[i]>=) //这个因子的个数必须不小于4, 才能成为4个数的约束
{
if(fac_pri[i]&) //素数个数为奇数时, 加
tmp += C(fac_num[i]);
else //素数个数为偶数时, 减
tmp -= C(fac_num[i]);
}
}
LL ans = C(n) - tmp; //总的减去gcd(a,b,c,d)!=1的个数,即为gcd(a,b,c,d)=1的个数。
printf("%lld\n", ans);
} int main()
{
while(scanf("%lld",&n)!=EOF)
{
init();
solve();
}
}

poj3904 Sky Code —— 唯一分解定理 + 容斥原理 + 组合的更多相关文章

  1. POJ3904 Sky Code

    题意 Language:Default Sky Code Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3980 Accepte ...

  2. [poj 3904] sky code 解题报告(组合计算+容斥原理)

    题目链接:http://poj.org/problem?id=3904 题目大意: 给出一个数列,询问从中取4个元素满足最大公约数为1的方案数 题解: 很显然,ans=总的方案数-最大公约数大于1的4 ...

  3. POJ3904 Sky Code【容斥原理】

    题目链接: http://poj.org/problem?id=3904 题目大意: 给你N个整数.从这N个数中选择4个数,使得这四个数的公约数为1.求满足条件的 四元组个数. 解题思路: 四个数的公 ...

  4. POJ 3904 Sky Code (容斥原理)

    B - Sky Code Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  5. [poj3904]Sky Code_状态压缩_容斥原理

    Sky Code poj-3904 题目大意:给你n个数,问能选出多少满足题意的组数. 注释:如果一个组数满足题意当且仅当这个组中有且只有4个数,且这4个数的最大公约数是1,$1\le n\le 10 ...

  6. Sky Code(poj3904)

    Sky Code Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2085   Accepted: 665 Descripti ...

  7. poj2773 —— 二分 + 容斥原理 + 唯一分解定理

    题目链接:http://poj.org/problem?id=2773 Happy 2006 Time Limit: 3000MS   Memory Limit: 65536K Total Submi ...

  8. [Codeforces 997C]Sky Full of Stars(排列组合+容斥原理)

    [Codeforces 997C]Sky Full of Stars(排列组合+容斥原理) 题面 用3种颜色对\(n×n\)的格子染色,问至少有一行或一列只有一种颜色的方案数.\((n≤10^6)\) ...

  9. Pairs Forming LCM (LCM+ 唯一分解定理)题解

    Pairs Forming LCM Find the result of the following code: ; i <= n; i++ )        for( int j = i; j ...

随机推荐

  1. deferred 对象

    转载,原文连接:http://www.ruanyifeng.com/blog/2011/08/a_detailed_explanation_of_jquery_deferred_object.html ...

  2. Codeforces 401D Roman and Numbers

    题目大意 Description 给定一个数 N(N<1018) , 求有多少个经过 N 重组的数是 M(M≤100) 的倍数. 注意: ①重组不能有前导零; ②重组的数相同, 则只能算一个数. ...

  3. nginx 自签名https

    繁杂的命令,以下准备写好的sh,拷贝https.sh文件,设置执行权限:chmod u+x https.sh #!/bin/sh # create self-signed server certifi ...

  4. DICOM医学图像处理:深入剖析Orthanc的SQLite,了解WADO & RESTful API

    背景: 上一篇博文简单翻译了Orthanc官网给出的CodeProject上“利用Orthanc Plugin SDK开发WADO插件”的博文,其中提到了Orthanc从0.8.0版本之后支持快速查询 ...

  5. Git学习0基础篇(下)

    server上的 Git - 协议 Git能够使用四种基本的协议传输资料:本地协议(Local).HTTP 协议.SSH(Secure Shell) 协议以及 Git 协议.眼下使用最普及的是 SSH ...

  6. iphone越狱-------平刷回越狱前(未越狱)状态

    众所周知,iPhone采用了沙盒机制,应用之间不能任意的访问,所以很多机友在拿到iPhone后,往往选择进行越狱,但是有时候,越狱的手机有的时候在安全.性能.流畅性等方面表现并不如意,所以只好寻求重新 ...

  7. NYOJ 722 数独 【DFS】+【预处理】

    数独 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描写叙述 数独是一种运用纸.笔进行演算的逻辑游戏.玩家须要依据9×9盘面上的已知数字,推理出全部剩余空格的数字,并满足每一 ...

  8. Sencha touch 初体验

    一.什么是Sencha Touch? Sencha Touch是一个应用手持移动设备的前端js框架,与extjs是同一个门派的,它继承了extjs的优点和缺点.功能很强大,效果很炫丽,效率不高. 二. ...

  9. MySQL 下优化SQL语句的一些经验

    http://java-guru.iteye.com/blog/143377

  10. libpcre.so.0 被删除怎么办?

    删除  #rpm -e --nodeps pcre-6.6-1.1 1> wget   http://mirror.centos.org/centos/6/os/i386/Packages/pc ...