[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,每课水果树有两个参数描述:水果成熟的时间和这棵树上水 ...
随机推荐
- JS 判断手机操作系统代码
还是利用UA, 返回值: ios, android, unknown function getMobileType () { var ua = window.navigator.userAgent.t ...
- app 测试基础
1. 安装和启动 (1) OTA安装测试 · app必须能够通过ota安装到测试设备上 · 如果app安装完后没有icon,那么必须能够通过其他的方法来启动这个app ...
- Linux端口占用
1.netstat netstat -anp | grep 23232 Sample: [root@BICServer 0825]# netstat -anp | grep 23232 tcp 0 0 ...
- 解读Linux命令格式(转)
解读Linux命令格式 环境 Linux HA5-139JK 2.6.18-164.el5 #1 SMP Tue Aug 18 15:51:48 EDT 2009 x86_64 x86_64 x8 ...
- Python os模块和sys模块 操作系统的各种接口
一.os模块 这个模块提供了一个便携式去使用操作系统的相关功能,如果只是想操作路径,请参阅os.path模块. ''' os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 ...
- 禁用quartz自动检查更新
禁用quartz自动检查更新的3种方法 1, <bean id="startQuertz" lazy-init="false" autowire=&quo ...
- Vim的分屏功能
本篇文章主要教你如何使用 Vim 分屏功能. 分屏启动Vim 使用大写的O参数来垂直分屏. vim -On file1 file2 ... 使用小写的o参数来水平分屏. vim -on file1 f ...
- Ubuntu每次开机后提示:检测到系统程序出现问题的解决方法
首先,错误报告存放位置: cd /var/crash/ ls //可以查看错误报告 1 2 sudo rm /var/crash/* //删除该目录下的所有文件 1 但是,这只是删除掉的是错误报告,如 ...
- 使用angluar-cli的ng g component home指令出现的错误
Error: ELOOP: too many symbolic links encountered, stat '/Users/zzy/angular/taskmgr/node_modules/@an ...
- 进度条算法 progressBar
; ;var maxNum=int.MaxValue; progressBar.Maximum =maxNum; progressBar.Minimum = ; progressBar.Value = ...