3-idiots

题目

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≤10 5).
The following line contains N integers a_i (1≤a_i≤10 5), 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 n n个数字,任选不同三个能构成三角形的概率~
解题思路:我们判断构成三角形,那就是两个最小边之和 > > >最大边,这里我们先统计任意不同两条边的和,然后对于每一个最大边,枚举三角形的最大边,找出不能构成三角形的组合,即两边之和小于这个最大边的情况,然后 1-不合要求/总数 的概率就是构成三角形的概率啦~总数为 n ∗ ( n − 1 ) ∗ ( n − 2 ) / 6 n*(n-1)*(n-2)/6 n(n1)(n2)/6;
( P s : (Ps: (Ps:也有直接求构成三角形的做法啦,我这里提供一种简单点的 ) ) )

代码如下

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
const int mx=1e5+10;
const double pi=acos(-1.0);
struct Complex{
    double r,i;
    Complex(double r=0,double i=0):r(r),i(i){};
    Complex operator + (const Complex &rhs){
        return Complex(r+rhs.r,i+rhs.i);
    }
    Complex operator - (const Complex &rhs){
        return Complex(r-rhs.r,i-rhs.i);
    }
    Complex operator * (const Complex &rhs){
        return Complex(r*rhs.r-i*rhs.i,i*rhs.r+r*rhs.i);
    }
};
Complex F[mx*4];
int n,t,len;
ll num[mx*4],a[mx*4],sum[mx*4];
ll tot,cnt;

inline void sincos(double theta,double &p0,double &p1){
    p0=sin(theta);
    p1=cos(theta);
}

void FFT(Complex P[], int n, int oper){
    for(int i=1,j=0;i<n-1;i++){
        for(int s=n;j^=s>>=1,~j&s;);
        if(i<j)swap(P[i],P[j]);
    }
    Complex unit_p0;
    for(int d=0;(1<<d)<n;d++){
        int m=1<<d,m2=m*2;
        double p0=pi/m*oper;
        sincos(p0,unit_p0.i,unit_p0.r);
        for(int i=0;i<n;i+=m2){
            Complex unit=1;
            for(int j=0;j<m;j++){
                Complex &P1=P[i+j+m],&P2=P[i+j];
                Complex t=unit*P1;
                P1=P2-t;
                P2=P2+t;
                unit=unit*unit_p0;
            }
        }
    }
    if(oper==-1)for(int i=0;i<len;i++)P[i].r/=len;
}

void Conv(Complex a[],int len){//求卷积
    FFT(a,len,1);//FFT
    for(int i=0;i<len;++i)a[i]=a[i]*a[i];
    FFT(a,len,-1);//IFFT
}

int main()
{
	scanf("%d",&t);
	while(t--)
	{
		memset(num,0,sizeof(num));
		scanf("%d",&n);
		ll maxx=-1;
		len=1;
		cnt=0;
		for(int i=0;i<n;i++)
		{
		  scanf("%lld",&a[i]);
		  maxx=max(maxx,a[i]);
		  num[a[i]]++;
	    }
	    int len1=maxx+1;
	    while(len<len1*2)
	    len<<=1;
	    
	    for(int i=0;i<len1;i++)
	    F[i]=Complex(num[i],0);
	    
	    for(int i=len1;i<len;i++)
	    F[i]=Complex(0,0);
	    
	    Conv(F,len);
	    
	    for(int i=0;i<len;i++)
	    num[i]=(ll)(F[i].r+0.5);//以上基本操作统计
	    
	    for(int i=0;i<n;i++)
	    num[a[i]+a[i]]--;//减掉自身相加的情况啦
	    
	    for(int i=0;i<len;i++)//这里除2是保证无序
	    num[i]/=2;
	    
	    sum[0]=0;
	    for(int i=0;i<len;i++)
	    sum[i]=sum[i-1]+num[i];//sum数组统计任意两条边之和<=i的个数
	    
	    for(int i=0;i<n;i++)
	    cnt+=sum[a[i]]; //枚举每一条边作为最大边,找出不和情况的两小边之和的情况
	    
	    tot=(ll)n*(n-1)*(n-2)/6;
	    printf("%.7lf\n",1-cnt*1.0/tot);
	}
	return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值