欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

HDU4609 3-idiots(生成函数)

程序员文章站 2022-04-28 14:00:51
题意 "链接" Sol 这个题就很休闲了。。 首先这是个数数题,我们要求的是$\frac{\sum{[a_i + a_j a_k]}}{C_n^3}$ 其中$a$按从小到大排序, $i define Pair pair define MP(x, y) make_pair(x, y) define f ......

题意

sol

这个题就很休闲了。。

首先这是个数数题,我们要求的是\(\frac{\sum{[a_i + a_j > a_k]}}{c_n^3}\)

其中\(a\)按从小到大排序, \(i < j < k\)

因为\(a_i \leqslant 10^5\),那么可以直接暴力生成函数卷积。

但是如果直接算合法的方案的话会出现一点问题。我们在算的时候维护了一个后缀和表示乘起来大于等于这个数的方案。我们要求的方案需要满足\(i < j < k\),但是这样计算可能会出现不合法的情况。

那么可以算不合法的方案维护前缀和,这样就不会出现上面的情况了

#include<bits/stdc++.h> 
#define pair pair<int, int>
#define mp(x, y) make_pair(x, y)
#define fi first
#define se second
#define ll long long 
#define ull unsigned long long 
#define fin(x) {freopen(#x".in","r",stdin);}
#define fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int maxn = 1e6 + 10, inf = 1e9 + 1;
const double eps = 1e-9, pi = acos(-1);
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
namespace poly {
    int rev[maxn], gpow[maxn], a[maxn], b[maxn], c[maxn], lim, inv2;
    const int g = 3, mod = 998244353, mod2 = 998244352;
    template <typename a, typename b> inline ll add(a x, b y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
    template <typename a, typename b> inline void add2(a &x, b y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
    template <typename a, typename b> inline ll mul(a x, b y) {return 1ll * x * y % mod;}
    template <typename a, typename b> inline void mul2(a &x, b y) {x = (1ll * x * y % mod + mod) % mod;}
    template <typename a, typename b> inline bool chmax(a &x, b y) {return x < y ? x = y, 1 : 0;}
    template <typename a, typename b> inline bool chmin(a &x, b y) {return x > y ? x = y, 1 : 0;}
    int fp(int a, int p, int p = mod) {
        int base = 1;
        for(; p > 0; p >>= 1, a = 1ll * a * a % p) if(p & 1) base = 1ll * base *  a % p;
        return base;
    }
    int inv(int x) {
        return fp(x, mod - 2);
    }
    int getlen(int x) {
        int lim = 1;
        while(lim < x) lim <<= 1;
        return lim;
    }
    void init(/*int p,*/ int lim) {
        inv2 = fp(2, mod - 2);
        for(int i = 1; i <= lim; i++) gpow[i] = fp(g, (mod - 1) / i);
    }
    void ntt(int *a, int lim, int opt) {
        int len = 0; for(int n = 1; n < lim; n <<= 1) ++len; 
        for(int i = 1; i <= lim; i++) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (len - 1));
        for(int i = 0; i <= lim; i++) if(i < rev[i]) swap(a[i], a[rev[i]]);
        for(int mid = 1; mid < lim; mid <<= 1) {
            int wn = gpow[mid << 1];
            for(int i = 0; i < lim; i += (mid << 1)) {
                for(int j = 0, w = 1; j < mid; j++, w = mul(w, wn)) {
                    int x = a[i + j], y = mul(w, a[i + j + mid]);
                    a[i + j] = add(x, y), a[i + j + mid] = add(x, -y);
                }
            }
        }
        if(opt == -1) {
            reverse(a + 1, a + lim);
            int inv = fp(lim, mod - 2);
            for(int i = 0; i <= lim; i++) mul2(a[i], inv);
        }
    }
    void mul(int *a, int *b, int n, int m) {
        memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b));
        int lim = 1, len = 0; 
        while(lim <= n + m) len++, lim <<= 1;
        for(int i = 0; i <= n; i++) a[i] = a[i]; 
        for(int i = 0; i <= m; i++) b[i] = b[i];
        ntt(a, lim, 1); ntt(b, lim, 1);
        for(int i = 0; i <= lim; i++) b[i] = mul(b[i], a[i]);
        ntt(b, lim, -1);
        for(int i = 0; i <= n + m; i++) b[i] = b[i];
        memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b));
    }
};
using namespace poly; 
int n, a[maxn], b[maxn], c[maxn], mx;
ll sum[maxn];
ll comb(int n) {
    ll ta = n, tb = n - 1, tc = n - 2;
    bool f2 = 1, f3 = 1;
    if(ta % 2 == 0 && f2) ta /= 2, f2 = 0;
    if(tb % 2 == 0 && f2) tb /= 2, f2 = 0;
    if(ta % 3 == 0 && f3) ta /= 3, f3 = 0;
    if(tb % 3 == 0 && f3) tb /= 3, f3 = 0;
    if(tc % 3 == 0 && f3) tc /= 3, f3 = 0;
    return 1ll * ta * tb * tc;
}
void solve() {
    memset(b, 0, sizeof(b)); memset(c, 0, sizeof(c)); mx = 0; sum[0] = 0;
    n = read();
    for(int i = 1; i <= n; i++) a[i] = read(), b[a[i]]++, c[a[i]]++, chmax(mx, a[i]);
    mul(b, c, mx, mx);
    for(int i = 1; i <= n; i++) c[2 * a[i]]--;
    for(int i = 1; i <= mx; i++) c[i] /= 2;
    for(int i = 1; i <= mx; i++) sum[i] = sum[i - 1] + c[i];
    ll ans = 0;
    for(int i = 1; i <= n; i++) ans += sum[a[i]];
    ll tmp = comb(n); ans = tmp - ans;
    printf("%.7lf\n", (double) ans / tmp);
}
signed main() {
//  freopen("a.in", "r", stdin);
    init(4e5);
    for(int t = read(); t--; solve());
    return 0;
}