HDUOJ 4609 3-idiots

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4609

题意:给你n条边的长度,问你任意取三条边能组成三角形的概率。

思路:构造一个多项式,a_{i}x^{i}表示长度为i的边的个数为a_{i},那么我们如果将这个多项式自乘,则可以知道两个边相加的长度的个数,然后根据两边之和大于第三边,就可以计数了,当然这其中是有重复的,所以我们还要减去重复的。把边的长度排个序,枚举每条边,以这条边为最长边,那么可以知道重复的地方有三个:1.取了这条边;2.取了一个比它长的和比它短的;3.两个取了都比它长,枚举的时候减去就可以了,关于多项式自乘,这里要用FFT来加速。

#pragma comment(linker, "/STACK:102400000,102400000")
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define MOD 1e9+7
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
#define Key_Value ch[ch[root][1]][0]
#define DBN1(a)           cerr<<#a<<"="<<(a)<<"\n"
#define DBN2(a,b)         cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
#define DBN3(a,b,c)       cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define DBN4(a,b,c,d)     cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
#define DBN5(a,b,c,d,e)   cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n"
#define DBN6(a,b,c,d,e,f) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<", "<<#f<<"="<<(f)<<"\n"
#define clr(a,x) memset(a,x,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn=500000+5;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
template<typename T> inline T gcd(T&a,T&b){return b==0?a:gcd(b,a%b);}
template<typename T> inline T lcm(T&a,T&b){return a/gcd(a,b)*b;}
template<typename T>
inline T read(T&x){
    x=0;int _f=0;char ch=getchar();
    while(ch<'0'||ch>'9')_f|=(ch=='-'),ch=getchar();
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x=_f?-x:x;
}
struct Complex{
    double real,image;
    Complex(double _real,double _image){
        real=_real;
        image=_image;
    }
    Complex(){}
};
Complex operator+(const Complex &c1, const Complex &c2){
    return Complex(c1.real+c2.real,c1.image+c2.image);
}
Complex operator-(const Complex &c1,const Complex &c2){
    return Complex(c1.real-c2.real,c1.image-c2.image);
}
Complex operator*(const Complex &c1,const Complex &c2){
    return Complex(c1.real*c2.real-c1.image*c2.image,c1.real*c2.image+c1.image*c2.real);
}
int rev(int id,int len){
    int ret=0;
    for (int i=0;(1<<i)<len;i++){
        ret<<=1;
        if (id&(1<<i)) ret|=1;
    }
    return ret;
}
Complex tA[264000];
void FFT(Complex* a,int len,int DFT){
    for (int i=0;i<len;i++){
        tA[rev(i,len)]=a[i];
    }
    for (int s=1;(1<<s)<=len;s++){
        int m=(1<<s);
        Complex wm=Complex(cos(DFT*2*PI/m),sin(DFT*2*PI/m));
        for (int k=0;k<len;k+=m){
            Complex w=Complex(1,0);
            for (int j=0;j<(m>>1);j++){
                Complex u=w*tA[k+j+(m>>1)];
                Complex t=tA[k+j];
                tA[k+j]=t+u;
                tA[k+j+(m>>1)]=t-u;
                w=w*wm;
            }
        }
    }
    if (DFT==-1) for (int i=0;i<len;i++) tA[i].real/=len,tA[i].image/=len;
    for (int i=0;i<len;i++) a[i]=tA[i];
    return;
}
int T,n;
int branch[100010];
int num[200010];
Complex a[264000];
ll A[200010],sum[200010];
int main(){
    for (read(T);T--;){
        read(n);
        int maxBranch=0;
        for (int i=0;i<n;i++){
            read(branch[i]);
            maxBranch=max(maxBranch,branch[i]);
        }
        memset(num,0,sizeof(num));
        for (int i=0;i<n;i++) num[branch[i]]++;
        int len=1;
        while (len<=maxBranch) len<<=1;
        len<<=1;
        for (int i=0;i<=maxBranch;i++) a[i]=Complex(num[i],0);
        for (int i=maxBranch+1;i<len;i++) a[i]=Complex(0,0);
        FFT(a,len,1);
        for (int i=0;i<len;i++) a[i]=a[i]*a[i];
        FFT(a,len,-1);
        for (int i=0;i<len;i++) A[i]=1LL*(a[i].real+0.5);
        len=2*maxBranch;
        for (int i=0;i<n;i++) A[branch[i]+branch[i]]--;
        for (int i=1;i<=len;i++) A[i]/=2;
        sum[0]=0;
        for (int i=1;i<=len;i++) sum[i]=sum[i-1]+A[i];
        ll ans=0;
        sort(branch,branch+n);
        for (int i=0;i<n;i++){
            ll tmp=sum[len]-sum[branch[i]];
            tmp-=(ll)(n-i-1)*(i);
            tmp-=(ll)(n-i-1)*(n-i-2)/2;
            tmp-=(n-1);
            ans+=tmp;
        }
        printf("%.7f\n",ans*6.0/n/(n-1)/(n-2));
    }
    return 0;
}

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注