Valera is a coder. Recently he wrote a funny program. The pseudo code for this program is given below:

//input: integers x, k, p
a = x;
for(step = 1; step <= k; step = step + 1){
rnd = [random integer from 1 to 100];
if(rnd <= p)
a = a * 2;
else
a = a + 1;
} s = 0; while(remainder after dividing a by 2 equals 0){
a = a / 2;
s = s + 1;
}

Now Valera wonders: given the values x, k and p, what is the expected value of the resulting number s?

Input

The first line of the input contains three integers x, k, p (1 ≤ x ≤ 109; 1 ≤ k ≤ 200; 0 ≤ p ≤ 100).

Output

Print the required expected value. Your answer will be considered correct if the absolute or relative error doesn't exceed 10 - 6.

Examples

Input
1 1 50
Output
1.0000000000000
Input
5 3 0
Output
3.0000000000000
Input
5 3 25
Output
1.9218750000000

题意:给定X,现在进行K轮操作,每一轮有P%的概率加倍,(100-P)%的概率加一,问K轮之后的X的因子2的次数。

思路:首先一个数X中2的因子个数=转化为二进制后末尾0的个数=__builtin_ctz(X);题解给的四维DP比较麻烦,这里有一种比较难以想到,但是不难理解的二维DP。

用dp[i][j],表示X+j进行i轮操作后的因子2的幂。 那么答案就是dp[K][0];

那么转移就是:

dp[i][j]+=(dp[i-1][j+1])*(1.0-P);   即第一次操作为+1;
            dp[i][j<<1]+=(dp[i-1][j]+1)*P;  即第一次操作为*2;

虽然乘法的话第二维会加倍增长,但加法的话第二维会减小1,所以最小影响到dp[K][0]的最大第二维就是K,而不用维护所有的dp[][j],即j<=K;

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std; //*2 P
double dp[][],P;
int main()
{
int X,K;
scanf("%d%d%lf",&X,&K,&P); P/=100.0;
rep(i,,K) dp[][i]=__builtin_ctz(X+i);
rep(i,,K){
rep(j,,K){
dp[i][j]+=(dp[i-][j+])*(1.0-P);
dp[i][j<<]+=(dp[i-][j]+)*P;
}
}
printf("%.10lf\n",dp[K][]);
return ;
}

CodeForces - 441E:Valera and Number (DP&数学期望&二进制)的更多相关文章

  1. 【Codeforces441E】Valera and Number [DP]

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

  2. CF 441E Valera and Number

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

  3. LightOj 1030 - Discovering Gold(dp+数学期望)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1030 题意:在一个1*n 的格子里,每个格子都有相应的金币数,走到相应格子的话,就会得 ...

  4. BZOJ1076 [SCOI2008]奖励关 【状压dp + 数学期望】

    1076: [SCOI2008]奖励关 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 3074  Solved: 1599 [Submit][Sta ...

  5. bzoj1415 [Noi2005]聪聪和可可【概率dp 数学期望】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1415 noip2016 D1T3,多么痛的领悟...看来要恶补一下与期望相关的东西了. 这是 ...

  6. P4707-重返现世【dp,数学期望,扩展min-max容斥】

    正题 题目链接:https://www.luogu.com.cn/problem/P4707 题目大意 \(n\)个物品,每次生成一种物品,第\(i\)个被生成的概率是\(\frac{p_i}{m}\ ...

  7. 【BZOJ】1076: [SCOI2008]奖励关(状压dp+数学期望)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1076 有时候人蠢还真是蠢.一开始我看不懂期望啊..白书上其实讲得很详细的,什么全概率,全期望(这个压 ...

  8. CF 148D D. Bag of mice (概率DP||数学期望)

    The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests ...

  9. [poj2096] Collecting Bugs【概率dp 数学期望】

    传送门:http://poj.org/problem?id=2096 题面很长,大意就是说,有n种bug,s种系统,每一个bug只能属于n中bug中的一种,也只能属于s种系统中的一种.一天能找一个bu ...

随机推荐

  1. Auto Encoder用于异常检测

    对基于深度神经网络的Auto Encoder用于异常检测的一些思考 from:https://my.oschina.net/u/1778239/blog/1861724 一.前言 现实中,大部分数据都 ...

  2. ONVIF协议学习笔记

    一.理解 1.1 技术理解 ONVIF = 服务端 + 客户端 =(Web Services + RTSP)+ 客户端 = ((WSDL + SOAP) + RTSP) + 客户端 WSDL是服务端用 ...

  3. --save-dev 与 --save区别

    npm install 在安装 npm 包时,有两种命令参数可以把它们的信息写入 package.json 文件,一个是npm install --save另一个是 npm install --sav ...

  4. 微信小程序web-view使用测试总结

    1.后台配置业务域名. 2.在开发者工具的web-view组件中绑定业务域名. 3.点击开发者工具的详情按钮,选择调试基础库高版本,如果不设置,有可能绑定的业务域名内容不显示. 4.如果是公众号上的内 ...

  5. learning hdmi edid protocol

    referenc: https://en.wikipedia.org/wiki/Extended_Display_Identification_Data

  6. day19-python的正则表达式2

    正则对象的findall方法 findall(string[, pos[, endpos]])  搜索string,以列表形式返回全部能匹配的子串. import re p1 = re.compile ...

  7. OO作业总结报告3

    规格化设计的发展史 下面部分来源:https://www.cnblogs.com/eggert/p/9098446.html: 随着计算机硬件的飞速发展,以及应用复杂度越来越高,软件规模越来越大,原有 ...

  8. mac以及centos下安装Elasticsearch 以及权限管理插件

    Elasticsearch安装(提前系统需要安装java环境)mac安装 brew install elasticsearch centos安装 下载ElasticSearch安装包,https:// ...

  9. Cracking The Coding Interview 3.6

    // Write a program to sort a stack in ascending order. You should not make any assumptions about how ...

  10. 4.6 C++抽象基类和纯虚成员函数

    参考:http://www.weixueyuan.net/view/6376.html 总结: 在C++中,可以通过抽象基类来实现公共接口 纯虚成员函数没有函数体,只有函数声明,在纯虚函数声明结尾加上 ...