HDU4609 FFT+组合计数

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4609

题意:

找出n根木棍中取出三根木棍可以组成三角形的概率

题解:

我们统计每种长度的棍子的个数

我们对于长度就有一个多项式

\[f=num[0]*i_0+num[1]*i_1+num[2]*i_2.....num[len]*i_len
\]

我们考虑两根棍子可以组成所有长度的方案数

所以我们对num数组求一次FFT

两根棍子组成长度的上界是\(len_{max}*2\)

可能存在棍子重复组合的情况,这个时候我们需要去重

去掉两种重复的情况

1.自己和自己组合 即去除a[i]+a[i]的情况

2.A和B组合 B又和A组合的情况 这种时候每个组合/2即可

然后通过组合数计数即可,tips:在过程中可能爆int

代码:

#include <set>
#include <map>
#include <cmath>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
const int maxn = 3e5 + 5;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double Pi = acos(-1);
LL quick_pow(LL x, LL y) {
LL ans = 1;
while(y) {
if(y & 1) {
ans = ans * x % mod;
} x = x * x % mod;
y >>= 1;
} return ans;
}
struct complex {
double x, y;
complex(double xx = 0, double yy = 0) {
x = xx, y = yy;
}
} x[maxn]; int a[maxn];
complex operator + (complex a, complex b) {
return complex(a.x + b.x, a.y + b.y);
}
complex operator - (complex a, complex b) {
return complex(a.x - b.x, a.y - b.y);
}
complex operator * (complex a, complex b) {
return complex(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);
} int n, m;
int l, r[maxn];
int limit = 1;
void fft(complex *A, int type) {
for(int i = 0; i < limit; i++) {
if(i < r[i]) swap(A[i], A[r[i]]);
}
for(int mid = 1; mid < limit; mid <<= 1) {
complex Wn(cos(Pi / mid), type * sin(Pi / mid));
for(int R = mid << 1, j = 0; j < limit; j += R) {
complex w(1, 0);
for(int k = 0; k < mid; k++, w = w * Wn) {
complex x = A[j + k], y = w * A[j + mid + k];
A[j + k] = x + y;
A[j + k + mid] = x - y;
}
}
}
}
LL num[maxn];//100000*100000会超int
LL sum[maxn];
int main() {
#ifndef ONLINE_JUDGE
FIN
#endif
int T;
scanf("%d", &T);
while(T--) {
int n;
memset(num, 0, sizeof(num));
scanf("%d", &n);
for(int i = 0; i < n; i++) {
scanf("%d", &a[i]);
num[a[i]]++;
}
sort(a, a + n);
int len1 = a[n - 1] + 1;
limit = 1;
l = 0;
while(limit < 2 * len1) limit <<= 1, l++;
for(int i = 0; i < len1; i++) {
x[i] = complex(num[i], 0);
}
for(int i = len1; i < limit ; i++) {
x[i] = complex(0, 0);
}
for(int i = 0; i < limit; i++) {
r[i] = (r[i >> 1] >> 1) | ((i & 1) << (l - 1));
}
fft(x, 1);
for(int i = 0; i < limit; i++) {
x[i] = x[i] * x[i];
}
fft(x, -1);
for(int i = 0; i < limit; i++) {
x[i].x /= limit;
}
for(int i = 0; i < limit; i++) {
num[i] = (LL)(x[i].x + 0.5);
// debug1(num[i]);
}
limit = 2 * a[n - 1];
//去重,去除 a_i,a_i这种情况
for(int i = 0; i < n; i++) {
num[a[i] + a[i]]--;
}
//去重,去除 (a_i,a_j),(a_j,a_i)这种情况
for(int i = 1; i <= limit; i++) {
num[i] /= 2;
}
sum[0] = 0;
for(int i = 1; i <= limit; i++)
sum[i] = sum[i - 1] + num[i];
LL cnt = 0;
for(int i = 0; i < n; i++) {
cnt += sum[limit] - sum[a[i]];
//减掉一个取大,一个取小的
cnt -= (long long)(n - 1 - i) * i;
//减掉一个取本身,另外一个取其它
cnt -= (n - 1);
//减掉大于它的取两个的组合
cnt -= (long long)(n - 1 - i) * (n - i - 2) / 2;
}
//总数
long long tot = (long long)n * (n - 1) * (n - 2) / 6;
printf("%.7f\n", (double)cnt / tot); }
return 0;
}

HDU4609 FFT+组合计数的更多相关文章

  1. BZOJ 4555: [Tjoi2016&Heoi2016]求和 [分治FFT 组合计数 | 多项式求逆]

    4555: [Tjoi2016&Heoi2016]求和 题意:求\[ \sum_{i=0}^n \sum_{j=0}^i S(i,j)\cdot 2^j\cdot j! \\ S是第二类斯特林 ...

  2. BZOJ 4555: [Tjoi2016&Heoi2016]求和 [FFT 组合计数 容斥原理]

    4555: [Tjoi2016&Heoi2016]求和 题意:求\[ \sum_{i=0}^n \sum_{j=0}^i S(i,j)\cdot 2^j\cdot j! \\ S是第二类斯特林 ...

  3. BZOJ 2287. [HZOI 2015]疯狂的机器人 [FFT 组合计数]

    2287. [HZOI 2015]疯狂的机器人 题意:从原点出发,走n次,每次上下左右不动,只能在第一象限,最后回到原点方案数 这不煞笔提,组合数写出来发现卷积NTT,然后没考虑第一象限gg 其实就是 ...

  4. 2019暑期金华集训 Day1 组合计数

    自闭集训 Day1 组合计数 T1 \(n\le 10\):直接暴力枚举. \(n\le 32\):meet in the middle,如果左边选了\(x\),右边选了\(y\)(且\(x+y\le ...

  5. bzoj 2281 [Sdoi2011]黑白棋(博弈+组合计数)

    黑白棋(game) [问题描述] 小A和小B又想到了一个新的游戏. 这个游戏是在一个1*n的棋盘上进行的,棋盘上有k个棋子,一半是黑色,一半是白色. 最左边是白色棋子,最右边是黑色棋子,相邻的棋子颜色 ...

  6. 【BZOJ5491】[HNOI2019]多边形(模拟,组合计数)

    [HNOI2019]多边形(模拟,组合计数) 题面 洛谷 题解 突然特别想骂人,本来我考场现切了的,结果WA了几个点,刚刚拿代码一看有个地方忘记取模了. 首先发现终止态一定是所有点都向\(n\)连边( ...

  7. [总结]数论和组合计数类数学相关(定理&证明&板子)

    0 写在前面 0.0 前言 由于我太菜了,导致一些东西一学就忘,特开此文来记录下最让我头痛的数学相关问题. 一些引用的文字都注释了原文链接,若侵犯了您的权益,敬请告知:若文章中出现错误,也烦请告知. ...

  8. 【BZOJ5323】[JXOI2018]游戏(组合计数,线性筛)

    [BZOJ5323][JXOI2018]游戏(组合计数,线性筛) 题面 BZOJ 洛谷 题解 显然要考虑的位置只有那些在\([l,r]\)中不存在任意一个约数的数. 假设这样的数有\(x\)个,那么剩 ...

  9. 【BZOJ5305】[HAOI2018]苹果树(组合计数)

    [BZOJ5305][HAOI2018]苹果树(组合计数) 题面 BZOJ 洛谷 题解 考虑对于每条边计算贡献.每条边的贡献是\(size*(n-size)\). 对于某个点\(u\),如果它有一棵大 ...

随机推荐

  1. celery 动态定时任务探索

    环境: celery 4.3 flask python 3.7 linux 需求: 动态添加定时任务,且方便维护. 解决思路: 参考django-celery 或是celery源码,将定时任务配置放置 ...

  2. 如何处理iOS中照片的方向

    使用过iPhone或者iPad的朋友在拍照时不知是否遇到过这样的问题,将设备中的照片导出到Windows上时,经常发现导出的照片方向会有问题,要么横着,要么颠倒着,需要旋转才适合观看.而如果直接在这些 ...

  3. jQuery 链

    通过 jQuery,可以把动作/方法链接在一起. Chaining 允许我们在一条语句中运行多个 jQuery 方法(在相同的元素上). jQuery 方法链接 直到现在,我们都是一次写一条 jQue ...

  4. 2019-2-24-VisualStudio-过滤输出窗口文本

    title author date CreateTime categories VisualStudio 过滤输出窗口文本 lindexi 2019-2-24 11:10:7 +0800 2019-0 ...

  5. @codechef - RNG@ Random Number Generator

    目录 @description@ @solution@ @part - 1@ @part - 2@ @part - 3@ @accepted code@ @details@ @description@ ...

  6. Spring读取配置文件,地址问题,绝对路径,相对路径

    Spring在读取配置文件时,是相对于bin,或者WEB-INF的: “applicationContext.xml”就是找bin或WEB-INF及子文件夹下的文件: “/res/applicatio ...

  7. @noi.ac - 171@ 立方体

    目录 @description@ @solution@ @accepted code@ @details@ @description@ TonyFang 打算送你一些立方体. 你需要在 [1, n] ...

  8. Laravel引入第三方库的方法

    https://blog.csdn.net/will5451/article/details/52472695 1.首先在app目录下创建一个新的文件夹,命名libs(可自定义) 2.(可选)考虑到后 ...

  9. Win7如何显示文件后缀

    有些时候,我们需要修改文件的后缀名,但是Windows7系统默认不显示文件后缀.我们怎样显示和修改文件后缀呢?请接着往下看. 工具/原料   一个win7系统 方法/步骤   1 如图所示,此时是无法 ...

  10. 洛谷P5022 旅行 题解 去环/搜索

    题目链接:https://www.luogu.org/problem/P5022 这道题目一开始看的时候没有思路,但是看到数据范围里面有一个: \(m = n-1\) 或 \(m = n\) ,一下子 ...