题目大意:
  给你一个数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的更多相关文章

  1. 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 ...

  2. CF 441E Valera and Number

    CF 441E Description 一共执行\(k\)次,每次有\(p\%\)把\(x * 2\),有\((100 - p)\%\)把\(x + 1\).问二进制下\(x\)末尾期望\(0\)的个 ...

  3. Codeforces 441C Valera and Tubes

    题目链接:Codeforces 441C Valera and Tubes 没看到r >= 2一直错.让前几个管子占用2个格子.最后一个把剩下的都占用了.假设问题有解.这样做一定有解.其它策略就 ...

  4. 【Codeforces441E】Valera and Number [DP]

    Valera and Number Time Limit: 20 Sec  Memory Limit: 512 MB Description Input Output Sample Input 5 3 ...

  5. CodeForces - 369E Valera and Queries(树状数组)

    CodeForces - 369E Valera and Queries 题目大意:给出n个线段(线段的左端点和右端点坐标)和m个查询,每个查询有cnt个点,要求给出有多少条线段包含至少其中一个点. ...

  6. dp --- Codeforces 245H :Queries for Number of Palindromes

    Queries for Number of Palindromes Problem's Link:   http://codeforces.com/problemset/problem/245/H M ...

  7. Educational Codeforces Round 11 D. Number of Parallelograms 暴力

    D. Number of Parallelograms 题目连接: http://www.codeforces.com/contest/660/problem/D Description You ar ...

  8. Codeforces 980 E. The Number Games

    \(>Codeforces \space 980 E. The Number Games<\) 题目大意 : 有一棵点数为 \(n\) 的数,第 \(i\) 个点的点权是 \(2^i\) ...

  9. Codeforces 724 G Xor-matic Number of the Graph 线性基+DFS

    G. Xor-matic Number of the Graph http://codeforces.com/problemset/problem/724/G 题意:给你一张无向图.定义一个无序三元组 ...

  10. codeforces 441B. Valera and Fruits 解题报告

    题目链接:http://codeforces.com/problemset/problem/441/B 题目意思:有 n 棵fruit trees,每课水果树有两个参数描述:水果成熟的时间和这棵树上水 ...

随机推荐

  1. JAVA 企业培训

  2. php常用表单验证类用法实例

    <?php /** * 页面作用:常用表单验证类 * 作 者:欣然随风 * QQ:276624915 */ class class_post { //验证是否为指定长度的字母/数字组合 func ...

  3. Mysql储存过程4:mysql变量设置

    默认全局变量是两个@@开头, 可用show variables查看所有默认变量: @@user #declare定义变量只能用在储存过程中 #declare 变量名 数据类型 可选类型 declare ...

  4. mysql中列的增删改

    增加列: ); ) after id; ) first; 修改列名: ); #change可改名字与字段类型 mysql> alter table a change uid uid int; Q ...

  5. Python3 反射及常用的方法

    反射就是通过字符串映射或修改程序运行时的状态.属性.方法 有四个常用方法: hasattr(obj,name_str) 判断一个obj对象是否有对应name_str的方法 getattr(obj,na ...

  6. VirtualBox与Genymotion命令行启动

    一.VirtualBox命令行启动 1.添加环境变量: %programfiles%\Oracle\VirtualBox 2.用VBoxManage查看已存在vmname|uuid命令: VBoxMa ...

  7. Netty框架入门

    一.概述     Netty是由JBOSS提供的一个java开源框架.     Netty提供异步的.事件驱动的网络应用程序框架和工具,用以快速开发高性能.高可靠性的网络服务器和客户端程序.   二. ...

  8. Linux 不常用命令总结

    1. vim编辑模式下,搜索,/user,跳转下一个,小写的n 2.

  9. [ python ] 初始面向对象

    首先,通过之前学习的函数编写一个 人狗大战 的例子. 分析下这个需求,人 狗 大战  三个事情.角色:人.狗动作:狗咬人,人打狗 先创建人和狗两个角色: def person(name, hp, ag ...

  10. java关键字(详解)

    目录 1. 基本类型 1) boolean 布尔型 2) byte 字节型 3) char 字符型 4) double 双精度 5) float 浮点 6) int 整型 7) long 长整型 8) ...