感觉题解写的就是坨不知道什么东西。。
看得这个题解。
#include<bits/stdc++.h> #define LL long long #define LD long double #define fi first #define se second #define mk make_pair #define PLL pair<LL, LL> #define PLI pair<LL, int> #define PII pair<int, int> #define SZ(x) ((int)x.size()) #define ull unsigned long long using namespace std; const int N = 1000 + 7; const int inf = 0x3f3f3f3f; const LL INF = 0x3f3f3f3f3f3f3f3f; const int mod = 1e9 + 7; const double eps = 1e-8; const double PI = acos(-1); template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;} template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < 0) a += mod;} template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;} template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;} int n, m, num1, num2; int cnt[N]; int need[N]; bool have[N]; vector<PII> vc; double dp[N][N]; double sum[N]; double calc(int n, int m) { return exp(sum[m] + sum[n - m] - sum[n]); } int main() { for(int i = 1; i < N; i++) sum[i] = sum[i - 1] + log(i); scanf("%d%d", &n, &m); for(int i = 1; i <= m; i++) { scanf("%d", &cnt[i]); for(int j = 1; j <= cnt[i]; j++) { int x; scanf("%d", &x); vc.push_back(mk(x, i)); } } sort(vc.rbegin(), vc.rend()); int val = vc[n - 1].fi; num1 = n; num2 = 0; if(SZ(vc) == n || vc[n].fi != val) val = -1; if(~val) { for(auto& t : vc) { if(t.fi > val) need[t.se]++, num1--; else if(t.fi == val) have[t.se] = true, num2++; else break; } } else { for(int i = 0; i < n; i++) need[vc[i].se]++; num1 = 0, num2 = 0; } dp[0][0] = 1.0; for(int i = 1; i <= m; i++) { for(int j = 0; j <= i && j <= num1; j++) { if(!have[i]) { dp[i][j] = dp[i - 1][j] * calc(cnt[i], need[i]); } else { if(j) dp[i][j] = dp[i - 1][j] * calc(cnt[i], need[i]) + dp[i - 1][j - 1] * calc(cnt[i], need[i] + 1); else dp[i][j] = dp[i - 1][j] * calc(cnt[i], need[i]); } } } printf("%.15f ", dp[m][num1] * calc(num2, num1)); return 0; } /* */