[CodeForces-441E]Valera and Number
题目大意:
给你一个数x,进行k次操作:
1.有p%的概率将x翻倍;
2.有1-p%的概率将x加1。
问最后二进制下x末尾0个数的期望。
思路:
动态规划。
由于k只到200,所以每次修改只与最后8位有关。
f[i][x][y][z]表示操作次数为i时,末尾8为表示的数字为x,第9位为y,第9位及以上和第9位数字连续相同的长度。
对于两种情况分别转移,注意特判超过8位的进位。
最后计算期望的时候,可以枚举第一维为k的所有状态,然后通过状态可以直接计算出末尾0的数量,乘上概率即可。
#include<cstdio>
#include<cctype>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
const int K=;
double f[K][][][];//[処理回数][末尾8bit][9bit目の値][9bit目の値が上にいくつ連続するか]
int main() {
int x=getint(),k=getint();
double p=getint()/100.0;
//初始状态
int cnt=,tmp=x>>;
while(tmp&&((tmp&)==((x>>)&))) {
tmp>>=;
cnt++;
}
f[][x&][(x>>)&][cnt]=;
for(register int i=;i<k;i++) {
for(register int x=;x<=;x++) {
for(register int y=;y<;y++) {
for(register int z=;z<;z++) {
/*times 2*/
f[i+][(x<<)&][(x>>)&][(((x>>)&)^y)?:(z+)]+=f[i][x][y][z]*p;
/*plus 1*/
if(x==) {//特殊情况:考虑最后八位存不下而进位的情况
if(y) {//如果第九位是1,则相当于前面那么多1都变成0
f[i+][][][z]+=f[i][x][y][z]*(-p);
} else {//如果第九位是0,则相当于把这个0变成1
f[i+][][][]+=f[i][x][y][z]*(-p);//这里把9位以后的1算作多少都没关系,因为反正最后统计的是0
}
} else {
f[i+][x+][y][z]+=f[i][x][y][z]*(-p);
}
}
}
}
}
//计算期望
double ans=;
for(register int x=;x<=;x++) {
for(register int y=;y<;y++) {
for(register int z=;z<;z++) {
int cnt=,tmp=x;
while(cnt<&&!(tmp&)) {
cnt++;
tmp>>=;
}
if(!y&&cnt==) cnt+=z;
ans+=f[k][x][y][z]*cnt;
}
}
}
printf("%.13f\n",ans);
return ;
}
[CodeForces-441E]Valera and Number的更多相关文章
- CodeForces - 441E:Valera and Number (DP&数学期望&二进制)
Valera is a coder. Recently he wrote a funny program. The pseudo code for this program is given belo ...
- CF 441E Valera and Number
CF 441E Description 一共执行\(k\)次,每次有\(p\%\)把\(x * 2\),有\((100 - p)\%\)把\(x + 1\).问二进制下\(x\)末尾期望\(0\)的个 ...
- Codeforces 441C Valera and Tubes
题目链接:Codeforces 441C Valera and Tubes 没看到r >= 2一直错.让前几个管子占用2个格子.最后一个把剩下的都占用了.假设问题有解.这样做一定有解.其它策略就 ...
- 【Codeforces441E】Valera and Number [DP]
Valera and Number Time Limit: 20 Sec Memory Limit: 512 MB Description Input Output Sample Input 5 3 ...
- CodeForces - 369E Valera and Queries(树状数组)
CodeForces - 369E Valera and Queries 题目大意:给出n个线段(线段的左端点和右端点坐标)和m个查询,每个查询有cnt个点,要求给出有多少条线段包含至少其中一个点. ...
- dp --- Codeforces 245H :Queries for Number of Palindromes
Queries for Number of Palindromes Problem's Link: http://codeforces.com/problemset/problem/245/H M ...
- Educational Codeforces Round 11 D. Number of Parallelograms 暴力
D. Number of Parallelograms 题目连接: http://www.codeforces.com/contest/660/problem/D Description You ar ...
- Codeforces 980 E. The Number Games
\(>Codeforces \space 980 E. The Number Games<\) 题目大意 : 有一棵点数为 \(n\) 的数,第 \(i\) 个点的点权是 \(2^i\) ...
- Codeforces 724 G Xor-matic Number of the Graph 线性基+DFS
G. Xor-matic Number of the Graph http://codeforces.com/problemset/problem/724/G 题意:给你一张无向图.定义一个无序三元组 ...
- codeforces 441B. Valera and Fruits 解题报告
题目链接:http://codeforces.com/problemset/problem/441/B 题目意思:有 n 棵fruit trees,每课水果树有两个参数描述:水果成熟的时间和这棵树上水 ...
随机推荐
- 【洛谷P2015】二叉苹果树
题目描述 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的结点) 这棵树共有N个结点(叶子点或者树枝分叉点),编号为1-N,树根编号一定是1. 我们用一根树枝两端连接的结点的编号来 ...
- 蓝色的PC端后台管理界面设计模板——后台
链接:http://pan.baidu.com/s/1o82hXX4 密码:x6le
- WordPress404页面自定义
不知道大家是怎么设计404页面,个性的404可以为网站增色不少,wordpress设置404是在主题里面的404.php页面上,当然比如你用Apache.nginx等服务器,你可以自己建一个单页,内容 ...
- dlmalloc(一)【转】
转自:http://blog.csdn.net/ycnian/article/details/12971863 我们写过很多C程序了,经常会分配内存.记得刚学C语言时老师说过,可以向两个地方申请内存: ...
- MAC和PHY的区别 (转自http://www.cnblogs.com/feitian629/archive/2013/01/25/2876857.html)
一块以太网网卡包括OSI(开方系统互联)模型的两个层.物理层和数据链路层.物理层定义了数据传送与接收所需要的电与光信号.线路状态.时钟基准.数据编码和电路等,并向数据链路层设备提供标准接口.数据链路层 ...
- 《secret》读书笔记
这是在从大连到深圳飞机上看完的一本书,也是大学毕业时室友整理书籍给我的.正好回来的途中,一气呵成读完了. 全书讲了几个事情,其中费了很大篇幅就围绕一个主题,当然书题了——秘密,这个秘密就是“吸引力法则 ...
- 用C++写程序的一些感悟
前言 近期使用C++有了一些心得很感悟,这里整理一下. 心得1 如果只会使用LabVIEW写程序,还想要进一步深入程序设计,一定要学习一门文本语言. 什么是会用LabVIEW 会用是个比较笼统的概念. ...
- 修改帧大小和socket缓冲区大小(转)
修改帧大小和socket缓冲区大小 MTU (最大传输单元)的缺省值为1500. 通过下面命令将其改为9000(jumbo frame) % ifconfig eth0 mtu 9000 socket ...
- AGC 16 D - XOR Replace
AGC 16 D - XOR Replace 附上attack(自为风月马前卒爷) 的题解 Problem Statement There is a sequence of length N: a=( ...
- fastdfs5.10 centos6.9 安装配置
下载相关软件 https://codeload.github.com/happyfish100/fastdfs/tar.gz/V5.10http://download.csdn.net/detail/ ...