HDU4609 3-idiots(生成函数)

题意

链接

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;
}
posted @ 2019-03-14 10:41  自为风月马前卒  阅读(419)  评论(0编辑  收藏  举报

Contact with me