bzoj2676
二分概率+矩乘+dp
也是二分概率,然后dp[i][j][k]表示当前到了i,有j条命,下一次的收益是k,然后矩乘转移,但是我自己的似乎wa了,抄了liu_runda的才行,具体不知道为什么
注释的是我自己写的,谁能告诉我哪里错了?
#include<bits/stdc++.h>
using namespace std;
const int N = ;
int n, r, q, tot;
int id[N][N];
double S;
struct matrix {
double a[N][N];
matrix() { for(int i = ; i <= tot; ++i) for(int j = ; j <= tot; ++j) a[i][j] = 0.0; }
matrix friend operator * (const matrix &a, const matrix &b) {
matrix ret;
for(int k = ; k <= tot; ++k)
for(int i = ; i <= tot; ++i) if(a.a[i][k] >= 1e-)
for(int j = ; j <= tot; ++j)
ret.a[i][j] += a.a[i][k] * b.a[k][j];
return ret;
}
void set() {
for(int i = ; i <= tot; ++i) a[i][i] = 1.0;
}
};
double calc(double p)
{
matrix A, B;
A.set();
/*
第i轮j条命这一次得k分
dp[i][j][k]=(dp[i+1][j + 1][k + 1] + k) * p
dp[i][j][k] += dp[i + 1][j - 1][1] * (1.0 - p)
矩阵乘法
*/
B.a[tot][tot] = 1.0;
for(int i = ; i <= q; ++i)
for(int j = ; j <= r; ++j)
{
if(i > ) B.a[id[i - ][]][id[i][j]] = 1.0 - p;
B.a[tot][id[i][j]] = p * (double)j;
if(i < q && j < r) B.a[id[i + ][j + ]][id[i][j]] = p;
else if(i < q) B.a[id[i + ][j]][id[i][j]] = p;
else if(j < r) B.a[id[i][j + ]][id[i][j]] = p;
else B.a[id[i][j]][id[i][j]] = p;
}
// for(int j = 0; j <= q; ++j)
// for(int k = 1; k <= r; ++k) B.a[tot][id[j][k]] = p * (double)k;
// for(int j = 0; j <= q; ++j)
// for(int k = 1; k <= r; ++k) B.a[id[min(j + 1, q)][min(k + 1, r)]][id[j][k]] = p;
// for(int j = 2; j <= q; ++j)
// for(int k = 1; k <= r; ++k) B.a[id[j - 1][1]][id[j][k]] = 1.0 - p;
for(int i = n; i; i >>= , B = B * B) if(i & ) A = A * B;
// double ret = 0.0;
// for(int i = 0; i <= tot; ++i)
// {
// for(int j = 0; j <= tot; ++j) printf("%.6f ", A.a[i][j]);
// puts("");
// }
// for(int i = 0; i < tot; ++i) ret += A.a[tot][i];
// printf("p = %.6f ret = %.6f\n", p, ret);
return A.a[tot][id[q][]];
}
int main()
{
scanf("%d%d%d%lf", &n, &r, &q, &S);
for(int i = ; i <= q; ++i)
for(int j = ; j <= r; ++j) id[i][j] = tot++;
// printf("tot = %d\n", tot);
double l = 0.0, r = 1.0, ans = -1.0;
while(r - l > 1e-)
{
double mid = (l + r) / 2.0;
if(calc(mid) > S) r = ans = mid;
else l = mid;
}
if(ans == -1.0) puts("Impossible.");
else printf("%.6f\n", ans);
return ;
}
bzoj2676的更多相关文章
- bzoj2676 Contra
题意: 给定N,R,Q,S 有N个关卡,初始有Q条命,且任意时刻最多只能有Q条命 每通过一个关卡,会得到u分和1条命,其中u=min(最近一次连续通过的关数,R) 若没有通过这个关卡,将失去一条命,并 ...
随机推荐
- Hadoop安装和基本单机部署
下载安装 # 下载 $ cd /usr/local $ wget http://mirrors.hust.edu.cn/apache/hadoop/common/hadoop-2.9.2/hadoo ...
- Spring Boot集成Spring Data Reids和Spring Session实现Session共享(多个不同的应用共用一个Redis实例)
从Redis的Key入手,比如Spring Session在注解@EnableRedisHttpSession上提供了redisNamespace属性,只需要在这里设置不同的值即可,效果应该是这样的: ...
- Windows Server 远程桌面报错:No Remote Desktop License Servers Available
问题描述: 在用远程桌面访问Window Server服务器时,出现如下错误: The remote session was disconnected because there are no Rem ...
- SQL视图优化改写为存储过程遇到 双引号 单引号问题
核心在于拼接SQL字符串中遇到中文双引号问题: 可以使用系统函数 替换掉set @pageStr = replace(@queryStr,'"','''') 不过更推荐 使用两个单 ...
- android官方Api 理解Activity生命周期的回调机制(适合有基础的人看)
原文地址:http://www.android-doc.com/training/basics/activity-lifecycle/starting.html#lifecycle-states 此处 ...
- AWS向中国有限预览客户推出多级别AWS支持服务
2014年9月26日 在AWS中国(北京)区域有限预览服务开展的过程中.很多客户都提出了对AWS支持服务(AWS Support)的需求. AWS客户在向云端部署系统,日常运营维护以及关键性项目实 ...
- webrtc初探
0.闲来无事,想研究webrtc,看了一些网上的文章之后,觉得谬误较多,以讹传讹的比较多,自己试验了一把,记录一下. 官网的写的教程在实践中也觉得不用那么复杂,有种落伍与繁冗的感觉. 1.我想看的是w ...
- 【转载】一致性哈希算法(consistent hashing)
一致性哈希算法在1997年由麻省理工学院提出的一种分布式哈希(DHT)实现算法,设计目标是为了解决因特网中的热点(Hot spot)问题,初衷和CARP十分类似.一致性哈希修正了CARP使用的简 单哈 ...
- 令人赞叹的 MySQL
原文链接 译文链接 感谢 艾凌风 小伙伴校稿 令人赞叹的 MySQL 一个很棒的 MySQL 软件.库以及资源列表. 这个列表接受并鼓舞 pull requests,请看 CONTRIBUTING 文 ...
- hiberinate二级缓存
hibernate.cfg.xml配置 <!-- 二级缓存类型 --> <property name="hibernate.cache.region.factory_cla ...