题意:给定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的更多相关文章

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

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

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

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

  3. poj3904 Sky Code —— 唯一分解定理 + 容斥原理 + 组合

    题目链接:http://poj.org/problem?id=3904 Sky Code Time Limit: 1000MS   Memory Limit: 65536K Total Submiss ...

  4. POJ Sky Code 莫比乌斯反演

    N. Sky Code Time Limit: 1000ms Case Time Limit: 1000ms Memory Limit: 65536KB   64-bit integer IO for ...

  5. 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 ...

  6. POJ3904 Sky Code

    题意 Language:Default Sky Code Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3980 Accepte ...

  7. POJ3094 Sky Code(莫比乌斯反演)

    POJ3094 Sky Code(莫比乌斯反演) Sky Code 题意 给你\(n\le 10^5\)个数,这些数\(\le 10^5\),问这些这些数组成的互不相同的无序四元组(a,b,c,d)使 ...

  8. 【POJ 1850】 Code

    [POJ 1850] Code 还是非常想说 数位dp真的非常方便! !. 数位dp真的非常方便!.! 数位dp真的非常方便! !! 重要的事说三遍 该题转换规则跟进制差点儿相同 到z时进一位 如az ...

  9. POJ 3904(容斥原理)

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

随机推荐

  1. PHP学习笔记 - 入门篇(2)

    PHP入门篇(2) 什么是变量 变量是用于存储值的容器,如下 $var = @"6666" 如何定义变量 定义变量就是像服务器的内存申请空间,用来存储数据,eg: <?php ...

  2. DOM_节点层次_Document类型

    一.Document类型 nodeType: 9; nodeName: "#document"; nodeValue: null; parentValue: null; owner ...

  3. JAVA:避免重复的创建对象

    在实际开发中,重复使用同一个对象要比每次需要的时候就创建一个对象要好的多: 作为一个比较极端的反面例子,看下面这个语句: String s = new String("haha") ...

  4. 点线图中的A*算法

    A*简介 A*(A-Star)算法是一种启发式算法,是静态路网中求解最短路最有效的方法.公式表示为:f(n)=g(n)+h(n), 其中f(n) 是节点n从初始点到目标点的估价函数, g(n) 是在状 ...

  5. MySQL页面打捞工具使用方法

    MySQL数据打捞工具 0.1 windows版 下载 一,选择数据源与输出目录 数据源可以是分区或物理物理磁盘,如\\.\D: 或\\.\PhysicalDrive0; 二,参数设置 请设置扫描参数 ...

  6. 如何找到Linux下常用命令的源码

    Linux系统,常用命令的来源很多,有些命令是shell自带的,比如cd,通过执行help命令,可以查看当前系统所有的内置命令. 用type <cmd_name>来查看一个命令是否为内置命 ...

  7. DTCMS获取栏目子类

    <%set DataTable categoryList=get_category_child_list(channel,0)%> <%foreach(DataRow cdr in ...

  8. php文件夹与文件目录操作函数

    在php中一些常用的文件夹/文件目录操作函数总结. php文件夹操作函数 string basename ( string path [, string suffix] ) 给出一个包含有指向一个文件 ...

  9. php获取数组中重复数据的两种方法

    分享下php获取数组中重复数据的两种方法. 1,利用php提供的函数,array_unique和array_diff_assoc来实现 <?php function FetchRepeatMem ...

  10. 用端口映射在Apache中对每个项目分配端口,实现一个端口访问一个网站

    映口映射的功能主要就是实际互联网可以访问当前进行映射的电脑,首先我们要进行路由器的端口映射,常见的开放端口有80,21,3389等,80是网站常用端口,21是FTP服务器常用端口,3389是远程桌面连 ...