首页 > 其他分享 >Codeforces Round 105 (Div. 2) - D. Bag of mice DP 或 记忆化搜索 求概率

Codeforces Round 105 (Div. 2) - D. Bag of mice DP 或 记忆化搜索 求概率

时间:2023-07-29 22:34:17浏览次数:54  
标签:typedef int Codeforces long Bag maxm Round dp define

D. Bag of mice

题意

待补充~

思路

可利用 DP 或者记忆化搜索求解本问题,实际上这两个方法等价。
当 \(w = 0\) 时必输
当 $w \ne 0 $ 但 $b = 0 $ 时必赢
剩下的情况,先考虑一个问题:赢的局面是怎么构成的?

代码

  • 记忆化搜索
//>>>Qiansui
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define mem(x,y) memset(x, y, sizeof(x))
#define debug(x) cout << #x << " = " << x << '\n'
#define debug2(x,y) cout << #x << " = " << x << " " << #y << " = "<< y << '\n'
//#define int long long

using namespace std;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pull;
typedef pair<double, double> pdd;
/*
上一发DP,这一发记忆化搜索
*/
const int maxm = 1e3 + 5, inf = 0x3f3f3f3f, mod = 998244353;
double dp[maxm][maxm];

double dfs(int n, int m){
	if(n <= 0 || m < 0) return 0;       //attention!
	if(m == 0) return 1;
	if(dp[n][m] != 0) return dp[n][m];
	dp[n][m] = 1.0 * n / (n + m);
	if(n + m >= 3){
		dp[n][m] += dfs(n - 1, m - 2) * m * (m - 1) * n / (n + m) / (n + m - 1) / (n + m - 2)
			+   dfs(n, m - 3) * m * (m - 1) * (m - 2) / (n + m) / (n + m - 1) / (n + m - 2);
	}
	return dp[n][m];
}

void solve(){
	int n, m;
	cin >> n >> m;
	cout << fixed << setprecision(9) << dfs(n, m) << '\n';
	return ;
}

signed main(){
	ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
	int _ = 1;
	// cin >> _;
	while(_ --){
		solve();
	}
	return 0;
}
  • DP
//>>>Qiansui
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define mem(x,y) memset(x, y, sizeof(x))
#define debug(x) cout << #x << " = " << x << '\n'
#define debug2(x,y) cout << #x << " = " << x << " " << #y << " = "<< y << '\n'
//#define int long long

using namespace std;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pull;
typedef pair<double, double> pdd;
/*

*/
const int maxm = 1e3 + 5, inf = 0x3f3f3f3f, mod = 998244353;
double dp[maxm][maxm];

void solve(){
	for(int i = 0; i < maxm; ++ i){
		dp[i][0] = 1;
		dp[0][i] = 0;
	}
	int n, m;
	cin >> n >> m;
	for(int i = 1; i <= n; ++ i){
		for(int j = 1; j <= m; ++ j){
			dp[i][j] = 1.0 * i / (i + j);
			if(i + j >= 3){
				if(j >= 2)
					dp[i][j] += dp[i - 1][j - 2] * j * (j - 1) * i / (i + j) / (i + j - 1) / (i + j - 2);
				if(j >= 3)
					dp[i][j] += dp[i][j - 3] * j * (j - 1) * (j - 2) / (i + j) / (i + j - 1) / (i + j - 2);
			}
		}
	}
	cout << fixed << setprecision(9) << dp[n][m] << '\n';
	return ;
}

signed main(){
	ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
	int _ = 1;
	// cin >> _;
	while(_ --){
		solve();
	}
	return 0;
}

标签:typedef,int,Codeforces,long,Bag,maxm,Round,dp,define
From: https://www.cnblogs.com/Qiansui/p/17590685.html

相关文章

  • Educational Codeforces Round 152 (Rated for Div. 2)
    传送阵T1MorningSandwich题目大意\(t\)个测试,每个测试给三个正整数\(b,c,h\),分别表示面包和两种馅料的个数,每两个面包之间必须放一个馅料,问最多能摆几层?思路我们发现\(c,h\)所表示的两种馅料其实相差不大,所以我们可以分两种情况\(a>c+h\)因为开头总是面包,所以要+......
  • Codeforces Round 888 (Div. 3)
    CodeforcesRound888(Div.3)T1​ 思路:直接模拟。T2​思路:首先记录原始数组的奇偶性,然后将奇数、偶数分为不同两组进行排序,然后再根据原数组的奇偶性按顺序填入奇数偶数,最后判断整个数组是否非递减。T3思路:我们已知开始在\(a_1\),最后在\(a_n\)。那么当\(a_1\ne......
  • Educational Codeforces Round 152 (Rated for Div. 2) 题解
    \(6\)题做出来\(3\)题,这一次的D题没能复刻上一次Round888Div.3最后几分钟AC的奇迹A.MorningSandwich大水题,5min时间4min都在翻译题面直接拿\(b\)和\(c+h\)进行比较分类讨论即可单次操作时间复杂度\(O(1)\)B.Monsters首先有一个特别显然、复杂度特别高的......
  • @Around简单使用示例——SpringAOP增强处理
    @Around简单使用示例——SpringAOP增强处理@Around的作用既可以在目标方法之前织入增强动作,也可以在执行目标方法之后织入增强动作;可以决定目标方法在什么时候执行,如何执行,甚至可以完全阻止目标目标方法的执行;可以改变执行目标方法的参数值,也可以改变执行目标方法之后的返回......
  • Educational Round 147
    前言:非常好场次编号,爱来自小粉兔。唉,GF。A.shaber模拟。B.shaber。找最大的满足\(a_{l\simr}\)和\(a'_{l\simr}\)有不同,且\(a'_{l\simr}\)递增的\(\langlel,r\rangle\)即可。\(\mathcalO(n)\)。C.shaber。枚举字符\(c\),非\(c\)最大连续段长是\(k\)的......
  • Educational Codeforces Round 152 (Rated for Div. 2)记录
    A.MorningSandwich#include<cstdio>#include<algorithm>#include<cmath>#include<vector>#include<string.h>#include<set>#include<string>#include<map>#include<iostream>#include<queue......
  • SMU Summer 2023 Contest Round 7
    SMUSummer2023ContestRound7A.TwoRivalStudents答案不能大于\(n-1\);如果竞争对手之间的当前距离小于\(n-1\),我们总是可以将这个距离增加一个交换数;即答案等于\(min(n-1,|a-b|+x)\)。#include<bits/stdc++.h>#defineintlonglongusingnamespac......
  • Educational Codeforces Round 76 (Rated for Div. 2)
    EducationalCodeforcesRound76(RatedforDiv.2)A-TwoRivalStudents思路:最多可加x个距离,且最后的距离不能超过n-1#include<bits/stdc++.h>usingnamespacestd;#defineintlonglong//#defineint__int128typedefpair<int,int>PII;typedefpair<string,......
  • Educational Codeforces Round 152 A~D
    A#include<bits/stdc++.h>#defineendl'\n'#defineiosios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)usingnamespacestd;typedefpair<int,int>PII;constintN=2e5+10;constintMOD=1e9+7;intT;vo......
  • Codeforces Round 888 (Div. 3) 题解
    考场上\(7\)题做出来\(4\)题,最后几分钟才把D题调出来,但还是吃了不少罚时A.EscalatorConversations\(O(n)\)枚举即可,对于每个人计算需要的间隔台阶数是否在\((0,m)\)以内以及相差高度是否是\(k\)的倍数B.ParitySort显然,偶数和奇数是不可能产生交换操作的,而偶数......