CodeForces - 540D Bad Luck Island (概率dp)

本文介绍了一种使用概率动态规划的方法来计算三种物种(石头、剪刀、布)在相互作用过程中的长期存活概率。通过定义状态和转移方程,解决了给定初始数量时,每种物种最终成为唯一幸存者的概率。

D. Bad Luck Island
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The Bad Luck Island is inhabited by three kinds of species: r rocks, s scissors and p papers. At some moments of time two random individuals meet (all pairs of individuals can meet equiprobably), and if they belong to different species, then one individual kills the other one: a rock kills scissors, scissors kill paper, and paper kills a rock. Your task is to determine for each species what is the probability that this species will be the only one to inhabit this island after a long enough period of time.

Input

The single line contains three integers rs and p (1 ≤ r, s, p ≤ 100) — the original number of individuals in the species of rock, scissors and paper, respectively.

Output

Print three space-separated real numbers: the probabilities, at which the rocks, the scissors and the paper will be the only surviving species, respectively. The answer will be considered correct if the relative or absolute error of each number doesn't exceed 10 - 9.

Examples
input
2 2 2
output
0.333333333333 0.333333333333 0.333333333333
input
2 1 2
output
0.150000000000 0.300000000000 0.550000000000
input
1 1 3
output
0.057142857143 0.657142857143 0.285714285714

题意:有r个石头,s个剪刀,p个布,求在足够长一段时间后,石头、剪刀、布分别存活下来的概率。

思路:概率dp,定义dp[i][j][k],表示有i个石头,j个剪刀,k个布情况下的概率。

状态转移方程:dp[i-1][j][k]=dp[i-1][j][k]+dp[i][j][k]*i*k/(i*j+i*k+j*k); 表示布包石头的情况,石头-1。

ansr表示石头最终存活下来的概率。对所有dp[i][j][0]求和即可,即布已为0,石头一定存活。


代码:

#include<bits/stdc++.h>
using namespace std;

const int N=105;
double dp[N][N][N];

int main(){
	int r,s,p;
	double ansr=0,anss=0,ansp=0;
	scanf("%d%d%d",&r,&s,&p);
	memset(dp,0,sizeof(dp));
	dp[r][s][p]=1;
	for(int i=r;i>=1;i--){
		for(int j=s;j>=1;j--){
			for(int k=p;k>=1;k--){
				int temp=i*j+i*k+j*k;
				dp[i-1][j][k]+=dp[i][j][k]*i*k/temp;
				dp[i][j-1][k]+=dp[i][j][k]*j*i/temp;
				dp[i][j][k-1]+=dp[i][j][k]*k*j/temp;
			}
		}
	} 
	for(int i=r;i>=0;i--){
		for(int j=s;j>=0;j--){
			ansr+=dp[i][j][0];
		}
	}
	printf("%.12f ",ansr);
	
	for(int i=s;i>=0;i--){
		for(int j=p;j>=0;j--){
			anss+=dp[0][i][j];
		}
	}
	printf("%.12f ",anss);
	
	for(int i=r;i>=0;i--){
		for(int j=p;j>=0;j--){
			ansp+=dp[i][0][j];
		}
	}
	printf("%.12f\n",ansp);
	
	return 0;
} 



内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值