POJ 3904 Sky Code
题意:给定n个数ai, ai <= 10000, n <= 10000, 从中选出4个数要求gcd为1,这样的集合有多少个?
分析:首先总共集合nCr(n, 4) = n*(n-1)*(n-2)*(n-3)/24个,那么需要减掉gcd >=2 的集合。先减掉gcd为各个素数的方案数目,然后再由这些素数组成一些因子,考虑gcd为这些因子的情况。最后总结起来就是,素数个数为奇数时,减去;素数个数为偶数时,加上。具体实现的时候只要对每个ai分解质因数,然后单独考虑他的素因子能组成哪些数,这样再计算。
#include <cstdio>
#include <iostream>
#include <map>
#include <cstring>
#include <cstdlib>
#include <cmath>
#define pb push_back
#define mp make_pair
#define esp 1e-8
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define sz(x) ((int)((x).size()))
#define pb push_back
#define in freopen("solve_in.txt", "r", stdin);
#define bug(x) printf("Line : %u >>>>>>\n", (x));
#define inf 0x7f7f7f7f
using namespace std;
typedef long long LL;
typedef map<int, int> MPS;
typedef pair<int, int> PII; const int maxn = + ;
int a[maxn];
LL vis[][maxn], p[], pa[];
int cnt;
void dfs(int st, int pos, int lim) {
if(pos == lim) {
int tmp = ;
for(int i = ; i < pos; i++)
tmp *= pa[i];
vis[lim][tmp]++;
return;
}
for(int i = st; i < cnt; i++) {
pa[pos] = p[i];
dfs(i+, pos+, lim);
}
}
int main() { int n;
while(scanf("%d", &n) == ) {
memset(vis, , sizeof vis);
for(int i = ; i <= n; i++) {
scanf("%d", a+i);
int x = a[i];
cnt = ;
for(int j = ; j*j <= x; j++) {
if(x%j == ) {
p[cnt++] = j;
while(x%j == )
x /= j;
}
}
if(x != )
p[cnt++] = x;
for(int j = ; j <= cnt; j++)
dfs(, , j);
}
double ans = 1.0*n*(n-)*(n-)*(n-)/;
for(int i = ; i < ; i++)
for(int j = ; j <= ; j++)
if(vis[i][j] >= ) {
double tmp = 1.0;
for(LL k = vis[i][j]; k > vis[i][j]-; k--)
tmp = tmp*k;
tmp /= ;
ans += ((i&) ? - : )*tmp;
}
printf("%.0f\n", ans);
}
return ;
}
UPD:这题还可以用扩展欧拉函数做?链接:http://scturtle.is-programmer.com/posts/19402.html
其实是概率角度解释解释欧拉函数,然后拓展一下就可以解这个题了。1/p为包含素因子p的概率。1/p^n为n个数都包含素因子p的概率。
ans = m^n * ∏(1-1/(p^n))
POJ 3904 Sky Code的更多相关文章
- POJ 3904 Sky Code (容斥原理)
B - Sky Code Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- [poj 3904] sky code 解题报告(组合计算+容斥原理)
题目链接:http://poj.org/problem?id=3904 题目大意: 给出一个数列,询问从中取4个元素满足最大公约数为1的方案数 题解: 很显然,ans=总的方案数-最大公约数大于1的4 ...
- poj3904 Sky Code —— 唯一分解定理 + 容斥原理 + 组合
题目链接:http://poj.org/problem?id=3904 Sky Code Time Limit: 1000MS Memory Limit: 65536K Total Submiss ...
- POJ Sky Code 莫比乌斯反演
N. Sky Code Time Limit: 1000ms Case Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO for ...
- 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 ...
- POJ3904 Sky Code
题意 Language:Default Sky Code Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3980 Accepte ...
- POJ3094 Sky Code(莫比乌斯反演)
POJ3094 Sky Code(莫比乌斯反演) Sky Code 题意 给你\(n\le 10^5\)个数,这些数\(\le 10^5\),问这些这些数组成的互不相同的无序四元组(a,b,c,d)使 ...
- 【POJ 1850】 Code
[POJ 1850] Code 还是非常想说 数位dp真的非常方便! !. 数位dp真的非常方便!.! 数位dp真的非常方便! !! 重要的事说三遍 该题转换规则跟进制差点儿相同 到z时进一位 如az ...
- POJ 3904(容斥原理)
Sky Code Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1750 Accepted: 545 Descripti ...
随机推荐
- 你早该这么玩Excel 读书笔记
1. Excel用来分析数据,至少要有一份原始数据和对于的分类汇总数据,这两种数据在一项任务中,应该是存放在同一个Excel文档中的,在书籍中,把他们叫做源数据表和分类汇总表.用户输入源数据表,根据相 ...
- Poj 2840 Big Clock
1.Link: http://poj.org/problem?id=2840 2.Content: Big Clock Time Limit: 1000MS Memory Limit: 13107 ...
- 修改zepto源代码,使支持wp8的ie10
注意:当前1.1.3版本的zepto,已经有模块来支持wp8 原先的zepto,通过__proto__赋值,来使dom继承到$.fn方法, 无奈IE11之前的IE10,IE9不支持这种写法, 所以我们 ...
- 配置ADB 工具 (Win7_64)
ADB (Android Debut Bridge) ADB这个工具, 让我们可以用电脑来操纵手机 Android studio 安装好之后在SDK 中就有ADB 但是我们想使用它还需要配置它的环境变 ...
- android studio笔记之编译运行错误
错误类型: Error:java.lang.UnsupportedClassVersionError: com/android/dx/command/Main : Unsupported major错 ...
- php mysqli多个查询的例子
php中Mysqli多个查询的例子,感兴趣的朋友可以参考下. php中Mysqli多个查询的例子,感兴趣的朋友可以参考下. mysqli_multi_query(mysqli link,string ...
- Spark Streaming揭秘 Day29 深入理解Spark2.x中的Structured Streaming
Spark Streaming揭秘 Day29 深入理解Spark2.x中的Structured Streaming 在Spark2.x中,Spark Streaming获得了比较全面的升级,称为St ...
- Inside of Jemalloc
INSIDE OF JEMALLOCThe Algorithm and Implementation of Jemalloc author: vector03mail: mmzsmm@163.co ...
- 动态LINQ构建(实现等于不等于大于小于,like以及IN)
首先感谢园子里的“红烧狮子头”,他的工作是本文的基础,引文如下http://www.cnblogs.com/daviddai/archive/2013/03/09/2952087.html,本版本实现 ...
- Linux C程序的编译过程
Linux C程序的编译过程 学习一门语言程序,本人觉得还是得学习它的编译规则,现在,通过小例子小结下自己对C编译的认识. /*test.c 了解C程序的编译*/ #include <s ...