hdu 4609 (FFT求解三角形)
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.
4
1 3 3 4
4
2 3 3 4
1.0000000
题意:给你n个边,求从其中选出3个组成三角形的概率
思路:参考-kuangbin大神
如果我们用num[i]表示长度为i的木棍有多少个,对于1 3 3 4就是
num[] = {0 1 0 2 1}从卷积的公式来看
乘法第k位置上的值 便是a[i]*b[j](i + j == k),如果位置表示的是长度,num[]表示的个数,那么卷积过后我们得到的便是两边和的个数
{0 1 0 2 1}*{0 1 0 2 1} 卷积的结果应该是{0 0 1 0 4 2 4 4 1 }。
在求出了两条边的和后,枚举第三边
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
typedef long double ld;
const ld eps=1e-10;
const int inf = 0x3f3f3f;
const int MOD = 1e9+7; const double PI = acos(-1.0); struct Complex
{
double x,y;
Complex(double _x = 0.0,double _y = 0.0)
{
x = _x;
y = _y;
}
Complex operator-(const Complex &b)const
{
return Complex(x-b.x,y-b.y);
}
Complex operator+(const Complex &b)const
{
return Complex(x+b.x,y+b.y);
}
Complex operator*(const Complex &b)const
{
return Complex(x*b.x-y*b.y,x*b.y+y*b.x);
}
}; void change(Complex y[],int len)
{
int i,j,k;
for(i = 1,j = len/2; i < len-1; i++)
{
if(i < j) swap(y[i],y[j]);
k = len/2;
while(j >= k)
{
j-=k;
k/=2;
}
if(j < k) j+=k;
}
} void fft(Complex y[],int len,int on)
{
change(y,len);
for(int h = 2; h <= len; h <<= 1)
{
Complex wn(cos(-on*2*PI/h),sin(-on*2*PI/h));
for(int j = 0; j < len; j+=h)
{
Complex w(1,0);
for(int k = j; k < j+h/2; k++)
{
Complex u = y[k];
Complex t = w*y[k+h/2];
y[k] = u+ t;
y[k+h/2] = u-t;
w = w*wn;
}
}
}
if(on == -1)
{
for(int i = 0; i < len; i++)
y[i].x /= len;
}
} const int maxn = 401000;
Complex x1[maxn],x2[maxn];
ll sum[maxn];
ll num[maxn];
int a[maxn]; int main()
{
int T;
int n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
int len = 1;
int len1;
memset(num,0,sizeof(num));
for(int i = 0; i < n; i++)
{
scanf("%d",&a[i]);
num[a[i]]++;
}
sort(a,a+n);
len1 = a[n-1] + 1;
while(len < len1*2) len <<= 1; for(int i = 0; i < len1; i++)
x1[i] = Complex(num[i],0);
for(int i = len1; i < len; i++)
x1[i] = Complex(0,0); fft(x1,len,1);
//fft(x2,len,1);
for(int i = 0; i < len; i++)
{
x1[i] =x1[i]*x1[i];
//cout << x1[i].x << " "<< x1[i].y <<endl;
}
fft(x1,len,-1);
for(int i = 0; i < len; i++)
{
sum[i] = (ll)(x1[i].x+0.5);
//cout << sum[i] << endl;
}
len = a[n-1] * 2;
for(int i = 0; i < n; i++) sum[a[i]+a[i]] --;
for(int i = 1; i <= len; i++) sum[i] /= 2;
for(int i = 1; i <= len; i++)
{
sum[i] += sum[i-1];
}
ll tot = (ll)n*(n-1)*(n-2)/6;
ll ans = 0; for(int i = 0; i < n; i++)
{
ans += sum[len]-sum[a[i]]; //两边之和大于第三边 ans -= (ll)(n-1-i) * i; //一个比自己大,一个比自己小
ans -= (n-1); //取了自己
ans -= (ll)(n-1-i)*(n-2-i)/2; //都比自己大
}
//printf("%.7lf\n",(double)ans/tot);
printf("%.7f\n",(double)ans/tot);
}
return 0;
}
hdu 4609 (FFT求解三角形)的更多相关文章
- HDU 4609 FFT模板
http://acm.hdu.edu.cn/showproblem.php?pid=4609 题意:给你n个数,问任意取三边能够,构成三角形的概率为多少. 思路:使用FFT对所有长度的个数进行卷积(\ ...
- hdu 4609 FFT
题意:给出一堆数,问从这些数中取3个能组成三角形的概率? sol:其实就是问从这些数里取3个组成三角形有多少种取法 脑洞大开的解法:用FFT 设一开始的数是1 3 3 4 作一个向量x,其中x[i]= ...
- HDU 4609 FFT+组合数学
3-idiots Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- HDU 4609 FFT+各种分类讨论
思路: http://www.cnblogs.com/kuangbin/archive/2013/07/24/3210565.html 其实我是懒得写了.... 一定要define int long ...
- hdu 4609 3-idiots [fft 生成函数 计数]
hdu 4609 3-idiots 题意: 给出\(A_i\),问随机选择一个三元子集,选择的数字构成三角形的三边长的概率. 一开始一直想直接做.... 先生成函数求选两个的方案(注意要减去两次选择同 ...
- 快速傅里叶变换应用之二 hdu 4609 3-idiots
快速傅里叶变化有不同的应用场景,hdu4609就比较有意思.题目要求是给n个线段,随机从中选取三个,组成三角形的概率. 初始实在没发现这个怎么和FFT联系起来,后来看了下别人的题解才突然想起来:组合计 ...
- hdu 4609 3-idiots
http://acm.hdu.edu.cn/showproblem.php?pid=4609 FFT 不会 找了个模板 代码: #include <iostream> #include ...
- hdu 5830 FFT + cdq分治
Shell Necklace Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- hdu 5885 FFT
XM Reserves Time Limit: 10000/10000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)T ...
随机推荐
- logging日志
import logging logging.basicConfig(filename='log.log', format='%(asctime)s - %(name)s - %(levelname) ...
- Flask 学习 十 博客文章
提交和显示博客文章 app/models.py 文章模型 class Post(db.Model): __tablename__ = 'posts' id = db.Column(db.Integer ...
- centos7 编译安装greenplum5.7
一.配置系统 安装是以一个主节点,三个子节点进行安装.gp是在github上下载的5.7的源码.地址https://github.com/greenplum-db/gpdb/tree/5.7.0. 1 ...
- Mego(07) - 关系配置
这个是本框架的重要功能,该关系就是指对象中的复杂对象或集合属性,该关系与EF中的关系是有区别的.EF中强调关系的成对出现,这是由于数据库关系的思想决定的.然而Mego更接近与对象化逻辑,我们只关心当前 ...
- Web Api 接收图片
public async Task<HttpResponseMessage> Upload() { if (!Request.Content.IsMimeMultipartContent( ...
- 增加Linux虚拟机的硬盘空间
原配置为40G,现需要增加到60G,操作方法如下: 一.虚拟机关机,在编辑设置里调整硬盘空间到60G 二.虚拟机开机,扩展硬盘空间 1.安装gparted,命令如下 sudo apt-get inst ...
- 一次“峰回路转”的troubleshooting经历
某天,用户现场人员找到我,说应用的某个功能一点就报错,在数据库上直接跑功能对应的SQL也报错,SQL大致如下: 后来向他们要了alert.log和trace files,通过分析,确定为用户数据库版本 ...
- IntelliJ IDEA sass环境配置及常见报错处理
1.下载安装ruby,网上教程很多的,安装完之后在命令行输入ruby -v检查一下是否安装成功了.(注意安装的时候要勾选第二项).
- WPF 自定义ComboBox样式
一.ComboBox基本样式 ComboBox有两种状态,可编辑和不可编辑状态.通过设置IsEditable属性可以切换控件状态. 先看基本样式效果: 基本样式代码如下: <!--ComboBo ...
- 离线Chrome插件安装文件(crx)的安装方法
离线Chrome插件安装文件(crx)的安装方法 一.正常安装方法 1.开发谷歌浏览器,设置->扩展程序 在打开的谷歌浏览器的扩展管理器中用户可以看到一些已经安装程序的Chrome插件,或者一个 ...