分析
直接把所有可能的五张牌的状态搜出来,至多 种,然后排序;然后从小到大枚举所有状态,若当前状态可以成为我方手牌,就容斥算一下答案。容斥的时候需要查询包含特定子集的状态已经加入了多少,由于这里子集大小 ,可以每次现场算一下这个子集在所有 种子集中的排名(可以钦定元素少的更小),开个桶存一下即可。判定牌型不需要各种奇怪的一坨,只需要记录牌型是否顺子、是否同花、点数众数出现次数以及不同点数个数即可简单判断。
代码
#include <iostream>
#include <algorithm>
#include <array>
#include <map>
#define int long long
using namespace std;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int n, XX, YY, N = 5;
array<int, 4> a[3000005];
int ncnt, A[20], B[20];
int ap[20];
void dfs(int x, int lst, int S) {
if (x == N + 1) {
if ((S & XX) != XX && (S & YY) != YY)
return;
for (int i = 1; i < x; i++) B[i] = A[i];
// aps of mode, dif count, is straight, is flush
int md = 0, df = 0, st = 1, fl = 1, vp = 0, vd = 0;
int sp = 1, mxp = 0;
for (int i = 0; i < 15; i++) ap[i] = 0;
for (int i = 1; i < x; i++) {
md = max(md, ++ap[A[i] / 4]);
df += (ap[A[i] / 4] == 1);
st &= (A[i] / 4 - A[1] / 4 == i - 1);
fl &= (A[i] % 4 == A[1] % 4);
sp &= ((i == 1 && A[i] / 4 == 0) || (i != 1 && N - i == 12 - A[i] / 4));
mxp = max(mxp, A[i] / 4);
}
if (st |= sp) {
sort(B + 1, B + N + 1, [&](int a, int b) { return a + ((a < 4 && sp) * 100) < b + ((b < 4 && sp) * 100); });
vp = (sp ? 14 : mxp);
} else {
sort(B + 1, B + N + 1, [](int a, int b) { return ap[a/4]==ap[b/4]?(a+(a<4)*100<b+(b<4)*100):(ap[a/4]<ap[b/4]); });
for (int i = N; i; i--) vp = vp * 15 + B[i] / 4 + (B[i] < 4) * 13;
}
a[++ncnt] = (array<int, 4>) { max({ (st & fl) * 9ll, (md == 4) * 8ll, (md == 3 && df == 2) * 7ll, fl * 6ll, st * 5ll, (md == 3) * 4ll, (md == 2) * 3ll - (df > 3), 1ll }), vp, B[N] % 4, S };
return;
}
for (int i = lst + 1; i < 52; i++) A[x] = i, dfs(x + 1, i, S | (1ll << i));
}
int cnt[3000005];
int mp[3000005];
int C[105][105];
int Acnt;
int f(int S) {
int ret = Acnt = 0, c = __builtin_popcountll(S);
for (int i = 0; i < c; i++) ret += C[52][i];
while (S) A[++Acnt] = __lg(S) + 1, S ^= (1ll << __lg(S));
for (int i = 1; i <= Acnt; i++) ret += (A[i] != 1 ? C[A[i] - 1][Acnt - i + 1] : 0);
return ret + 1;
}
signed main() {
for (int i = C[0][0] = 1; i <= 55; i++) for (int j = C[i][0] = 1; j <= min(i, 6ll); j++) C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
freopen("showhand.in", "r", stdin);
freopen("showhand.out", "w", stdout);
cin >> n;
for (int x, y, i = 1; i <= n; i++) {
cin >> x >> y; y = 5 - y;
XX |= (1ll << ((x - 1) * 4 + y - 1));
}
for (int x, y, i = 1; i < n; i++) {
cin >> x >> y; y = 5 - y;
YY |= (1ll << ((x - 1) * 4 + y - 1));
}
dfs(1, -1, 0);
sort(a + 1, a + ncnt + 1);
int ans = 0;
for (int i = 1; i <= ncnt; i++) {
int s = a[i][3];
if ((s & XX) == XX) {
for (int c, j = s; j; j = (j - 1) & s) c = mp[f(j)], cnt[i] += (__builtin_popcountll(j) & 1 ? -c : c);
cnt[i] += mp[1];
ans += cnt[i];
}
if ((s & YY) == YY) {
for (int j = s; j; j = (j - 1) & s) mp[f(j)] += ((s & YY) == YY);
++mp[1];
}
}
int tot = 1, res = 52 - n - n + 1;
for (int i = 1; i <= 10 - n - n + 1; i++) tot *= res, --res;
for (int i = 1; i <= 5 - n; i++) tot /= i;
for (int i = 1; i <= 6 - n; i++) tot /= i;
int g = gcd(ans, tot);
cout << ans / g << "/" << tot / g << "\n";
return 0;
}