题目大意:

给出n(1e5)条线段(长度为整数,<=1e5),求任取3条线段能组成一个三角形的概率。

用cnt[i]记录长度为i的线段的个数,通过卷积可以计算出两条线段长度之和为i的方案数sum[i]:先用FFT计算出cnt[i]的卷积sum[i],为取两条线段长度和为i的排列数(包括自己和自己),去掉自己和自己的方案数,再对所有sum[i]除以2即为所求方案数。

之后对所有线段a[i]有大到小排列,考虑第i条线段是三角形最长边的情况(长度相同则将编号大的视为更长,就没有长度相同的情况了。实际计算时无影响)。首先是sum[i + 1] + sum[i + 2] + ... + sum[len - 1],即其他两边之和要大于他的方案数。然后去掉:1.另两边有一条比他大,有一条比他小。 2.两条都比他长。 3.有一条是他自己。这三种情况。

要注意有些地方爆int。还有len的计算要注意边界(一直wa,wa了半天)。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std; const double pi = acos(-); struct Complex{
double real, imag;
Complex (){}
Complex (double _real, double _imag){
real = _real;
imag = _imag;
}
}; Complex operator + (Complex x, Complex y){
return Complex(x.real + y.real, x.imag + y.imag);
} Complex operator - (Complex x, Complex y){
return Complex(x.real - y.real, x.imag - y.imag);
} Complex operator * (Complex x, Complex y){
return Complex(x.real * y.real - x.imag * y.imag, x.real * y.imag + x.imag * y.real);
} Complex operator / (Complex x, double y){
return Complex(x.real / y, x.imag / y);
} int reverse(int x, int len){
int t = ;
for (int i = ; i < len; i <<= ){
t <<= ;
if (x & i) t |= ;
}
return t;
} Complex A[];
void FFT(Complex *a, int n, int DFT){
for (int i = ; i < n; ++i) A[reverse(i, n)] = a[i];
for (int i = ; i <= n; i <<= ){
Complex wn = Complex(cos( * pi / i), DFT * sin( * pi / i));
for (int j = ; j < n; j += i){
Complex w = Complex(, );
for (int k = ; k < (i >> ); ++k){
Complex x = A[j + k];
Complex y = w * A[j + k + (i >> )];
A[j + k] = x + y;
A[j + k + (i >> )] = x - y;
w = w * wn;
}
}
}
if (DFT == -) for (int i = ; i < n; ++i) A[i] = A[i] / n;
for (int i = ; i < n; ++i) a[i] = A[i];
} int T, n;
int a[];
int cnt[];
Complex B[];
long long sum[]; int main(){ scanf("%d", &T);
while (T--){
scanf("%d", &n);
int maxL = ;
memset(cnt, , sizeof(cnt));
memset(sum, , sizeof(sum));
for (int i = ; i < n; ++i){
scanf("%d", a + i);
if (a[i] > maxL) maxL = a[i];
++cnt[a[i]];
}
int len = ;
while (len <= maxL * ) len <<= ;
for (int i = ; i <= maxL; ++i) B[i] = Complex(cnt[i], );
for (int i = maxL + ; i < len; ++i) B[i] = Complex(, );
FFT(B, len, );
for (int i = ; i < len; ++i) B[i] = B[i] * B[i];
FFT(B, len, -);
for (int i = ; i < len; ++i) sum[i] = (long long)(B[i].real + 0.5);
for (int i = ; i < n; ++i) --sum[a[i] + a[i]];
for (int i = ; i < len; ++i) sum[i] >>= ;
for (int i = len - ; i >= ; --i) sum[i] += sum[i + ];
sort(a, a + n);
long long ans = ;
for (int i = ; i < n; ++i){
long long tmp = sum[a[i] + ];
tmp -= ((long long)n - i - ) * i;
tmp -= ((long long)n - i - ) * (n - i - ) / 2LL;
tmp -= n - ;
ans += tmp;
}
double Ans = (double)ans * 6.0 / n / (n - ) / (n - );
printf("%.7f\n", Ans);
} return ;
}

HDU4609 计数问题+FFT的更多相关文章

  1. 3-idiots hdu4609 母函数+FFT 组合数学题

    http://acm.hdu.edu.cn/showproblem.php?pid=4609 题意:1e5个数,求取三个数能形成三角形的概率. 题解(这怎么会是fft入门题QAQ): 概率的算法就是三 ...

  2. HDU4609:3-idiots(FFT)

    Description Input Output Sample Input Sample Output Solution 题意:给你$n$根木棍,问你任选三根能构成三角形的概率是多少. 写挂sb细节心 ...

  3. [HDU4609] 3-idiots FFT+计数

    用FFT再去重计算出两条边加起来为某个值得方案数,然后用总方案数减去不合法方案数即可. #include<iostream> #include<cstdio> #include ...

  4. HDU4609 & FFT

    关于这道题请移步kuangbin爷的blog:http://www.cnblogs.com/kuangbin/archive/2013/07/24/3210565.html 感觉我一辈子也不能写出这么 ...

  5. [HDU4609]3-idiots(生成函数+FFT)

    3-idiots Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  6. HDU-4609(FFT/NTT)

    HDU-4609(FFT/NTT) 题意: 给出n个木棒,现从中不重复地选出3根来,求能拼出三角形的概率. 计算合法概率容易出现重复,所以建议计算不合法方案数 枚举选出的最大边是哪条,然后考虑剩下两条 ...

  7. HDU4609 FFT+组合计数

    HDU4609 FFT+组合计数 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4609 题意: 找出n根木棍中取出三根木棍可以组成三角形的概率 题解: ...

  8. HDU4609 3-idiots(母函数 + FFT)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=4609 Description King OMeGa catched three men wh ...

  9. 【hdu4609】 3-idiots FFT

    题外话:好久没写blog了啊-- 题目传送门 题目大意:给你m条长度为ai的线段,求在其中任选三条出来,能构成三角形的概率.即求在这n条线段中找出三条线段所能拼出的三角形数量除以$\binom{m}{ ...

随机推荐

  1. [js]nodejs初探http/url/fs模块

    难怪我没学会, 因为我的套路有问题. 错误点, 1,大而全 2,不注重思路 学习要领: 1, 小而精 2, 重思路(总结) nodejs特点: 1.node提供了js的运行环境, 一般将node运行在 ...

  2. Linux下安装whl文件

    直接使用pip安装: [root@mycentos ~]# pip install *.whl

  3. phpstorm----------phpstorm2017基本使用

    1.关闭2017版本的,函数参数提示.关闭方式如下: 2.如何设置代码里面的变量等号对齐,和key => value 对齐     ctrl+alt+l 3.修改PHP文件类创建的默认注释 4. ...

  4. word之高级

    1.更正拼写和语法错误. 2.取消自动编号. 3.添加删除水印. 4.段落设置首行缩进2个字符. 需要先选中需要设置的段落 5.文字覆盖. insert键切换插入与改写功能.修改word状态栏上的改写 ...

  5. 新装Windows Server 2008 r2无法连接有线网络

    新装的Windows Server 2008 r2没有网卡驱动,所以没有网络适配器. 首先,我在相同的型号电脑上查到这个主板的网卡驱动安装的是Intel(R) Ethernet Coinnection ...

  6. js语言精粹

    1.typeof null == “object” ,所以不能通过typeof ~ == "object",判断为对象   : a.判断为null的,直接~ === null:b. ...

  7. The Code analysis of the FFDNet model

    1. 读取图像并判断是否为灰度图,如为RGB图转化为灰度图,并读取图像的w.h 2.数据格式转换:将uint8表示的读取图像矩阵变为double表示. 3.加入噪声,如果噪声水平$\sigma = 5 ...

  8. MySQL数据的导出和导入

    MySQL环境变量设置,将%MySQL_HOME%下的MySQL Server 5.1/bin放到Path下. MySQL的mysqldump工具,基本用法是:   shell> mysqldu ...

  9. 文件上传时出现 Processing of multipart/form-data request failed. Unexpected EOF read on the socket错误

    上传时一直出现这个错误,修改tomcat的server.xml文件,更改tomcat版本,也查阅了网上的很多解决办法,都不能解决问题. 后在stackoverflow的一篇文章上找到了解决方法: 加上 ...

  10. javax.el.PropertyNotFoundException: Property 'know_id' not found on type java.lang.String

    今天通过Servlet明明查出来了结果,在跳转到页面时报这个异常.根据经验仔细核对了字段书写时,未发现错误. 耐心仔细检查之后发现el表达式的List集合写错了 <c:forEach items ...