HDU - 4609 3-idiots (FFT)

这篇博客介绍了如何使用快速傅里叶变换(FFT)来解决数学问题,特别是计算排列组合计数,并应用这些计数来求解求任选3个数能构成三角形的概率问题。通过排序、多项式乘法和容斥原理,博主给出了三种不同的实现方式,分别处理了重复计数和除以2的情况,最后得出概率结果。
摘要由CSDN通过智能技术生成

题链:https://vjudge.net/problem/HDU-4609

题意:n个数,求任选3个数组成三角形的概率。

思路:先排一下序,然后就变成求 ai+aj>ak (i<j<k)三元组的个数,和此题类似,将ai看作多项式的指数num[ai]看作多项式的系数。FFT求多项式相乘,再计算答案,系数ai就是对应和为i个数。首先要把自己加自己(ai+ai)的减去,然后再除于2(因为 ai+aj 和 aj+ai 都算了)。注意,我们要求的ai+aj>ak,ai和aj要小于ak。根据容斥原理,要减去一大一小,有自己本身,两个都大的情况

代码1:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5+10;
const int M = 9e4+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 &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 = 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].r /= len;
}
Complex x1[N<<2];
Complex x2[N<<2];
int n,m,a[N];
int num[N];
ll num1[N<<1],pre[N<<1]; 
int main(void){
	int t;
	scanf("%d",&t);
	while(t--){
		int maxa=0;
		scanf("%d",&n);
		memset(num,0,sizeof num);
		for(int i=1;i<=n;i++){
			scanf("%d",&a[i]);
			maxa=max(maxa,a[i]);
			num[a[i]]++;
		}
		sort(a+1,a+1+n);
		int len,len1;
		len1=maxa+1;
		len=1;
		while(len < 2 * len1) 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);
		for(int i = 0;i < len;i++)
	            x1[i] = (x1[i]*x1[i]);
		fft(x1,len,-1);	
		maxa<<=1;
	    for(int i = 0;i <= maxa ;i++)
	        num1[i] = (long long)(x1[i].r+0.5);	
		for(int i=1;i<=n;i++)//ai+ai 
			num1[(a[i]<<1)]--;
		pre[0]=0;
		for(int i=1;i<= maxa ;i++){
			num1[i]>>=1;
			pre[i]=pre[i-1]+num1[i];
		}
		ll up=0;
		for(int i=1;i<=n;i++){
			up+=(pre[maxa]-pre[a[i]]);
			//一大一小 
			up-=1LL*(n-i)*(i-1);
			//自己本身 
			up-=(n-1);
			//两个都大 
			up-=1LL*(n-i)*(n-i-1)/2;
		}	
		ll down = 1LL*n*(n-1)*(n-2)/6;		
		printf("%.7f\n",1.0*up/down);	
	}
	
	return 0;	
} 

代码2: 不事先除以2

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5+10;
const int M = 9e4+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 &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 = 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].r /= len;
}
Complex x1[N<<2];
Complex x2[N<<2];
int n,m,a[N];
int num[N];
long long num1[N<<1],pre[N<<1]; 
int main(void){
    int t;
    scanf("%d",&t);
    while(t--){
        int maxa=0;
        scanf("%d",&n);
        memset(num,0,sizeof num);
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
            maxa=max(maxa,a[i]);
            num[a[i]]++;
        }
        sort(a+1,a+1+n);
        int len,len1;
        len1=maxa+1;
        len=1;
        while(len < 2 * len1) 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);
        for(int i = 0;i < len;i++)
                x1[i] = (x1[i]*x1[i]);
        fft(x1,len,-1);
		maxa<<=1;    
        for(int i = 0;i <= maxa ;i++)
            num1[i] = (long long)(x1[i].r+0.5);    
        for(int i=1;i<=n;i++)
            num1[(a[i]<<1)]--;
        pre[0]=0;
        for(int i=1;i<=maxa ;i++){
            pre[i]=pre[i-1]+num1[i];
        }
        ll up=0;
        for(int i=1;i<=n;i++){
            up+=pre[maxa]-pre[a[i]];
			//不除于2,要减去(i,1),(i,2),(i,3),...(i,i-1),(i,i+1),...,(i,n).注意 (i,i)已经事先减去了
						   //(i+1,1),(i+1,2),(i+1,3),...(i+1,i),(i+1,i+2),...,(i,n).注意 (i+1,i+1)已经事先减去了 
						   //         ...... 
						   //(n,1),(n,2),(n,3),...(n,i),...,(n,n-1).注意 (n,n)已经事先减去了 
			up-=1LL*(n-i+1)*(n-1);
			//不除于2,要减去(1,i),(1,i+1),(1,i+2),...(1,n).
						   //(2,i),(2,i+1),(2,i+2),...(2,n).
						   //         ...... 
						   //(i-1,i),(i-1,i+1),(i-1,i+2),...(i-1,n). 
            up-=1LL*(i-1)*(n-i+1);
        }    
        up/=2;//最后除以2 
        ll down =1LL*n*(n-1)*(n-2)/6;        
        printf("%.7f\n",1.0*up/down);    
    }
    
    return 0;    
} 

代码3:除以2,另一种容斥方式

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5+10;
const int M = 9e4+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 &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 = 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].r /= len;
}
Complex x1[N<<2];
Complex x2[N<<2];
int n,m,a[N];
int num[N];
ll num1[N<<1],pre[N<<1]; 
int main(void){
	int t;
	scanf("%d",&t);
	while(t--){
		int maxa=0;
		scanf("%d",&n);
		memset(num,0,sizeof num);
		for(int i=1;i<=n;i++){
			scanf("%d",&a[i]);
			maxa=max(maxa,a[i]);
			num[a[i]]++;
		}
		sort(a+1,a+1+n);
		int len,len1;
		len1=maxa+1;
		len=1;
		while(len < 2 * len1) 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);
		for(int i = 0;i < len;i++)
	            x1[i] = (x1[i]*x1[i]);
		fft(x1,len,-1);	
		maxa<<=1;
	    for(int i = 0;i <= maxa ;i++)
	        num1[i] = (long long)(x1[i].r+0.5);	
		for(int i=1;i<=n;i++)
			num1[(a[i]<<1)]--;
		pre[0]=0;
		for(int i=1;i<= maxa ;i++){
			num1[i]>>=1;
			pre[i]=pre[i-1]+num1[i];
		}
		ll up=0;
		for(int i=1;i<=n;i++){
			up+=(pre[maxa]-pre[a[i]]);
			//要减去(i,1),(i,2),(i,3),...(i,i-1),(i,i+1),...,(i,n).注意 (i,i)已经事先减去了
				  //(i+1,1),(i+1,2),(i+1,3),...(i+1,i),(i+1,i+2),...,(i,n).注意 (i+1,i+1)已经事先减去了 
				  //         ...... 
				  //(n,1),(n,2),(n,3),...(n,i),...,(n,n-1).注意 (n,n)已经事先减去了 					
			up-=1LL*(n-i+1)*(n-1);
			//多减了两个都大的情况,(i,i+1),(i+1,i)都减了 
			up+=1LL*(n-i+1)*(n-i)/2;
		}	
		ll down = 1LL*n*(n-1)*(n-2)/6;		
		printf("%.7f\n",1.0*up/down);	
	}
	
	return 0;	
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值