Hdu 4609 (FFT)

题目链接

3-idiots

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


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个数,从中选出三个数,问能组成三角形的概率。
首先记录每个数出现的次数。然后和自己做多项式乘法,得到两个数组合起来可以拼成的数的情况。
当然,这里应该去掉自身和自身组合,也要考虑组合的无序性。
然后将所有数排序,假设a[i]作为三角形中最大的边。代码里有相关注释。
Accepted Code:
  1 #define _CRT_SECURE_NO_WARNINGS
  2 #include <string>
  3 #include <vector>
  4 #include <algorithm>
  5 #include <numeric>
  6 #include <set>
  7 #include <map>
  8 #include <queue>
  9 #include <iostream>
 10 #include <sstream>
 11 #include <cstdio>
 12 #include <cmath>
 13 #include <ctime>
 14 #include <cstring>
 15 #include <cctype>
 16 #include <cassert>
 17 #include <limits>
 18 #include <bitset>
 19 #include <complex>
 20 #define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
 21 #define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
 22 #define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
 23 #define all(o) (o).begin(), (o).end()
 24 #define pb(x) push_back(x)
 25 #define mp(x,y) make_pair((x),(y))
 26 #define mset(m,v) memset(m,v,sizeof(m))
 27 #define INF 0x3f3f3f3f
 28 #define INFL 0x3f3f3f3f3f3f3f3fLL
 29 using namespace std;
 30 typedef vector<int> vi; typedef pair<int,int> pii; typedef vector<pair<int,int> > vpii;
 31 typedef long long ll; typedef vector<long long> vl; typedef pair<long long,long long> pll; typedef vector<pair<long long,long long> > vpll;
 32 typedef vector<string> vs; typedef long double ld;
 33 template<typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; }
 34 template<typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; }
 35 
 36 typedef long double Num;    //??????long double?????
 37 const Num PI = 3.141592653589793238462643383279L;
 38 typedef complex<Num> Complex;
 39 //n?????
 40 //a?????
 41 void fft_main(int n, Num theta, Complex a[]) {
 42     for(int m = n; m >= 2; m >>= 1) {
 43         int mh = m >> 1;
 44         Complex thetaI = Complex(0, theta);
 45         rep(i, mh) {
 46             Complex w = exp((Num)i*thetaI);
 47             for(int j = i; j < n; j += m) {
 48                 int k = j + mh;
 49                 Complex x = a[j] - a[k];
 50                 a[j] += a[k];
 51                 a[k] = w * x;
 52             }
 53         }
 54         theta *= 2;
 55     }
 56     int i = 0;
 57     reu(j, 1, n-1) {
 58         for(int k = n >> 1; k > (i ^= k); k >>= 1) ;
 59         if(j < i) swap(a[i], a[j]);
 60     }
 61 }
 62 
 63 void fft(int n, Complex a[]) { fft_main(n, 2 * PI / n, a); }
 64 void inverse_fft(int n, Complex a[]) { fft_main(n, -2 * PI / n, a); }
 65 
 66 void convolution(vector<Complex> &v, vector<Complex> &w) {
 67     int n = 1, vwn = v.size() + w.size() - 1;
 68     while(n < vwn) n <<= 1;
 69     v.resize(n), w.resize(n);
 70     fft(n, &v[0]);
 71     fft(n, &w[0]);
 72     rep(i, n) v[i] *= w[i];
 73     inverse_fft(n, &v[0]);
 74     rep(i, n) v[i] /= n;
 75 }
 76 
 77 // solve problem....
 78 const int MAXN = 100002;
 79 int a[MAXN];
 80 long long num[MAXN], sum[MAXN<<2];
 81 
 82 void read(int &res) {
 83     res = 0;
 84     char c = ' ';
 85     while (c < '0' || c > '9') c = getchar();
 86     while (c >= '0' && c <= '9') res = res * 10 + c - '0', c = getchar();
 87 }
 88 
 89 vector<long long> calc(const vector<long long> &A) {
 90     vector<Complex> Lc(all(A)), Rc(all(A));
 91     convolution(Lc, Rc);
 92     long long n = A.size() + A.size() - 1;
 93     vector<long long> res(n);
 94     rep(i, n) res[i] = (long long)(Lc[i].real() + .5);
 95 //  rep(i, n) cerr << res[i] << ", "; cerr << endl;
 96     return res;
 97 }
 98 
 99 int main() {
100     int T; read(T);
101     while (T--) {
102         int N; read(N);
103         //a[i]出现次数
104         memset(num, 0, sizeof(num));
105         rep(i, N) {
106             read(a[i]);
107             num[a[i]]++;
108         }
109         sort(a, a + N);
110         int len = a[N-1] * 2;
111         vector<long long> A(num, num + a[N-1] + 1);
112         vector<long long> ans = calc(A);
113         //重复使用
114         rep(i, N) {
115             ans[a[i]+a[i]]--;
116         }
117         //无序
118         rep(i, len) {
119             ans[i+1] /= 2;
120         }
121         //前缀和
122         sum[0] = 0;
123         rep(i, len) {
124             sum[i+1] = sum[i] + ans[i+1];
125         }
126         long long cnt = 0;
127         //假设a[i] 为最大的边
128         rep(i, N) {
129             cnt += sum[len] - sum[a[i]];
130             //a[i]本身
131             cnt -= N - 1;
132             //一大一小
133             cnt -= (long long)i * (N - i - 1); 
134             //两大
135             cnt -= (long long)(N-i-1) * (N-i-2) / 2;
136         }
137         //总的方法数 = C(N, 3)
138         long long tot = (long long)N * (N-1) * (N-2) / 6;
139         printf("%.7f\n", (cnt+0.0) / tot);
140     }
141     return 0;
142 }

 

posted on 2014-08-19 10:42  Stomach_ache  阅读(204)  评论(0编辑  收藏  举报

导航