zoukankan      html  css  js  c++  java
  • HDU 4609 3-idiots FFT

    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个数中,任取三个数能构造三角形的概率,其实就是求能构成多少个三角形。先用fft快速算出由任意两个数组成的 i 数量。 num[i] 表示由两个数组成和为 i 的数量是num[i]。
    将给定的数字a排序,每次以a[i] 为最长的边,求它能构成的三角形的数量。
    a[i]为最长的边,最小的两条边要都小于等于a[i],并且他们的和大于a[i]。
    也就是sum[len]-sum[a[i]],
    这样的话有三种情况需要考虑,
    1、一个比a[i]大,一个比a[i]小
    2、两个比a[i]大
    3、一个就是a[i],一个是其他
    将这些去除就是答案了。
      1 #include <iostream>
      2 #include <algorithm>
      3 #include <stdio.h>
      4 #include <string.h>
      5 #include <math.h>
      6 #define ll long long
      7 using namespace std;
      8 const int N = 1e5+10;
      9 const double PI = acos(-1.0);
     10 struct complex {
     11     double r,i;
     12     complex(double _r = 0,double _i = 0) {
     13         r = _r; i = _i;
     14     }
     15     complex operator +(const complex &b) {
     16         return complex(r+b.r,i+b.i);
     17     }
     18     complex operator -(const complex &b) {
     19         return complex(r-b.r,i-b.i);
     20     }
     21     complex operator *(const complex &b) {
     22         return complex(r*b.r-i*b.i,r*b.i+i*b.r);
     23     }
     24 };
     25 void change(complex y[],int len) {
     26     int i,j,k;
     27     for(i = 1, j = len/2;i < len-1;i++) {
     28         if(i < j)swap(y[i],y[j]);
     29         k = len/2;
     30         while( j >= k) {
     31             j -= k;
     32             k /= 2;
     33         }
     34         if(j < k)j += k;
     35     }
     36 }
     37 void fft(complex y[],int len,int on) {
     38     change(y,len);
     39     for(int h = 2;h <= len;h <<= 1) {
     40         complex wn(cos(-on*2*PI/h),sin(-on*2*PI/h));
     41         for(int j = 0;j < len;j += h) {
     42             complex w(1,0);
     43             for(int k = j;k < j+h/2;k++) {
     44                 complex u = y[k];
     45                 complex t = w*y[k+h/2];
     46                 y[k] = u+t;
     47                 y[k+h/2] = u-t;
     48                 w = w*wn;
     49             }
     50         }
     51     }
     52     if(on == -1)
     53         for(int i = 0;i < len;i++)
     54             y[i].r /= len;
     55 }
     56 
     57 const int MAXN = 400040;
     58 complex x1[MAXN];
     59 int a[MAXN/4], n, m;
     60 ll num[MAXN], sum[MAXN];
     61 
     62 int main() {
     63     int t, n;
     64     cin >> t;
     65     while(t--) {
     66         cin >> n;
     67         memset(num, 0, sizeof(num));
     68         for(int i = 0; i < n; i ++) {
     69             cin >> a[i];
     70             num[a[i]]++;
     71         }
     72         sort(a,a+n);
     73         int len = 1, len1 = a[n-1]+1;
     74         while(len < 2*len1)len <<= 1;
     75         for(int i = 0;i < len1; i ++)
     76             x1[i] = complex(num[i],0);
     77         for(int i = len1;i < len; i ++)
     78             x1[i] = complex(0,0);
     79         fft(x1,len,1);
     80         for(int i = 0;i < len;i++)
     81             x1[i] = x1[i]*x1[i];
     82         fft(x1,len,-1);
     83         for(int i = 0;i < len;i++)
     84             num[i] = (long long)(x1[i].r+0.5);
     85 
     86            len = a[n-1]*2;
     87            for(int i = 0; i < n; i ++) num[a[i]+a[i]]--;
     88            for(int i = 0; i <= len; i ++) {
     89                num[i] /= 2;
     90            }
     91            sum[0] = 0;
     92            for(int i = 1; i <= len; i ++) sum[i] = sum[i-1] + num[i];
     93            ll ans = 0;
     94            for(int i = 0; i < n; i ++) {
     95                ans += sum[len]-sum[a[i]];
     96                ans -= 1LL*i*(n-i-1);
     97                ans -= 1LL*(n-i-1)*(n-i-2)/2;
     98                ans -= 1LL*(n-1);
     99            }
    100            // for(int i = 0; i < len; i ++) printf("%d %lld
    ",i,sum[i] );
    101            ll tot = 1LL*n*(n-1)*(n-2)/6;
    102            // printf("%lld %lld
    ",ans,tot);
    103            printf("%.7lf
    ",1.0*ans/tot);
    104     }
    105     return 0;
    106 }
  • 相关阅读:
    shell学习三十八天----运行顺序和eval
    开发新手教程【三】Arduino开发工具
    【Cloud Foundry】Cloud Foundry学习(四)——Service
    java调用oracle函数
    JDK动态代理
    Nginx 笔记与总结(8)Location:归纳总结
    机器学习从入门到放弃之决策树算法
    用数据分析进行品类管理
    用数据分析进行品类管理
    大数据分析过程中经常遇到那13个问题
  • 原文地址:https://www.cnblogs.com/xingkongyihao/p/9936255.html
Copyright © 2011-2022 走看看