daimayuan675 (期望dp)

image

  • 盘子的顺序是没有影响的,所以记下3种盘子的数量
  • 设fijk 为三种盘子的个数为ijk时候的期望。
  • 转移顺序比较乱,使用记忆化
    image
#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false) ,cin.tie(0), cout.tie(0);  
//#pragma GCC optimize(3,"Ofast","inline")
#define ll long long
const int N = 3e2 + 5;
const double PI = acos(-1.0);
int n, cot[4];
bool vis[N][N][N];
double f[N][N][N];
double dfs(int i, int j, int k) {
    if(i + j + k == 0) return 0;
    if(i < 0 || j < 0 || k < 0 ) return 0;
    if(vis[i][j][k]) return f[i][j][k];
    vis[i][j][k] = 1;
    double p0 = 1.0 * (n - (i + j + k)) / n;
    double p1 = 1.0 * i / n;
    double p2 = 1.0 * j / n;
    double p3 = 1.0 * k / n;
    return f[i][j][k] = (1.0 + p1 * dfs(i - 1, j, k) + p2 * dfs(i + 1, j - 1, k) + p3 * dfs(i, j + 1, k - 1)) / (1 - p0);
}
int main () {
    IOS; cin >> n; int x;
    for ( int i = 1; i <= n; ++ i ) cin >> x, ++ cot[x];
    printf("%.13lf\n", dfs(cot[1], cot[2], cot[3]));
    return 0;
}
posted @ 2022-04-18 16:22  qingyanng  阅读(22)  评论(0编辑  收藏  举报