[MUTC2013]idiots

嘟嘟嘟


首先\(O(n ^ 2)\)大家都会,枚举最长边,然后找两条短边满足两边之大于第三边即可。
然后估计就没法优化了。


正难则反,如果枚举的两条短边小于等于第三边会怎么样呢?发现\(a_i \leqslant 10 ^ 5\),那就可以FFT求出凑出每一条边的方案了,记为\(f(i)\)。不过还要减去自己配自己的情况,以及每一种方案实际上被算了两遍。
那么我们可以枚举最长边,然后所有不合法的方案就是\(cnt(i) * \sum _{j = 1} ^ {i} f(j)\)了,\(cnt(i)\)表示长度为\(i\)的边的个数。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cctype>
#include<map>
#include<queue>
#include<vector>
#include<assert.h>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 3e5 + 5;
const db PI = acos(-1);
In ll read()
{
    ll ans = 0;
    char ch = getchar(), las = ' ';
    while(!isdigit(ch)) las = ch, ch = getchar();
    while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
    if(las == '-') ans = -ans;
    return ans;
}
In void write(ll x)
{
    if(x < 0) putchar('-'), x = -x;
    if(x >= 10) write(x / 10);
    putchar(x % 10 + '0');
}
In void MYFILE()
{
#ifndef mrclr
    freopen("ha.in", "r", stdin);
    freopen("ha.out", "w", stdout);
#endif
}

int n, Max = 0, a[maxn], cnt[maxn];
ll ans[maxn];

int rev[maxn];
struct Comp
{
    db x, y;
    In Comp operator + (const Comp& oth)const
    {
        return (Comp){x + oth.x, y + oth.y};
    }
    In Comp operator - (const Comp& oth)const
    {
        return (Comp){x - oth.x, y - oth.y};
    }
    In Comp operator * (const Comp& oth)const
    {
        return (Comp){x * oth.x - y * oth.y, x * oth.y + y * oth.x};
    }
    friend In void swap(Comp& a, Comp& b)
    {
        swap(a.x, b.x); swap(a.y, b.y);
    }
}b[maxn], c[maxn], ret[maxn];
In void fft(Comp* a, int len, int flg)
{
    for(int i = 0; i < len; ++i) if(i < rev[i]) swap(a[i], a[rev[i]]);
    for(int i = 1; i < len; i <<= 1)
    {
        Comp omg = (Comp){cos(PI / i), sin(PI / i) * flg};
        for(int j = 0; j < len; j += (i << 1))
        {
            Comp o = (Comp){1, 0};
            for(int k = 0; k < i; ++k, o = o * omg)
            {
                Comp tp1 = a[k + j], tp2 = o * a[k + j + i];
                a[k + j] = tp1 + tp2, a[k + j + i] = tp1 - tp2;
            }
        }
    }
}
int main()
{
//  MYFILE();
    int T = read();
    while(T--)
    {
        n = read(); Mem(cnt, 0); Max = 0;
        for(int i = 0; i < maxn; ++i) b[i] = c[i] = (Comp){0, 0};
        for(int i = 1; i <= n; ++i)
        {
            ++cnt[a[i] = read()];
            Max = max(Max, a[i]);   
        } 
        for(int i = 1; i <= Max; ++i) b[i] = c[i] = (Comp){1.0 * cnt[i], 0};
        int len = 1, lim = 0;
        while(len <= Max + Max) len <<= 1, ++lim;
        for(int i = 0; i < len; ++i) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (lim - 1));
        fft(b, len, 1), fft(c, len, 1);
        for(int i = 0; i < len; ++i) ret[i] = b[i] * c[i];
        fft(ret, len, -1);
        for(int i = 1; i <= Max; ++i)
        {
            ans[i] = (ll)(ret[i].x / len + 0.5);
            if(!(i & 1)) ans[i] -= cnt[i >> 1];
            ans[i] >>= 1;
        }
        ll ans1 = 0, sum = 0, ans2 = 1LL * n * (n - 1) * (n - 2) / 6;
        for(int i = 1; i <= Max; ++i) sum += ans[i], ans1 += cnt[i] * sum;
        printf("%.7lf\n", 1.0 * (ans2 - ans1) / ans2);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/mrclr/p/11061948.html