hdu 4609 3-idiots(FFT+去重处理)
3-idiots
Problem Description
However, the three men were exactly idiots, and what they would do is only to pick the branches randomly. Certainly, they couldn't pick the same branch - but the one with the same length as another is available. Given the lengths of all branches in the forest, determine the probability that they would be saved.
Each test case begins with the number of branches N(3≤N≤105).
The following line contains N integers a_i (1≤a_i≤105), which denotes the length of each branch, respectively.
#include <bits/stdc++.h> using namespace std;
typedef long long ll;
const double pi = acos(-1.0);
const int maxn = 4e5+;
struct Complex
{
double r,i;
Complex(double _r,double _i):r(_r),i(_i){}
Complex(){}
Complex operator +(const Complex &b)
{
return Complex(r+b.r,i+b.i);
}
Complex operator -(const Complex &b)
{
return Complex(r-b.r,i-b.i);
}
Complex operator *(const Complex &b)
{
return Complex(r*b.r-i*b.i,r*b.i+i*b.r);
}
};
void change(Complex y[],int len)
{
int i,j,k;
for(i = , j = len/;i < len-;i++)
{
if(i < j)swap(y[i],y[j]);
k = len/;
while( j >= k)
{
j -= k;
k /= ;
}
if(j < k)j += k;
}
}
void fft(Complex y[],int len,int on)
{
change(y,len);
for(int h = ;h <= len;h <<= )
{
Complex wn(cos(-on**pi/h),sin(-on**pi/h));
for(int j = ;j < len;j += h)
{
Complex w(,);
for(int k = j;k < j+h/;k++)
{
Complex u = y[k];
Complex t = w*y[k+h/];
y[k] = u+t;
y[k+h/] = u-t;
w = w*wn;
}
}
}
if(on == -)
for(int i = ;i < len;i++)
y[i].r /= len;
}
int n;
int a[maxn];
ll num[maxn],sum[maxn];
Complex A[maxn];
int main()
{
//freopen("de.txt","r",stdin);
int T;
scanf("%d",&T);
while (T--){
memset(num,,sizeof num);
scanf("%d",&n);
for (int i=;i<n;++i) scanf("%d",&a[i]),num[a[i]]++;
sort(a,a+n);
int len=;
int len1=a[n-]+;
while (len<*len1) len<<=;
for (int i=;i<len1;++i)
A[i]=Complex(num[i],);
for (int i=len1;i<len;++i)
A[i]=Complex(,);
fft(A,len,);
for (int i=;i<len;++i)
A[i]=A[i]*A[i];
fft(A,len,-);
for (int i=;i<len;++i) num[i]=(ll)(A[i].r+0.5);
len = *a[n-];
for (int i=;i<n;++i)
num[a[i]+a[i]]--;
for (int i=;i<=len;++i)
num[i]/=;
sum[]=;
for (int i=;i<=len;++i) sum[i]=sum[i-]+num[i];
ll ans = ;
for (int i=;i<n;++i){
ans+=sum[len]-sum[a[i]];
ans-=(ll)(n-i-)*i;
ans-=n-;
ans-=(long long)(n-i-)*(n-i-)/;
}
ll tot =(long long )(n-)*n*(n-)/;
printf("%.7f\n",(double)ans/tot);
}
return ;
}
hdu 4609 3-idiots(FFT+去重处理)的更多相关文章
- HDU 4609 3-idiots(FFT)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4609 题意:给出n个正整数(数组A).每次随机选出三个数.问这三个数能组成三角形的概率为多大? 思路: ...
- HDU 4609 3-idiots (组合数学 + FFT)
题意:给定 n 条边,问随机选出 3 条边,能组成三角形的概率是多少. 析:答案很明显就是 能组成三角形的种数 / (C(n, 3)).现在的问题是怎么求能组成三角形的种数. 这个博客说的非常清楚了 ...
- 解题:HDU 4609 Three Idiots
题面 要求组合的方法显然我们需要对桶卷积,即设$F(x)=\sum\limits_{i=1}^{maxx}x^{cnt[i]}$,然后我们初步的先把$F^2(x)$卷出来,表示选两条边.然后我们发现如 ...
- HDU 4609 3-idiots ——(FFT)
这是我接触的第一个关于FFT的题目,留个模板. 这题的题解见:http://www.cnblogs.com/kuangbin/archive/2013/07/24/3210565.html. FFT的 ...
- hdu 4609: 3-idiots (FFT)
题目链接 题意:从N个数中,选出三个两两不同的数,求这三个数能够作为一个三角形的三边长的概率. 题解:用一个数组num[]记录大小为 i 的数出现的次数,通过 num[] 卷 num[] 得到 num ...
- hdu 4609 3-idiots [fft 生成函数 计数]
hdu 4609 3-idiots 题意: 给出\(A_i\),问随机选择一个三元子集,选择的数字构成三角形的三边长的概率. 一开始一直想直接做.... 先生成函数求选两个的方案(注意要减去两次选择同 ...
- 快速傅里叶变换应用之二 hdu 4609 3-idiots
快速傅里叶变化有不同的应用场景,hdu4609就比较有意思.题目要求是给n个线段,随机从中选取三个,组成三角形的概率. 初始实在没发现这个怎么和FFT联系起来,后来看了下别人的题解才突然想起来:组合计 ...
- bzoj 3513: [MUTC2013]idiots FFT
bzoj 3513: [MUTC2013]idiots FFT 链接 bzoj 思路 参考了学姐TRTTG的题解 统计合法方案,最后除以总方案. 合法方案要不好统计,统计不合法方案. \(a+b< ...
- hdu 4609 3-idiots——FFT
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4609 答案就是随便选三条边的方案 - 不合法的方案. 不合法的方案就是算出 x+y = k 的方案数 g[ ...
随机推荐
- Struts2基础-4 -struts拦截器
Struts2拦截器工作原理 拦截器围绕着 Action和 Result的执行而执行. Struts2拦截器的工作方式如图10.2所示.从上图中可以看出, Struts2拦截器的实现原理和 Servl ...
- ini操作
关于C#操作INI文件的总结 INI文件其实是一种具有特定结构的文本文件,它的构成分为三部分,结构如下: [Section1] key = value2 key = value2 …… [Sectio ...
- angualr项目引入容联 七陌7mroo
最近项目要求在注册页面增加客服服务浮窗,各种查找资料准备采用7moor来实现.现记录一下实现过程,便于后期查看: 引入7moor浮窗有两种方式: 1.h5方式,这种情况一般是单独打开新页面即可: 直接 ...
- [CSP-S模拟测试]:平均数(二分答案+归并排序)
题目描述 有一天,小$A$得到了一个长度为$n$的序列.他把这个序列的所有连续子序列都列了出来,并对每一个子序列都求了其平均值,然后他把这些平均值写在纸上,并对它们进行排序,最后他报出了第$k$小的平 ...
- MySQL新建数据库时utf8_general_ci编码解释
utf8_unicode_ci和utf8_general_ci对中英文来说没有实质的差别.utf8_general_ci: 校对速度快,但准确度稍差.utf8_unicode_ci: 准确度高,但校对 ...
- error C2065: “SHELLEXECUTEINFO”: 未声明的标识符
转自VC错误:http://www.vcerror.com/?p=1385 问题描述: error C2065: "SHELLEXECUTEINFO": 未声明的标识符 解决方法: ...
- xshell的安装及连接linux的使用方法
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/lx_Frolf/article/deta ...
- Ascii Chart
Char Dec Oct Hex | Char Dec Oct Hex | Char Dec Oct Hex | Char Dec Oct Hex -------------------------- ...
- Python3函数中特殊形参的使用:*、*args、**kwargs
Python3函数中特殊形参的使用:*.*args.**kwargs ==用法1:不定长参数== 当函数需要的参数数量不确定的时候,可以使用*args 和 **kwargs , 所有的位置参数保存在* ...
- C语言深度剖析自测题8解析
#include <stdio.h> int main(void) { int a[5] = {1, 2, 3, 4, 5}; int* ptr1 = (int*)(&a ...