SPOJ 4060 A game with probability
博弈论+dp+概率
提交链接-
题意不是很好懂
Ai 表示剩 i 个石头、 A 先手的获胜概率。
Bi 表示剩 i 个石头、 B先手的获胜概率。
如果想选,对于 Ai:
有 p 的概率进入 Bi−1 ;有 1−p 的概率进入 Bi
所以 fi=p∗Bi−1+(1−p)∗Bi
如果想选,对于 Bi:
有 q 的概率进入 Ai−1 ;有 1−q 的概率进入 Ai
所以 gi=q∗Ai−1+(1−q)∗Ai
如果不想选, 把 p 变成 1 - p, q 变成 1 - q 即可
为了满足递推关系,我们把 Bi 带入到 Ai 的式子中,
整理得:
Ai=(p∗Bi-1 +(1−p)∗q∗Ai-1 )/(1-(1−p)∗(1−q))
Bi=(q∗Ai-1+(1−q)∗p∗Bi−1)/(1−(1−p)∗(1−q))
然后剩 i 个石头时A的想不想选的意愿与 Ai−1、Ai−1 的大小关系有关。
Ai−1>Bi−1 都不想选。
因为 A 如果选了,就到了 Bi - 1,获胜概率就小了
如果 B 选了, 就到了 Ai - 1, A的获胜概率就大了,B 的获胜概率就小了
Bi−1<Bi−1 都想选。
同理
然后对于不想选的情况,那么 p=1−p,q=1−q 就行了。
然而这样就没法用矩阵乘法了。。。
就需要黑科技,,当n很大时,其实概率已经基本不动了,,让n=min(n,1000)就好了.
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int init() {
int rv = 0, fh = 1;
char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') fh = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
rv = (rv<<1) + (rv<<3) + c - '0';
c = getchar();
}
return fh * rv;
}
int T, n;
double a[1005], b[1005], p, q;
int main() {
T = init();
while(T--) {
n = init();
n = min(n, 1000);
scanf("%lf%lf", &p, &q);
a[0] = 0.0; b[0] = 1.0;
for(int i = 1 ; i <= n ; i++) {
if(a[i - 1] > b[i - 1]) {p = 1 - p; q = 1 - q;}
a[i] = p / (1 - (1 - p) * (1 - q)) * b[i - 1] +
(1 - p) * q / (1 - (1 - p) * (1 - q)) * a[i - 1];
b[i] = q / (1 - (1 - p) * (1 - q)) * a[i - 1] +
(1 - q) * p / (1 - (1 - p) * (1 - q)) * b[i - 1];
if(a[i - 1] > b[i - 1]) {p = 1 - p; q = 1 - q;}
}
printf("%.6lf\n",a[n]);
}
return 0;
}
SPOJ 4060 A game with probability的更多相关文章
- SPOJ 375. Query on a tree (树链剖分)
Query on a tree Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on SPOJ. Ori ...
- Project Euler 100 : Arranged probability 安排概率
Arranged probability If a box contains twenty-one coloured discs, composed of fifteen blue discs and ...
- BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 5217 Solved: 1233 ...
- SPOJ DQUERY D-query(主席树)
题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...
- SPOJ GSS3 Can you answer these queries III[线段树]
SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...
- 【BZOJ2318】Spoj4060 game with probability Problem 概率
[BZOJ2318]Spoj4060 game with probability Problem Description Alice和Bob在玩一个游戏.有n个石子在这里,Alice和Bob轮流投掷硬 ...
- 【填坑向】spoj COT/bzoj2588 Count on a tree
这题是学主席树的时候就想写的,,, 但是当时没写(懒) 现在来填坑 = =日常调半天lca(考虑以后背板) 主席树还是蛮好写的,但是代码出现重复,不太好,导致调试的时候心里没底(虽然事实证明主席树部分 ...
- SPOJ bsubstr
题目大意:给你一个长度为n的字符串,求出所有不同长度的字符串出现的最大次数. n<=250000 如:abaaa 输出: 4 2 1 1 1 spoj上的时限卡的太严,必须使用O(N)的算法那才 ...
- Fuzzy Probability Theory---(3)Discrete Random Variables
We start with the fuzzy binomial. Then we discuss the fuzzy Poisson probability mass function. Fuzzy ...
随机推荐
- Hyperledger Fabric on SAP Cloud Platform
今天的文章来自Wen Aviva, 坐Jerry面对面的程序媛. Jerry在之前的公众号文章<在SAP UI中使用纯JavaScript显示产品主数据的3D模型视图>已经介绍过Aviva ...
- python简单爬虫爬取百度百科python词条网页
目标分析:目标:百度百科python词条相关词条网页 - 标题和简介 入口页:https://baike.baidu.com/item/Python/407313 URL格式: - 词条页面URL:/ ...
- 单源最短路Dijstra
#include<iostream> #include<cstring> #define INF 0x3f3f3f3f using namespace std; ][],d[] ...
- Sql Server 自动备份
1)启动代理服务 服务->Sql server 代理启动 2)设置维护计划 维护计划->设置维护计划向导->修改名称及说明 3)更改计划 4)选择维护任务 5)顺序调整不做解释 6) ...
- Python3 try-except、raise和assert解析
Python3 try-except.raise和assert解析 一.说明 关于异常捕获try-except:在学java的时候就被教育异常捕获也是java相对c的一大优点,几年下来多少也写了些代码 ...
- java面试宝典第二弹
arraylist和linklist的区别,hashmap和hashset的区别,常用的集合有哪些 一.基础内容 容器就是一种装其他各种对象的器皿.java.util包 容器:Set, List, M ...
- C++高精度乘法
#include <cstdio> #include <iostream> #include <algorithm> void highPrecision (int ...
- 【搜索 技巧】Letter gaps
需要一定技巧的搜索题 题目描述 记得做过这样一道题 Ponder This Challenge: In the string CABACB, each letter appears exactly t ...
- Springboot(一)-IDEA搭建springboot项目(demo)
jdk版本:1.8.0_162 1.打开IDEA-file-new-project-Spring Initializer,JDK和URL选默认,next (这一步如果是不能联网的话,可以选择直接创建m ...
- 【mysql】linux, mac mysql数据库root 密码忘记修改
首先关闭正在运行的mysqld进程 执行mysqld_safe --skips-grant-tables & 双击enter 键进入命令行模式 执行 mysql linux 系统执行:upda ...