# HDU4609 3-idiots(生成函数)

## Sol

#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);
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;
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;
}

0 条评论

• ### HDU5036 Explosion(期望 bitset)

首先根据期望的线性性，可以转化为求每个点的期望打开次数，又因为每个点最多会被打开一次，只要算每个点被打开的概率就行了

• ### 洛谷P2881 [USACO07MAR]排名的牛Ranking the Cows(bitset Floyd)

显然如果题目什么都不说的话需要$$\frac{n * (n - 1)}{2}$$个相对关系

• ### 洛谷P4383 [八省联考2018]林克卡特树lct(DP凸优化/wqs二分)

小L 最近沉迷于塞尔达传说：荒野之息（The Legend of Zelda: Breath of The Wild）无法自拔，他尤其喜欢游戏中的迷你挑战。

• ### HDU5036 Explosion(期望 bitset)

首先根据期望的线性性，可以转化为求每个点的期望打开次数，又因为每个点最多会被打开一次，只要算每个点被打开的概率就行了

• ### 洛谷P4383 [八省联考2018]林克卡特树lct(DP凸优化/wqs二分)

小L 最近沉迷于塞尔达传说：荒野之息（The Legend of Zelda: Breath of The Wild）无法自拔，他尤其喜欢游戏中的迷你挑战。