3-idiots

Problem Description

King OMeGa catched three men who had been streaking in the street. Looking as idiots though, the three men insisted that it was a kind of performance art, and begged the king to free them. Out of hatred to the real idiots, the king wanted to check if they were lying. The three men were sent to the king's forest, and each of them was asked to pick a branch one after another. If the three branches they bring back can form a triangle, their math ability would save them. Otherwise, they would be sent into jail.
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.
 
Input
An integer T(T≤100) will exist in the first line of input, indicating the number of test cases.
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.
 
Output
Output the probability that their branches can form a triangle, in accuracy of 7 decimal places.
 
Sample Input
2
4
1 3 3 4
4
2 3 3 4
Sample Output
0.5000000
1.0000000
 
题意给你n个木棍,问从中选出三根能组成一个三角形,那么有多少种选法?求出合法选法除以总选法的概率
FFT可以处理选择任意两个木棍,这两个木棍的和的情况数,我们这里是算了好多重复的比如选了a[i]又选了a[i],我需要减一下
再一想选x选y跟选y选x是一样的,我们再除以2,我们对两根木棍的和的情况求一个前缀和
然后我们枚举a[i]作为三个木棍中最长的一个,我们先对答案笼统的加上剩下两根木棍和大于a[i]的情况数
我们还要减去剩下两个木棍一根>=a[i]另一根<=a[i]的情况,再减去剩下两根木棍都>a[i]的情况,再减去剩下两根木棍中a[i]又被选了的情况
统计一下就是答案了
#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+去重处理)的更多相关文章

  1. HDU 4609 3-idiots(FFT)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4609 题意:给出n个正整数(数组A).每次随机选出三个数.问这三个数能组成三角形的概率为多大? 思路: ...

  2. HDU 4609 3-idiots (组合数学 + FFT)

    题意:给定 n 条边,问随机选出 3 条边,能组成三角形的概率是多少. 析:答案很明显就是  能组成三角形的种数 / (C(n, 3)).现在的问题是怎么求能组成三角形的种数. 这个博客说的非常清楚了 ...

  3. 解题:HDU 4609 Three Idiots

    题面 要求组合的方法显然我们需要对桶卷积,即设$F(x)=\sum\limits_{i=1}^{maxx}x^{cnt[i]}$,然后我们初步的先把$F^2(x)$卷出来,表示选两条边.然后我们发现如 ...

  4. HDU 4609 3-idiots ——(FFT)

    这是我接触的第一个关于FFT的题目,留个模板. 这题的题解见:http://www.cnblogs.com/kuangbin/archive/2013/07/24/3210565.html. FFT的 ...

  5. hdu 4609: 3-idiots (FFT)

    题目链接 题意:从N个数中,选出三个两两不同的数,求这三个数能够作为一个三角形的三边长的概率. 题解:用一个数组num[]记录大小为 i 的数出现的次数,通过 num[] 卷 num[] 得到 num ...

  6. hdu 4609 3-idiots [fft 生成函数 计数]

    hdu 4609 3-idiots 题意: 给出\(A_i\),问随机选择一个三元子集,选择的数字构成三角形的三边长的概率. 一开始一直想直接做.... 先生成函数求选两个的方案(注意要减去两次选择同 ...

  7. 快速傅里叶变换应用之二 hdu 4609 3-idiots

    快速傅里叶变化有不同的应用场景,hdu4609就比较有意思.题目要求是给n个线段,随机从中选取三个,组成三角形的概率. 初始实在没发现这个怎么和FFT联系起来,后来看了下别人的题解才突然想起来:组合计 ...

  8. bzoj 3513: [MUTC2013]idiots FFT

    bzoj 3513: [MUTC2013]idiots FFT 链接 bzoj 思路 参考了学姐TRTTG的题解 统计合法方案,最后除以总方案. 合法方案要不好统计,统计不合法方案. \(a+b< ...

  9. hdu 4609 3-idiots——FFT

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4609 答案就是随便选三条边的方案 - 不合法的方案. 不合法的方案就是算出 x+y = k 的方案数 g[ ...

随机推荐

  1. Mybatis原理及源码分析

    什么是Mybatis? Mybatis是一个半自动化的持久层框架. Mybatis可以将向PreparedStatement中的输入参数自动进行映射(输入映射),将结果集映射成Java对象(输出映射) ...

  2. 如何在MaxCompute上处理存储在OSS上的开源格式数据

    0. 前言 MaxCompute作为使用最广泛的大数据平台,内部存储的数据以EB量级计算.巨大的数据存储量以及大规模计算下高性能数据读写的需求,对于MaxCompute提出了各种高要求及挑战.处在大数 ...

  3. 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 GSM Base Station Identification (点在多边形内模板)

    In the Personal Communication Service systems such as GSM (Global System for Mobile Communications), ...

  4. ini操作

    关于C#操作INI文件的总结 INI文件其实是一种具有特定结构的文本文件,它的构成分为三部分,结构如下: [Section1] key = value2 key = value2 …… [Sectio ...

  5. [CSP-S模拟测试]:God Knows(线段树维护单调栈)

    题目描述 小$w$来到天堂的门口,对着天堂的大门发呆.大门上有一个二分图,左边第$i$个点连到右边第$p_i$个点.(保证$p_i$是一个排列).小$w$每次可以找左边某个对应连线尚未被移除的点$i$ ...

  6. mysql捕捉所有SQL语句

    MySQL可以通过开通general_log参数(可动态修改)来扑捉所有在数据库执行的SQL语句.显示参数:mysql> show variables like 'general%log%';+ ...

  7. Oralce常用系统函数

    dual:Oracle系统内部提供的一个用于实现临时数据计算的特殊表,它只有一个列DUMMY (VARCHAR2(1)) 字符类函数: concat--连接字符串 initcap--每个单词首字母大写 ...

  8. 嵌入式C语言3.3 关键字---逻辑结构

    1. if  else if(条件表达式){ ****;} else {xxxxxx;} 2. switch    case    default 3. do   while   for 4. con ...

  9. 【设计模式】FactoryPattern工厂模式

    Factory Pattern 简单工厂模式 将变化的部分封装起来 //简单工厂 class SimpleProductFactory{ Product createProduct(String ty ...

  10. select 和 order by

    select 的优先级要高于order by,相当于是select先创建了一个临时表,再通过临时表去排序.所以,对于一些sum()的汇总,在进行排序,实际是排序的select后的字段,而不是表里的那个 ...