题目链接
https://atcoder.jp/contests/dp/tasks/dp_j
AC代码
点击查看代码
#include<bits/stdc++.h>
#define rep(i,x,y) for(int i=x; i<=y; ++i)
#define per(i,x,y) for(int i=x; i>=y; --i)
#define pushk push_back
#define popk pop_back
#define mem(a,b) memset(a,b,sizeof a)
#define ll long long
#define emk emplace_back
using namespace std;
const int N = 300+9;
double dp[N][N][N];
int cnt[4];
int a[N];
int main() {
int n;
cin>>n;
rep(i,1,n) cin>>a[i],cnt[a[i]]++;
dp[0][0][0]=0;
rep(k,0,n){
rep(j,0,n){
rep(i,0,n){
int t = i+j+k;
if(i) dp[i][j][k]+=i*1.0/t*dp[i-1][j][k];
//(i,j)用到了(i+1,j-1),所以j要在i前面
if(j) dp[i][j][k]+=j*1.0/t*dp[i+1][j-1][k];
// (j,k)用到了(j+1,k-1),所以k要在j的外层
if(k) dp[i][j][k]+=k*1.0/t*dp[i][j+1][k-1];
if(t) dp[i][j][k]+=n*1.0/t;
//cout<<dp[i][j][k]<<endl;
}
}
}
printf("%.10f\n",dp[cnt[1]][cnt[2]][cnt[3]]);
return 0;
}