Sky Code
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1750   Accepted: 545

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

[Submit]   [Go Back]   [Status]  
[Discuss]

Home Page   Go
Back
  To top

给出一个序列求gcd为1的四元组的个数,容斥搞一下。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#include <queue>
#define foreach(it,v) for(__typeof(v.begin()) it = v.begin(); it != v.end(); ++it)
using namespace std;
typedef long long ll;
const int maxn = 1e4 + 5;
bool check[maxn];
vector<int> v[maxn];
int g[maxn],a[maxn];//g[i] 表示i的倍数有多少个(仅仅考虑素因数分解式中素数幂不超过1的i,其余为0)
void init(int n)
{
memset(check, 0, sizeof check);
for(int i = 2; i <= n; i++) {
if(check[i])continue;
for(int j = i; j <= n; j += i)
v[j].push_back(i),check[j] = 1;
}
}
void Modify(int x,int d)
{
vector<int> &cur = v[x];
int M,sz = cur.size();
M = 1<<sz;
for(int s = 0; s < M; s++) {
int now = 1;
for(int j = 0; j < sz; j++)if((s>>j)&1){
now *= cur[j];
}
g[now] += d;
}
}
ll gao(int x)
{
ll t = g[x];
if(t < 4)return 0;
t = (t*(t-1)*(t-2)*(t-3))/24;
if(v[x].size()&1) t = -t;
return t;
}
int main(int argc, char const *argv[])
{
init(maxn-5);
int n;
while(~scanf("%d",&n)) {
memset(g,0,sizeof g);
int M = 0;
for(int i = 1; i <= n; i++) {
scanf("%d",a + i);
M = max(M,a[i]);
Modify(a[i],1);
}
ll ans = 0;
for(int i = 1; i <= M; i++) ans += gao(i);
printf("%I64d\n", ans);
}
return 0;
}

版权声明:本文博主原创文章。博客,未经同意不得转载。

POJ 3904(容斥原理)的更多相关文章

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

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

  2. poj 3904(莫比乌斯反演)

    POJ 3904 题意: 从n个数中选择4个数使他们的GCD = 1,求总共有多少种方法 Sample Input 4 2 3 4 5 4 2 4 6 8 7 2 3 4 5 7 6 8 Sample ...

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

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

  4. 【POJ 3904】 Sky Code

    [题目链接] http://poj.org/problem?id=3904 [算法] 问题可以转化为求总的四元组个数 - 公约数不为1的四元组个数 总的四元组个数为C(n,4),公约数不为1的四元组个 ...

  5. poj 2773(容斥原理)

    容斥原理入门题吧. Happy 2006 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 9798   Accepted: 3 ...

  6. POJ 3904 JZYZOJ 1202 Sky Code 莫比乌斯反演 组合数

    http://poj.org/problem?id=3904   题意:给一些数,求在这些数中找出四个数互质的方案数.   莫比乌斯反演的式子有两种形式http://blog.csdn.net/out ...

  7. POJ 3904

    第一道莫比乌斯反演的题. 建议参看http://www.isnowfy.com/mobius-inversion/ 摘其中部分 证明的话感觉写起来会比较诡异,大家意会吧说一下这个经典题目:令R(M,N ...

  8. Happy 2006 POJ - 2773 容斥原理+二分

    题意: 找到第k个与m互质的数 题解: 容斥原理求区间(1到r)里面跟n互质的个数时间复杂度O(sqrt(n))- 二分复杂度也是O(log(n)) 容斥原理+二分这个r 代码: 1 #include ...

  9. Find a multiple POJ - 2356 容斥原理(鸠巢原理)

    1 /* 2 这道题用到了鸠巢原理又名容斥原理,我的参考链接:https://blog.csdn.net/guoyangfan_/article/details/102559097 3 4 题意: 5 ...

随机推荐

  1. POJ 3187 Backward Digit Sums 枚举水~

    POJ 3187  Backward Digit Sums http://poj.org/problem?id=3187 题目大意: 给你一个原始的数字序列: 3   1   2   4  他可以相邻 ...

  2. Surging 微服务框架使用入门

    原文:Surging 微服务框架使用入门 前言 本文非 Surging 官方教程,只是自己学习的总结.如有哪里不对,还望指正.  我对 surging 的看法 我目前所在的公司采用架构就是类似与Sur ...

  3. 全面解析Activity的生命周期

    欢迎Follow我的GitHub, 关注我的CSDN. 在Android应用中, Activity是最重要的组件, 其生命周期(Lifecycle)被大家所熟知. 可是, 大家须要注意一些细节, 才干 ...

  4. (6)uboot具体解释——关闭缓存和mmu

    uboot具体解释--关闭缓存和mmu 当设置完时钟分频以后,uboot就会运行cpu_init_crit汇编函数,这个函数的主要作用就是关闭缓存和mmu.然后调用lowlevel_init函数进行系 ...

  5. swift 数据存储

    1.plist 存储 1.利用沙盒根目录拼接“Documents”字符串 //存储 func saveArray() { // 1.获得沙盒根路径,不管是真机还是模拟机,用它是最合适不过了 let h ...

  6. RMAN备份到NFS,报错 ORA-27054

    使用RMAN备份数据库到NFS挂载到的本地目录/backup 失败,失败提示如下: RMAN-03009: failure of backup command on ORA_DISK_1 channe ...

  7. python课程:python3函数

    摘自廖雪峰的网站:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014316 ...

  8. ORACLE中的Net Configuration Assistant 点击后无反应, sqlplus登录数据库提示Oracle11g ORA-12560: TNS: 协议适配器错误

    首先是对于点击无反应问题: 如果是客户端下的Net Configuration Assistant可用,而服务器端的Net Configuration Assistant等工具不可用的原因如下. 环境 ...

  9. [Node] Catch error for async await

    When we try to do MongoDB opration, mongoose return Promise, we can use async/await to simply the co ...

  10. Android多线程研究(9)——读写锁

    一.什么是锁 在Java的util.concurrent.locks包下有关于锁的接口和类如下: 先看一段代码: package com.codeing.snail.test; public clas ...