关闭

hdu 4609 3-idiots (FFT)

标签: FFTACM
136人阅读 评论(0) 收藏 举报
分类:

被每个长度生成一个函数,个数作为系数,然后FFT一下,然后把每条边作为最长边算一下,注意减去他不是最长边的情况。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=220005;
const double eps(1e-8);
typedef long long LL;
const double PI = acos(-1.0);

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 A[maxn<<1];
void FFT(Complex* a, int len, int DFT)//对a进行DFT或者逆DFT, 结果存在a当中,len必须是2的幂而且len大于多项式最高次数
{
    for(int i = 0; i < len; i++)
        A[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 t = w*A[k + j + (m >> 1)];
                Complex u = A[k + j];
                A[k + j] = u + t;
                A[k + j + (m >> 1)] = u - t;
                w = w*wm;
            }
        }
    }
    if(DFT == -1) for(int i = 0; i < len; i++) A[i].real /= len, A[i].image /= len;
    for(int i = 0; i < len; i++) a[i] = A[i];
    return;
}
Complex a[maxn<<1];
int san[maxn];
int num[maxn<<1];
LL xishu[maxn<<1];
int main()
{
    int t,n;
    cin>>t;
    while(t--){
        scanf("%d",&n);
        int len=1;
        int s=0;
        for(int i=1;i<=n;i++){
            scanf("%d",&san[i]);
            s=max(s,san[i]);
        }
        while(len<=s){
            len<<=1;
        }
        len<<=1;
        sort(san+1,san+1+n);
        for(int i=0;i<len;i++){
            a[i]=Complex(0,0);
            num[i]=0;
        }
        for(int i=1;i<=n;i++){
            a[san[i]].real+=1;
            num[san[i]]++;
        }
        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++){
            xishu[i]=(LL)(a[i].real+0.5);
        }
        for(int i=0;i<len/2;i++){
            xishu[2*i]-=num[i];
        }
        for(int i=0;i<len;i++){
            xishu[i]/=2;
        }
        xishu[len]=0;
        for(int i=len-1;i>=0;i--){
            xishu[i]+=xishu[i+1];
        }
        LL ans=0;
        for(int i=n;i>=1;i--){
            ans+=xishu[san[i]+1];
            ans-=((LL)(i+n-2)*(n-i+1))/2;
        }
        double ans1=1.0*ans/((LL)n*(n-1)*(n-2)/6);
        printf("%.7lf\n",ans1);
    }
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:87058次
    • 积分:3098
    • 等级:
    • 排名:第11537名
    • 原创:230篇
    • 转载:0篇
    • 译文:0篇
    • 评论:14条