Game

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 229    Accepted Submission(s): 85

Problem Description
There are N people playing a game. The rules of the game are described as follows:

Initially, there are N people numbered from 1-N. And they are arranged in a queue by the order from 1-N. Each round, only 4 people get into the game and each people has equally probability to win the game. The winner can continue to games, the loser will go to the end of the queue according to the order before this round (if someone was the winner before this round, we can consider he was the head of the queue). 

The first round of game, the first four people start to play the game. If someone continuously wins the game M times, he will become the final winner.

Now I want to know the probability for the K-th people to become the final winner.

 
Input
The first line of input contains T, the number of test cases.

Flowing T line, each line contains 3 integer N, M, K.(4<=N<=10, M<=10,K<=N)

 
Output
Each output should occupy one line. Each line should start with "Case #i : ", followed by the answer round to six decimal places.
 
Sample Input
3
4 1 1
5 1 5
5 2 1
 
Sample Output
Case #1: 0.250000
Case #2: 0.000000
Case #3: 0.217626
 
Author
BJTU
 
Source
 
Recommend
zhoujiaqi2010
 


题目大意:
给出n个人每次4人进行比赛其他人等待,胜者继续,负者排到最后,连续或得m次胜利的人成为最终的赢家,求第k个人最终获得胜利的概率是多少?对于这题,我们首先确立一个这样的模型: x1先赢了i局,正在于x2,x3,x4赌斗,后面依次有x5,x6,……,xn等待。用P[i][j]表示x1先赢了i局的情况下,当前的xj获胜的概率。


脑袋不灵光了,坑了一晚上才坑出来。。
下面的一段文字转自茂茂:可以参见大牛的博客

因为要考虑连续赢的情况并且每次动作只和第一个人有关,设第一维表示第一个人连续赢的次数。第二维表示第j个人此次赢的概率。

dp[i][j]表示第一个人已经赢了i次,当前第j个人能赢的概率。

最终也就是要求dp[0][k].表示第一个人一次都没赢时第k个人赢的概率。

当j=1时,dp[i][j]=1/4*dp[i+1][j]+3/4*dp[1][n-2]    //该人要么赢,要么输,输的话,后面有两个人排在他后面,所以他在n-2的位置。

当j=2时,dp[i][j]=1/4*dp[i+1][n-2]+1/4*dp[1][j-1]+2/4*dp[1][n-1]

当j=3时,dp[i][j]=1/4*dp[i+1][n-1]+1/4*dp[1][n-1]+1/4*dp[1][1]+1/4*dp[1][n]

当j=4时,dp[i][j]=1/4*dp[i+1][n]+2/4*dp[1][n]+1/4*dp[1][1];

当j>4时,dp[i][j]=1/4*dp[i+1][j-3]+3/4*dp[1][j-3]

注意

1、i<m,

2、dp[m][1]=1,表示第一个人已经赢了m次,结束。

此转移方程,前后都有,不能直接通过递推或迭代求出,所以选用高斯消元求解。

一共有m*n个未知数,所以可以求一个n*m元的一次方程。

实际上dp[0][1]即为x[1],dp[0][2]即为x[2]。。。依次类推。

         题目地址:Game

AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
using namespace std; #define maxn 102
#define eps 1e-10
double g[maxn][maxn];
double x[maxn];
int n,m,k; void add(int cnt,int i,int j,double val)
{
int t=i*n+j;
if(i==m)
{
if(j==1) //p[m][1]=1;结束
g[cnt][m*n+1]+=-1.0*val; //方程的右边
return;
}
g[cnt][t]+=val;
} void gauss(int n,int m)
{
int row,col,i,j,k;
for(row=1,col=1;row<n,col<m;row++,col++)
{
k=row;
for(i=row+1;i<=n;i++) //列主元
if(fabs(g[i][col])>fabs(g[k][col]))
k=i;
if(k!=row) //行交换
{
for(i=col; i<=m; i++)
swap(g[k][i],g[row][i]);
} for(i=row+1; i<=n; i++) //主元不是0把下面的行第一个值全部变为0
{
if(fabs(g[i][col])<eps)
continue;
double t=g[i][col]/g[row][col];
g[i][col]=0.0;
for(j=col+1;j<=m;j++)
g[i][j]-=t*g[row][j];
}
} for(i=n;i>=1;i--) //回代求解
{
x[i]=g[i][m];
for(j=i+1;j<=n;j++)
x[i]-=x[j]*g[i][j];
x[i]/=g[i][i];
}
} int main()
{
int i,j,cs,nn=0;
scanf("%d",&cs);
while(cs--){
scanf("%d%d%d",&n,&m,&k);
memset(g,0,sizeof(g));
int cnt=0;
for(i=0;i<m;i++) //i==m的时候只能在右边出现
for(j=1;j<=n;j++)
{
cnt++;
add(cnt,i,j,1.0);
if(j==1)
{
add(cnt,i+1,j,-0.25);
add(cnt,1,n-2,-0.75);
}
else if(j==2)
{
add(cnt,i+1,n-2,-0.25);
add(cnt,1,1,-0.25);
add(cnt,1,n-1,-0.5);
}
else if(j==3)
{
add(cnt,i+1,n-1,-0.25);
add(cnt,1,1,-0.25);
add(cnt,1,n-1,-0.25);
add(cnt,1,n,-0.25);
}
else if(j==4)
{
add(cnt,i+1,n,-0.25);
add(cnt,1,n,-0.5);
add(cnt,1,1,-0.25);
}
else
{
add(cnt,i+1,j-3,-0.25);
add(cnt,1,j-3,-0.75);
}
}
gauss(cnt,cnt+1);
printf("Case #%d: %.6lf\n",++nn,x[k]);
}
return 0;
}

HDU 4326Game(比较难理解的概率dp)的更多相关文章

  1. HDU 4405:Aeroplane chess(概率DP入门)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=4405 Aeroplane chess Problem Description   Hzz loves ...

  2. 2015多校第7场 HDU 5378 Leader in Tree Land 概率DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5378 题意:一棵n个节点的树.对其节点进行标号(1~n).求恰好存在k个节点的标号是其节点所在子树的最 ...

  3. HDU 4336 Card Collector(动态规划-概率DP)

    Card Collector Problem Description In your childhood, do you crazy for collecting the beautiful card ...

  4. [HDU 3689]Infinite monkey theorem (KMP+概率DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3689 黄老师说得对,题目只有做wa了才会有收获,才会有提高. 题意:一个猴子敲键盘,键盘上有n个键,猴 ...

  5. [HDU 4336] Card Collector (状态压缩概率dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4336 题目大意:有n种卡片,需要吃零食收集,打开零食,出现第i种卡片的概率是p[i],也有可能不出现卡 ...

  6. HDU 1203 【01背包/小数/概率DP】

    I NEED A OFFER! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...

  7. HDU 2955 【01背包/小数/概率DP】

    Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  8. HDU 5607 graph(矩阵优化+概率DP)

    该题非常easy想到求概率的转移方程:用d[i][j]表示第i步,走到j点的概率. 可是该题的k高达1e9.所以依照套路.要用矩阵相乘来优化. 第一次写矩阵相乘. 大概的意思就是利用矩阵实现递推. 而 ...

  9. hdu 4586 Play the Dice(概率dp)

    Problem Description There is a dice with n sides, which are numbered from 1,2,...,n and have the equ ...

随机推荐

  1. Confluent

    Confluent介绍(一)   最开始接触confluent是通过这篇博客,How to Build a Scalable ETL Pipeline with Kafka Connect,对于做大数 ...

  2. NET Core 的 Views

    NET Core 十种方式扩展你的 Views 原文地址:http://asp.net-hacker.rocks/2016/02/18/extending-razor-views.html作者:Jür ...

  3. Linux bug 14258279: scheduling clock overflows in 208 days

    早上同事反映数据库不能用.无法正常登录主机.多次尝试后终于登上主机,检查系统日志发现下述错误: BUG: soft lockup - CPU#5 stuck for 17163091988s! 貌似是 ...

  4. N沟道增强型MOS管双向低频开关电路

    MOS-N 场效应管 双向电平转换电路 -- 适用于低频信号电平转换的简单应用 如上图所示,是 MOS-N 场效应管 双向电平转换电路.双向传输原理: 为了方便讲述,定义 3.3V 为 A 端,5.0 ...

  5. UltraISO制作U盘系统安装盘(图文教程)

    虽然现在的系统盘做的越来越傻瓜化,安装方法也非常多,但是仍然时常有朋友询问怎么安装系统,特别是没有刻录机或不想刻盘,又不懂硬盘安装的朋友,这里特别介绍一种用U盘来安装系统的方法,非量产,量产因U盘芯片 ...

  6. 杂记之activity之间的跳转

    代码结构图 manifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xml ...

  7. <学习>.NET的反射基础

    关键词 Assembly 使用Assembly定义和加载程序集,加载在程序集清单中列出模块,以及从此程序集中查找类型并创建该类型的实例. Module 通过它可以获取包含模块的程序集以及模块中的类等, ...

  8. JS学习笔记(二)运算符和流程控制语句

    js中的运算符和流程控制,循环,判断语句都和C#基本一致,但又有其独特的运算符. typeof运算符 获得数据的数据类型,如number,string等.语法: string typeof(变量); ...

  9. ASPxGridView-为每行添加序号

    添加一个新的非绑定列,使用CustomColumnDisplayText事件来分配序号给该列 <dx:GridViewDataTextColumn Caption="序号" ...

  10. java 设计模式初探之适配器模式

    1. 概述 将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以在一起工作. 2. 解决的问题 即Adapter模式使得原本由于接口不兼容而不 ...