Hdu 4609 3-idiots FFT

3-idiots

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


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≤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
 

Source


在所有的线段中选三个边,求可以构成三角形的概率。


只要求有多少种组合。

用FFT求选两条边时,总长度为i的方法数a[i],这一数组可以卷积得到。

之后枚举最长的一条边,对于它,答案就是比它长的两条边组合数减去不符合要求的情况。

不符的情况有如下几种:选择自己和另外一条边,选择两条比自己长的边,选择一条比自己长的边。

依次减去即可。


要注意的是这题卡空间,需要在fft的时候把复数常量直接用cos和sin表示,如果直接预处理并保存会爆空间。

还有就是要注意计数过程当中long long的问题。


#include <cstdio>
#include <iostream>
#include <string.h>
#include <string> 
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=400005, inf = 0x3f3f3f3f;
const ll llinf = 0x3f3f3f3f3f3f3f3f;
const ld pi = acos(-1.0L);
ll cnt[maxn],p[maxn],x[maxn/4];

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);
	}
};
complex conj(complex a) {
	return complex(a.r, -a.i);
}
complex a[maxn], b[maxn];

struct fff {
	int n, rev[maxn];
	complex o[maxn], oi[maxn];

	void init(int m) {
		n = 1;
		int k = 0;
		while (n < m) n <<= 1, k++;
		for (int i = 0; i < n; i++) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (k - 1));
		for (k = 0; k < n; k++) {
			o[k] = complex(cos(2 * pi / n * k), sin(2 * pi / n * k));
			oi[k] = conj(o[k]);
		}
	}

	void fft(complex *a, complex *w) {
		for (int i = 0; i < n; i++)
			if (i < rev[i]) swap(a[i], a[rev[i]]);
		for (int l = 2; l <= n; l <<= 1) {
			int m = l >> 1;
			for (complex *p = a; p != a + n; p += l)
				for (int k = 0; k < m; k++) {
					complex t = w[n / l * k] * p[k + m];
					p[k + m] = p[k] - t;
					p[k] = p[k] + t;
				}
		}
	}

	void dft(complex *a, int f) {
		if (f == 1) fft(a, o); else {
			fft(a, oi);
			for (int i = 0; i < n; i++) a[i].r /= n;
		}
	}

	void mul(complex *a, complex *b, int m) {
		dft(a, 1); dft(b, 1);
		for (int i = 0; i < n; i++) a[i] = a[i] * b[i];
		dft(a, -1);
	}
};
fff f;

int main() {
	int cas;
	scanf("%d", &cas);
	f.init(200000);
	while (cas--) {
		ll n, i, y = -1;
		scanf("%lld", &n);
		mem0(cnt);
		for (i = 1; i <= n; i++) {
			scanf("%lld", &x[i]);
			cnt[x[i]-1]++;
			y = max(y, x[i]);
		}
		mem0(a); mem0(b); mem0(p);
		for (i = 0; i < y*2; i++) {
			a[i].r = b[i].r = cnt[i];
			p[i*2+2] = -cnt[i];
		}
		f.mul(a, b, 2 * y);
		ll ans = 0;
		for (i = 1; i < 2 * y; i++) {
			p[i+1] = ((ll) floor(a[i-1].r + 0.5) +p[i+1])/ 2;
			p[i+1] += p[i];
		}
		sort(x + 1, x + n + 1);
		for (i = 1; i <=n; i++) {
			ans += p[2 * y] - p[x[i]];
			ans -= n - 1;
			ans -= (n - i)*(n - i - 1) / 2;
			ans -= (n - i)*(i-1);
		}
		db pb = 6.0*((db)ans) / ((n - 1)*(n - 2)*n);
		printf("%.7lf\n", pb);
	//	printf("%lld\n", ans);
	}
//	system("pause");
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值