题目链接: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. Arduino可穿戴教程ArduinoIDE新建编辑源文件

    Arduino可穿戴教程ArduinoIDE新建编辑源文件 Arduino IDE新建源文件 Arduino IDE启动后默认就新建了一个源文件,如图2.20所示.新建的源文件名称是以sketch_开 ...

  2. 2017 [六省联考] T2 相逢是问候

    4869: [Shoi2017]相逢是问候 Time Limit: 40 Sec  Memory Limit: 512 MBSubmit: 1205  Solved: 409[Submit][Stat ...

  3. C# 生成二维码(带Logo)

    C# 生成二维码(带Logo) 第一种方式 我们需要引用 ThoughtWorks.QRCode.dll  生成带logo二维码(framework4.0以上) 下载地址:https://pan.ba ...

  4. JavaScript this用法总结(**************************************)

    JavaScript this用法总结 在JavaScript中,this关键字可以说是最复杂的机制之一.对this的作用机制缺乏比较深入的理解很容易在实际开发中出现问题. 1.this的作用 为什么 ...

  5. Oracle SOA Suit Adapter

    SOA架构的一个核心的使命是整合企业现存的各式各样的计算资源,它不仅仅是代码层面的整合,更是硬件,计算能力,服务能力的整合.Oracle SOA Suite在这方面做得特别的贴切,它提供了一组Adap ...

  6. 邁向IT專家成功之路的三十則鐵律 鐵律二十八 IT人教學之道-速戰

    所謂IT人教學之道是指可善用在工作之中帶領新人快速上手,或是使用在生活中指導他人迅速學會某項技能的重要經驗.相信大家都有當過新手被指導的體驗,也有擔任過資深的老手帶領新人的經驗.然而您可能不知道,即便 ...

  7. 【spring data jpa】使用repository进行查询,使用userRepository.getOne(id)和userRepository.findById(id)无法从数据库查询到数据

    如题: 使用repository进行查询,使用CrudRepository自带的getOne()方法和findById()方法查询,数据库中有这条数据,但是并不能查到. userRepository. ...

  8. MySQL主从复制技术与读写分离技术amoeba应用

    MySQL主从复制技术与读写分离技术amoeba应用 前言:眼下在搭建一个人才站点,估计流量会非常大,须要用到分布式数据库技术,MySQL的主从复制+读写分离技术.读写分离技术有官方的MySQL-pr ...

  9. python入门之搭建环境

    进入以下网站:python.org 选择你喜欢(需要)的版本下载 点击下载即可,本次提供下载:python3.6.3 (国外架设,非常慢) ,用百度的平台吧:python3.6.1,多谢百度. 开始安 ...

  10. 百度地图 创建 自定义控件(vue)

    1.组件代码 Bmap.vue <!-- 离线地图 组件 --> <template> <div id="map" :style="styl ...