1875F - Jellyfish and EVA - CodeForces Solution


dp math probabilities

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
#define int long long
#define db long double
using namespace std;
const int N=5077;
vector<int> a[N];
db g[N][N],f[N];
bool cmp(int x,int y){return f[x]>f[y];}
void solve()
{
	int n,m;
	cin>>n>>m;
	for(int i=1; i<=n; i++) for(int j=1; j<=i; j++) g[i][j]=(db)1/i;
	g[1][1]=1.0;
	g[2][1]=0.5,g[2][2]=0.0;
	g[3][1]=g[3][2]=g[3][3]=1.0/3.0;
	for(int i=4; i<=n; i++)
	{
		g[i][1]=g[i][2]=1.0/i;
		for(int j=3; j<i; j++) 
			g[i][j]=g[i-2][j-2]*(j-2)/i+g[i-2][j-1]*(i-j)/i;
		if(i%2==0) g[i][i]=0.0;
		else g[i][i]=(db)1/i;
	}
	for(int i=1; i<=n; i++) a[i].clear();
	for(int i=1; i<=m; i++)
	{
		int x,y;
		cin>>x>>y;
		a[x].push_back(y);
	}
	f[n]=1;
	for(int i=n-1; i>=1; i--)
	{
		f[i]=0;
		sort(a[i].begin(),a[i].end(),cmp);
		for(int j=0; j<a[i].size(); j++)
			f[i]+=f[a[i][j]]*g[a[i].size()][j+1];
	}
	cout<<f[1]<<"\n";
}
signed main()
{
	ios::sync_with_stdio(false); cin.tie(0),cout.tie(0);
	cout<<fixed<<setprecision(10);
	int T=1;
	cin>>T;
	while(T--)
	{
		solve();
	}
}


Comments

Submit
0 Comments
More Questions

96A - Football
702B - Powers of Two
1036A - Function Height
443A - Anton and Letters
1478B - Nezzar and Lucky Number
228A - Is your horseshoe on the other hoof
122A - Lucky Division
1611C - Polycarp Recovers the Permutation
432A - Choosing Teams
758A - Holiday Of Equality
1650C - Weight of the System of Nested Segments
1097A - Gennady and a Card Game
248A - Cupboards
1641A - Great Sequence
1537A - Arithmetic Array
1370A - Maximum GCD
149A - Business trip
34A - Reconnaissance 2
59A - Word
462B - Appleman and Card Game
1560C - Infinity Table
1605C - Dominant Character
1399A - Remove Smallest
208A - Dubstep
1581A - CQXYM Count Permutations
337A - Puzzles
495A - Digital Counter
796A - Buying A House
67A - Partial Teacher
116A - Tram