Batting Practice LightOJ - 1408
Batting Practice LightOJ - 1408(概率dp)
题意:有无限个球,进球的概率为p,问你连续不进k1个球或者连续进k2个球需要使用的球的个数的期望
思路:
\(定义f[i]表示已经连续不进i个球,还需要连续不进k1-i个球的期望\)
\(g[i]表示已经连续进了i个,还需要连续进k2-i个球的期望\)
显然\(f[k1] = g[k2] = 0\)
\(任意0<=i<k1有f[i] = (1-p) \cdot f[i+1] + p \cdot g[1] + 1\)
\(任意0<=i<k2有g[i] = (1-p) \cdot f[1] + p \cdot g[i+1] + 1\)
不好解的样子,列矩阵高斯消元一发,WA,把eps从1e-6改到1e-15还是过不了,用long double交,时限卡的紧,TLE
搜了一发题解,原来上面那个式子可以直接求通项,依次回代就可以求出f[0]和g[0]了
#include<bits/stdc++.h>
#define LL long long
#define P pair<int,int>
using namespace std;
const double eps = 1e-6;
double a[60][60];
int gauss(int n,int m){
int col,i,mxr,j,row;
for(row=col=0;row<=n&&col<=m;row++,col++){
mxr = row;
for(i=row+1;i<=n;i++)
if(fabs(a[i][col])>fabs(a[mxr][col]))
mxr = i;
if(mxr != row) swap(a[row],a[mxr]);
if(fabs(a[row][col]) < eps){
row--;
continue;
}
for(i=0;i<=n;i++)///消成上三角矩阵
if(i!=row&&fabs(a[i][col])>eps)
for(j=m;j>=col;j--)
a[i][j]-=a[row][j]/a[row][col]*a[i][col];
}
row--;
for(int i = row;i>=0;i--){///回代成对角矩阵
for(int j = i + 1;j <= row;j++){
a[i][m] -= a[j][m] * a[i][j];
}
a[i][m] /= a[i][i];
}
return row;
}
int main()
{
int T;
int cas = 1;
cin>>T;
while(T--)
{
int k1,k2;
double p;
scanf("%lf%d%d",&p,&k1,&k2);
printf("Case %d: ",cas++);
/*
memset(a,0,sizeof(a));
int col = k1 + k2 + 2;
for(int i = 0;i < k1;i++){
a[i][i] = 1;
a[i][i+1] = p - 1;
a[i][k1+2] = -p;
a[i][col] = 1;
}
a[k1][k1] = 1,a[k1][col] = 0;
for(int i = 0;i < k2;i++){
a[k1+1+i][k1+1+i] = 1;
a[k1+1+i][k1+1+i+1] = -p;
a[k1+1+i][1] = p - 1;
a[k1+1+i][col] = 1;
}
a[k1+k2+1][k1+k2+1] = 1,a[k1+k2+1][col] = 0;
int row = gauss(k1+k2+1,k1+k2+2);
printf("%.6f\n",a[0][col]);
*/
if(p < eps) printf("%.6f\n",1.0 * k1);
else if(1 - p < eps) printf("%.6f\n",1.0 * k2);
else{
double q;
q=1-p;
double a1=1-pow(q,k1-1),b1=a1/(1-q);
double a2=1-pow(p,k2-1),b2=a2/(1-p);
double t1=(a1*b2+b1)/(1-a1*a2),f1=a2*t1+b2;
printf("%.6f\n",p*f1+q*t1+1);
}
}
return 0;
}
Batting Practice LightOJ - 1408的更多相关文章
- lightoj 1408 Batting Practice (概率问题,求期望,推公式)
题意:一个人若连续进k1个球或连续不进k2个球,游戏结束,给出这个人不进球的概率p(注意:是不进球!!!),求到游戏结束时这个投球个数的期望. 不进球概率为p,进概率 q=1-p.设 f[i] 表示连 ...
- lightoj 1408 Batting Practice
题意:一个人若连续进k1个球或连续不进k2个球,游戏结束,给出这个人进球的概率p,求到游戏结束时这个投球个数的期望. 进球概率为p,不进概率 q=1-p 设 f[i] 表示连续 i 次不进距离连续k2 ...
- LightOj_1408 Batting Practice
题目链接 题意: 击球训练中, 你击中一个球的概率为p,连续击中k1个球, 或者连续击空k2个球, 则训练结束. 求结束训练所击球次数的期望. 思路: 设f[x]为连续击中x个球, 距离结束训练所需要 ...
- lightoj 1408 概率dp
https://blog.csdn.net/moon_sky1999/article/details/98097470 博主在此,牛逼神犇 #include<bits/stdc++.h> ...
- LightOJ 1341 唯一分解定理
Aladdin and the Flying Carpet Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%lld &a ...
- lightoj 1370 欧拉函数
A - Bi-shoe and Phi-shoe Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & % ...
- lightoj 1074 spfa判断负环
Extended Traffic Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Sub ...
- lightoj.1048.Conquering Keokradong(二分 + 贪心)
Conquering Keokradong Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- LightOJ 1234 Harmonic Number
D - Harmonic Number Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu S ...
随机推荐
- 获取Grid后台动态添加的子项
例:Grid的子项是包含边框的复选框CheckBox //遍历Grid中的子项 foreach (var c in this.grid_box.Children) { Border bd = c as ...
- 洛谷P3611 [USACO17JAN]Cow Dance Show奶牛舞蹈
题目描述 After several months of rehearsal, the cows are just about ready to put on their annual dance p ...
- http2.2配置
http: 超文本传输协议,工作在应用层 CentOS 6程序环境:httpd-2.2 配置文件: /etc/httpd/conf/httpd.conf /etc/httpd/conf.d/*.con ...
- GUI测试问题汇总
1.ajax实现的页面元素定位问题 最近在做项目的时候遇到一个问题,通过xpath定位到元素后做一个循环操作,第一循环可以正常执行,第二次循环后就报错,错误信息:Message: The elemen ...
- js | javascript实现浏览器窗口大小被改变时触发事件的方法
转载 当浏览器的窗口大小被改变时触发的事件window.onresize 为事件指定代码: 代码如下: window.onresize = function(){ } 例如: 浏览器可见区域信息: 代 ...
- C#基础-面向对象-封装
封装 命名空间 using System; using System.Collections.Generic; using System.Text; namespace ConsoleApp6 { c ...
- fastadmin 后台管理框架使用技巧(持续更新中)
fastadmin 后台管理框架使用技巧(持续更新中) FastAdmin是一款基于ThinkPHP5+Bootstrap的极速后台开发框架,具体介绍,请查看文档,文档地址为:https://doc. ...
- Python9-数据类型-day3
数据类型转换 #int----->str s = 1 i = str(s) print(i) #str----->int s = ' i = int(s) print(i) #int--- ...
- python 初学函数
#len # s = '金老板小护士' # len(s) # def my_len(): #自定义函数 # i = 0 # for k in s: # i += 1 # print(i) # # le ...
- java程序——随机数求和
设计思路:用随机算法随机生成10个数(0~100),循环填充一个数组,然后在循环中显示数组内容,接着用一个循环计算数组元素的和,将结果也显示在消息框中. 流程图: 源代码: package test; ...