POJ 3904(容斥原理)
Sky Code
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 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 Sample Output 1 Source |
[Submit] [Go Back] [Status]
[Discuss]
给出一个序列求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(容斥原理)的更多相关文章
- POJ 3904 Sky Code (容斥原理)
B - Sky Code Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- 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 ...
- [poj 3904] sky code 解题报告(组合计算+容斥原理)
题目链接:http://poj.org/problem?id=3904 题目大意: 给出一个数列,询问从中取4个元素满足最大公约数为1的方案数 题解: 很显然,ans=总的方案数-最大公约数大于1的4 ...
- 【POJ 3904】 Sky Code
[题目链接] http://poj.org/problem?id=3904 [算法] 问题可以转化为求总的四元组个数 - 公约数不为1的四元组个数 总的四元组个数为C(n,4),公约数不为1的四元组个 ...
- poj 2773(容斥原理)
容斥原理入门题吧. Happy 2006 Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 9798 Accepted: 3 ...
- POJ 3904 JZYZOJ 1202 Sky Code 莫比乌斯反演 组合数
http://poj.org/problem?id=3904 题意:给一些数,求在这些数中找出四个数互质的方案数. 莫比乌斯反演的式子有两种形式http://blog.csdn.net/out ...
- POJ 3904
第一道莫比乌斯反演的题. 建议参看http://www.isnowfy.com/mobius-inversion/ 摘其中部分 证明的话感觉写起来会比较诡异,大家意会吧说一下这个经典题目:令R(M,N ...
- Happy 2006 POJ - 2773 容斥原理+二分
题意: 找到第k个与m互质的数 题解: 容斥原理求区间(1到r)里面跟n互质的个数时间复杂度O(sqrt(n))- 二分复杂度也是O(log(n)) 容斥原理+二分这个r 代码: 1 #include ...
- Find a multiple POJ - 2356 容斥原理(鸠巢原理)
1 /* 2 这道题用到了鸠巢原理又名容斥原理,我的参考链接:https://blog.csdn.net/guoyangfan_/article/details/102559097 3 4 题意: 5 ...
随机推荐
- JS学习笔记 - 微博发布效果
<script> window.onload = function() { var oTxt = document.getElementById('txt1'); var oBtn = d ...
- iOS开发 非常全的三方库、插件、大牛博客等等
UI 下拉刷新 EGOTableViewPullRefresh- 最早的下拉刷新控件. SVPullToRefresh- 下拉刷新控件. MJRefresh- 仅需一行代码就可以为UITableVie ...
- App.js和App.css(用于移动应用的js和css)
App.js和App.css(用于移动应用的js和css) 一.App.js和App.css(用于移动应用的js和css) App.js 是一个轻量级的 JavaScript UI 库,用来创建移动的 ...
- Shiro的Web项目配置(转)
Shiro的Web项目配置 一 shiro的学习 二 shiro的java客户端配置 三 关于权限的一些问题 一 shiro的学习 官网和张开涛博客 二 shiro的java客户端配置 1.在web. ...
- 工欲善其事必先利其器--------搭建Android平台
工欲善其事必先利其器--------搭建Android平台 1.1 安装JDK 在Eclipse的开发过程中需要JDK或JRE的支持,否则会报错. (1) 下载JDK(建 ...
- swift开发网络篇—利用NSURLConnection GET请求和POST请求
一.GET请求和POST请求简单说明 @IBOutlet weakvar userName:UITextField! @IBOutletweakvar userPwd:UITextField! @IB ...
- php重建二叉树(函数缺省参数相关的都写在后面,比如array_slice函数中的$length属性,故第一个参数是操作的数组)
php重建二叉树(函数缺省参数相关的都写在后面,比如array_slice函数中的$length属性,故第一个参数是操作的数组) 一.总结 牛客网和洛谷一样,是真的好用 二.php重建二叉树 输入某二 ...
- [Vue] Preload Data using Promises with Vue.js and Nuxt.js
Nuxt.js allows you to return a Promise from your data function so that you can asynchronously resolv ...
- Linux系统编程_8_进程控制之fork_wait_waitpid函数
fork函数: #include <unistd.h> pid_t fork(void); fork用来创建一个子进程: 特点: fork调用后会返回两次,子进程返回0,父进 ...
- libevent源码分析-介绍、安装、使用
Libevent介绍 安装 样例 Libevent介绍 在include\event2\event.h中有关于Libevent的介绍,这里简单翻译介绍一下: Libevent是以事件为驱动的开发可扩展 ...