GYM 101933E(记忆化搜索)

用每个人的血量作为状态去搜索T飞,考虑题解中更好的搜索方式:每种血量有多少个人作为状态。这样会减去很多重复的状态,因为只要乘一下就得到了所有相同情况的和。
虽然我不会算,但是直观感受起来复杂度比较优秀。

#include <cstdio>
#include <unordered_map>
using namespace std;

typedef double db;
typedef long long ll;

int n, m, d, mod = 1e6;
int cnt[2][10];
unordered_map<ll, db> mp;

void read(int n, int k) {
    for (int a, i = 1; i <= n; i++) {
        scanf("%d", &a);
        cnt[k][a]++;
    }
}

ll tran(ll res = 0) {
    for (int i = 0; i < 2; i++)
        for (int j = 1; j <= 6; j++)
            res = res * 10 + cnt[i][j];
    return res;
}

db dfs(ll t, int depth) {
    if (mp.count(t))    return mp[t];
    if (t % mod == 0)   return mp[t] = 1;
    if (depth == d) return mp[t] = 0;

    db res = 0; int tmp = 0;
    for (int i = 0; i < 2; i++)
        for (int j = 1; j <= 6; j++)
            tmp += cnt[i][j];
    for (int i = 0; i < 2; i++) {
        for (int j = 1; j <= 6; j++) {
            if (cnt[i][j]) {
                cnt[i][j]--;
                cnt[i][j - 1]++;
                res += dfs(tran(), depth + 1) * (cnt[i][j] + 1) / tmp;
                cnt[i][j]++;
                cnt[i][j - 1]--;
            }
        }
    }
    return mp[t] = res;
}

int main() {
    scanf("%d %d %d", &n, &m, &d);
    read(n, 0), read(m, 1);
    return !printf("%.8lf\n", dfs(tran(), 0));
}
上一篇:记忆化搜索 E - Loppinha, the boy who likes sopinha Gym - 101875E


下一篇:Gym 101128F Landscaping