题目链接: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. 洛谷——P2878 [USACO07JAN]保护花朵Protecting the Flowers

    P2878 [USACO07JAN]保护花朵Protecting the Flowers 题目描述 Farmer John went to cut some wood and left N (2 ≤ ...

  2. Codeforces Gym 100431A Achromatic Number 欧拉回路

    原题链接:http://codeforces.com/gym/100431/attachments/download/2421/20092010-winter-petrozavodsk-camp-an ...

  3. javaScript 时间转换,将后台返回的时间为一串数字转成正常格式

    js完整代码: function transferTime(cTime){ var jsonDate = new Date(parseInt(cTime)); Date.prototype.forma ...

  4. JVM类加载机制————2

    类加载机制的第一个阶段加载做的工作有: 1.通过一个类的全限定名(包名与类名)来获取定义此类的二进制字节流(Class文件).而获取的方式,可以通过jar包.war包.网络中获取.JSP文件生成等方式 ...

  5. python导入sklearn模块出现DLL load failed的解决办法

    笔者安装的python版本是2.7.6,最近在导入sklearn(版本:0.16.1)的模块时,经常出现DLL load failed的报错,具体截图如下: 解决办法与步骤如下: 由于sklearn的 ...

  6. android特效集合

    https://github.com/Trinea/android-open-project http://www.cnblogs.com/hawkon/p/3593709.html http://i ...

  7. background-attachment

      CreateTime--2017年9月28日10:58:58 Author:Marydon background-attachment 1.定义 定义背景图片随滚动轴的移动方式(设置背景图像是否固 ...

  8. iOS 私有库的使用

    最近项目说要用私有库 主要过程 创建两个库:  索引库   组件库 组件库  用git操作  比如更新代码 push   打tag等 索引库  存放组件的描述信息 也就是 .spec文件 这个文件和 ...

  9. linux查找文件夹下的全部文件里是否含有某个字符串

    查找文件夹下的全部文件里是否含有某个字符串  find .|xargs grep -ri "IBM"  查找文件夹下的全部文件里是否含有某个字符串,而且仅仅打印出文件名称  fin ...

  10. AMD单桥主板上电时序的详细解释

    3个待机条件: 1.桥需要得到待机电压:3.3V,1.5V/1.2V2.25M起振注:NV的RTC电路,一般不会导致时序故障,都可以出CPURST#3.PWRGD-SB(即INTEL芯片组的RSMRS ...