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. Eclipse "Could not create java virtual machine"的问题解决

    今天到了新的环境,需要重新搭建Android的开发环境,下载eclipse并安装了JDK1.6后,启动eclipse,发现出现了错误“Could not create Javavirtual mach ...

  2. element ui源码解析 -- input篇

    el-input是element ui中使用最频繁的组件之一了,分析其构成从四个方面入手:DOM结构,属性,样式,事件入手 DOM结构: <div> <input /> < ...

  3. 手把手教你----MyEclipse中 配置 Tomcat

    电脑上配置Tomcatserver 安装Tomcat并配置环境变量 測试是否配置成功 MyEclipse中配置Tomcat 想要开发Java Web的程序.首先在MyEclipse中必须配置Tomca ...

  4. 一位90后程序员的自述:如何从年薪3w到30w!

    初入职场之时,大多数人都应该考虑过这样的一个问题,如何找到一种实用,简化web流程的方法,在工作之中能有所提升和突破. 学好哪些?基础必须精通! 九层之塔,起于垒土;千里之行,始于足下.入门之前,这些 ...

  5. java导出word直接下载

    导出word工具类 package util; import java.io.IOException; import java.io.Writer; import java.util.Map; imp ...

  6. iOS开发NSOperation 三:操作依赖和监听以及线程间通信

    一:操作依赖和监听 #import "ViewController.h" @interface ViewController () @end @implementation Vie ...

  7. solaris 11 stdio.h: No such file or directory

    http://www.zendo.name/solaris-11-stdio-h%EF%BC%9A-no-such-file-or-directory/ Posted on 2012 年 3 月 23 ...

  8. css3背景透明文字不透明

    在 FF/Chrome 等较新的浏览器中可以使用css属性background-color的rgba轻松实现背景透明,而文字保持不透明.而IE6/7/8浏览器不支持rgba,只有使用IE的专属滤镜fi ...

  9. USB 3.0规范中译本 第5章 机械结构

    本文为CoryXie原创译文,转载及有任何问题请联系cory.xie#gmail.com. 本章定义USB 3.0连接器和线缆组件的form, fit 和 function.包括以下方面: • 连接器 ...

  10. warning C4996: 'fopen': This function or variable may be unsafe.(_CRT_SECURE_NO_WARNINGS)

    在 windows 平台下的 visual studio IDE,使用 fopen 等 CRT 函数(C runtime library(part of the C standard library) ...