HDU - 4609 指数型生成函数+FFT

3-idiots

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10462 Accepted Submission(s): 3554

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个木棍,问随机选三个可以组成三角形的概率。


我们需要求出任意两个木棍可以组合的长度值。所以我们需要指数型生成函数做乘法,这里n为1e5所以需要用FFT去优化。

之后我们枚举每一条边作为最长边,然后计算两两相加的合法个数,但是要减去当前非最长边和当前边多选的情况。


AC代码:

#pragma GCC optimize("-Ofast","-funroll-all-loops")
#include<bits/stdc++.h>
//#define int long long
using namespace std;
typedef long long ll;
const double PI=acos(-1.0);
const int N=4e5+10;
int n,T,vis[N]; ll b[N],sum[N];
int r[N],lim,l;
struct Complex{
	double x,y;
}a[N];
Complex operator + (Complex a,Complex b){return {a.x+b.x,a.y+b.y};}
Complex operator - (Complex a,Complex b){return {a.x-b.x,a.y-b.y};}
Complex operator * (Complex a,Complex b){return {a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x};}
inline void FFT(Complex *a,int n,int k){
	for(int i=0;i<n;i++)	if(i<r[i])	swap(a[i],a[r[i]]);
	for(int mid=1;mid<n;mid<<=1){
		Complex wn={cos(2*PI/(mid<<1)),k*sin(2*PI/(mid<<1))};
		for(int i=0;i<n;i+=(mid<<1)){
			Complex w={1,0};
			for(int j=0;j<mid;j++,w=(w*wn)){
				Complex t0=a[i+j],t1=w*a[i+mid+j];
				a[i+j]=t0+t1;
				a[i+mid+j]=t0-t1;
			}
		}
	}
	if(k==-1)	for(int i=0;i<n;i++)	a[i].x=a[i].x/n+0.5;
}
inline void init(int n){
	lim=1,l=0;	while(lim<=(n<<1))	lim<<=1,l++;
	for(int i=0;i<lim;i++)	r[i]=(r[i>>1]>>1)|((i&1)<<(l-1));
}
inline double calc(){
	ll res=0,fm=1LL*n*(n-1)*(n-2)/6;
	for(int i=1;i<=n;i++){
		res+=sum[lim]-sum[b[i]];
		res-=1LL*(n-i)*(n-i-1)/2;
		res-=1LL*(n-i)*(i-1);
		res-=1LL*(n-1);
	}
	return (double)res/fm;
}
inline void solve(){
	cin>>n;	memset(vis,0,sizeof vis);
	for(int i=1;i<=n;i++)	scanf("%lld",&b[i]),vis[b[i]]++;
	sort(b+1,b+1+n);	init(b[n]);
	for(int i=0;i<=lim;i++)	a[i]={0,0};
	for(int i=0;i<=b[n];i++)	a[i].x=vis[i];
	FFT(a,lim,1);
	for(int i=0;i<=lim;i++)	a[i]=a[i]*a[i];
	FFT(a,lim,-1);
	for(int i=0;i<=lim;i++)	sum[i]=a[i].x;
	for(int i=1;i<=n;i++)	sum[b[i]*2]--;
	for(int i=0;i<=lim;i++)	sum[i]/=2;
	for(int i=1;i<=lim;i++)	sum[i]+=sum[i-1];
	printf("%.7lf\n",calc());
}
signed main(){
	cin>>T;
	while(T--)	solve();
	return 0;
}
发布了442 篇原创文章 · 获赞 238 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43826249/article/details/104016072