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错误记录 - 取消事件冒泡、按钮、回车、ctrl回车提交留言
window.onload = function () { var oDiv = document.getElementById('div1'); var oBtn = document.getEle ...
- JSF教程(11)——生命周期之Invoke Application Phase
在这个阶段JSF实现将处理不论什么应用界别的事件,比如表单的提交或者链接点击后跳转到还有一个页面. 这时假设应用须要重定向不同 的web应用字眼或者产生一个资源其并不喊不论什么的JSF组件,那么就调用 ...
- js用button激活 Alert 元素关闭按钮的交互功能
js用button激活 Alert 元素关闭按钮的交互功能 一.总结 1.点(.)对应class,井号(#)对应id 2.jquery:amaze里面用的jquery,jquery熟悉之后,这些东西 ...
- python3中numpy函数tile的用法
tile函数位于python模块 numpy.lib.shape_base中,他的功能是重复某个数组.比如tile(A,n),功能是将数组A重复n次,构成一个新的数组,我们还是使用具体的例子来说明问题 ...
- ITFriend开发日志20140611
原文链接:http://www.itfriend.cn/user/ITFriend/article/details/100274 1.调整登录页. 把大背景图,改为通用的banner图,节省流量. 登 ...
- Unity插件之NGUI学习(5)—— 创建Label图文混排及文字点击
创建一个新的Scene,并按 Unity插件之NGUI学习(2)创建UI Root. 准备工作,制作Font.如今Project窗体创建一个Font目录.然后从系统自带字体目录中选择自己须要的字体,我 ...
- Classification and Representation
Classification To attempt classification, one method is to use linear regression and map all predict ...
- JSP自己定义标签
JSP自己定义标签 API文档: http://docs.oracle.com/javaee/7/api/ watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ ...
- Linux系统编程——线程私有数据
在多线程程序中.常常要用全局变量来实现多个函数间的数据共享.因为数据空间是共享的,因此全局变量也为全部线程共同拥有. 測试代码例如以下: #include <stdio.h> #inclu ...
- [转] Valgrind使用
http://www.cnblogs.com/napoleon_liu/articles/2001802.html 调不尽的内存泄漏,用不完的Valgrind Valgrind 安装 1. 到www. ...


