题目链接: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. 洛谷—— P1875 佳佳的魔法药水

    https://www.luogu.org/problemnew/show/1875 题目背景 发完了 k 张照片,佳佳却得到了一个坏消息:他的 MM 得病了!佳佳和大家一样焦急 万分!治好 MM 的 ...

  2. Ubuntu下添加开机启动项的2种方法

    1.方法一,编辑rc.loacl脚本 Ubuntu开机之后会执行/etc/rc.local文件中的脚本,所以我们可以直接在/etc/rc.local中添加启动脚本.当然要添加到语句:exit 0 前面 ...

  3. 全面解读java虚拟机(面试考点大全)d

    学习java以来,jvm的原理已经看过好多遍了,可是很多知识点都串不起来. 今天我把jvm相关知识整理了一下,看完之后肯定会对JVM很的清楚. JVM是虚拟机,也是一种规范,他遵循着冯·诺依曼体系结构 ...

  4. Direct2D教程(一)Direct2D已经来了,谁是GDI的终结者?

    什么是Direct2D 一言以蔽之,就是Windows 7平台上的一个2D图形API,可以提供高性能,高质量的2D渲染.大多数人对Direct2D可能都比较陌生,以至于我之前在论坛上提到这个词的时候, ...

  5. Kermit,Xmodem,1K-Xmodem,Ymodem,Zmodem传输协议小结

    来自:http://blog.163.com/czblaze_3333/blog/static/208996228201272295236713/ Kermit协议 报文格式: 1.       MA ...

  6. 怎样用命令行管理SharePoint Feature?

    普通情况下对IT管理者来说.在SharePoint Farm中维护Feature,更喜欢使用命令行实现,这样能够省去登录到详细网站的操作. 比方IT接到end user的一个需求,要开启Site Co ...

  7. css3 position fixed居中的问题

    通常,我们要让某元素居中,会这样做: #element{ margin:0 auto; } 假设还想让此元素位置固定呢?一般我们会加入position:fixed,例如以下: #element{ po ...

  8. caffe搭建--caffe在invidia+cpu 酷睿2Q9300 + ubuntu16.04.2上面的安装和编译过程

    本文原创,转载请注明出处. ------------------------------------------------分割线-------------------------------- 概要 ...

  9. C#高级编程七十五天----C#使用指针

    在C#中使用指针的语法 假设想在C#中使用指针,首先对项目进行过配置: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/font ...

  10. 在html中显示Flash的代码

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://down ...