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

    3-idiots

    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

      任意两条边组合的情况就是卷积

      快速求出来后,容斥一下

    #include<bits/stdc++.h>
    using namespace std;
    #pragma comment(linker, "/STACK:102400000,102400000")
    #define ls i<<1
    #define rs ls | 1
    #define mid ((ll+rr)>>1)
    #define pii pair<int,int>
    #define MP make_pair
    typedef long long LL;
    const long long INF = 1e18+1LL;
    const double pi = acos(-1.0);
    const int N = 1e5+10, M = 1e3+20,inf = 2e9,mod = 1e9+7;
    
    
    struct Complex {
        double r , i ;
        Complex () {}
        Complex ( double r , double i ) : r ( r ) , i ( i ) {}
        Complex operator + ( const Complex& t ) const {
            return Complex ( r + t.r , i + t.i ) ;
        }
        Complex operator - ( const Complex& t ) const {
            return Complex ( r - t.r , i - t.i ) ;
        }
        Complex operator * ( const Complex& t ) const {
            return Complex ( r * t.r - i * t.i , r * t.i + i * t.r ) ;
        }
    } ;
    
    void FFT ( Complex y[] , int n , int rev ) {
        for ( int i = 1 , j , t , k ; i < n ; ++ i ) {
            for ( j = 0 , t = i , k = n >> 1 ; k ; k >>= 1 , t >>= 1 ) j = j << 1 | t & 1 ;
            if ( i < j ) swap ( y[i] , y[j] ) ;
        }
        for ( int s = 2 , ds = 1 ; s <= n ; ds = s , s <<= 1 ) {
            Complex wn ( cos ( rev * 2 * pi / s ) , sin ( rev * 2 * pi / s ) ) , w ( 1 , 0 ) , t ;
            for ( int k = 0 ; k < ds ; ++ k , w = w * wn ) {
                for ( int i = k ; i < n ; i += s ) {
                    y[i + ds] = y[i] - ( t = w * y[i + ds] ) ;
                    y[i] = y[i] + t ;
                }
            }
        }
        if ( rev == -1 ) for ( int i = 0 ; i < n ; ++ i ) y[i].r /= n ;
    }
    
    Complex y[N*4];
    int T,n,num[N],x,sum[N];
    int main() {
        scanf("%d",&T);
        while(T--) {
            int mx = 0;
            scanf("%d",&n);
            memset(num,0,sizeof(num));
            for(int i = 1; i <= n; ++i)
                scanf("%d",&x),num[x]++,mx = max(mx,x);
                
            for(int i = 0; i <= mx; ++i)
                y[i] = Complex(num[i],0);
            for(int i = mx+1; i < n1; ++i)
                y[i] = Complex(0,0);
            FFT(y,n1,1);
            for(int i = 0; i < n1; ++i)
                y[i] = y[i]*y[i];
            FFT(y,n1,-1);
            LL tot = (LL) n*(n-1)*(n-2)/6,ans = tot;
            for(int i = 1; i <= mx; ++i) sum[i] = sum[i-1]+num[i];
            for(int i = 2; i <= mx; ++i) {
                LL x = (LL) (y[i].r + 0.5);
                if(i%2==0) x -= num[i/2];
                x /= 2;
                ans -= x * (n - sum[i-1]);
            }
            printf("%.7f
    ",(double)ans/tot);
        }
        return 0;
    }

      

      

  • 相关阅读:
    django 的操作
    numpy 初步学习
    django orm 多对多操作
    selenium 安装chrome 以及chromedriver
    配置uwsgi + nginx
    ubuntu安装 anaconda
    django ---手动删除数据库
    前端(六)之盒模型显隐、定位与流式布局思想
    前端(五)之display 总结与浮动
    前端(四)之精灵图与盒模型布局
  • 原文地址:https://www.cnblogs.com/zxhl/p/7101632.html
Copyright © 2011-2022 走看看