CodeForces - 441E:Valera and Number (DP&数学期望&二进制)
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?
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
1 1 50
1.0000000000000
5 3 0
3.0000000000000
5 3 25
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&数学期望&二进制)的更多相关文章
- 【Codeforces441E】Valera and Number [DP]
Valera and Number Time Limit: 20 Sec Memory Limit: 512 MB Description Input Output Sample Input 5 3 ...
- CF 441E Valera and Number
CF 441E Description 一共执行\(k\)次,每次有\(p\%\)把\(x * 2\),有\((100 - p)\%\)把\(x + 1\).问二进制下\(x\)末尾期望\(0\)的个 ...
- LightOj 1030 - Discovering Gold(dp+数学期望)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1030 题意:在一个1*n 的格子里,每个格子都有相应的金币数,走到相应格子的话,就会得 ...
- BZOJ1076 [SCOI2008]奖励关 【状压dp + 数学期望】
1076: [SCOI2008]奖励关 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 3074 Solved: 1599 [Submit][Sta ...
- bzoj1415 [Noi2005]聪聪和可可【概率dp 数学期望】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1415 noip2016 D1T3,多么痛的领悟...看来要恶补一下与期望相关的东西了. 这是 ...
- P4707-重返现世【dp,数学期望,扩展min-max容斥】
正题 题目链接:https://www.luogu.com.cn/problem/P4707 题目大意 \(n\)个物品,每次生成一种物品,第\(i\)个被生成的概率是\(\frac{p_i}{m}\ ...
- 【BZOJ】1076: [SCOI2008]奖励关(状压dp+数学期望)
http://www.lydsy.com/JudgeOnline/problem.php?id=1076 有时候人蠢还真是蠢.一开始我看不懂期望啊..白书上其实讲得很详细的,什么全概率,全期望(这个压 ...
- 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 ...
- [poj2096] Collecting Bugs【概率dp 数学期望】
传送门:http://poj.org/problem?id=2096 题面很长,大意就是说,有n种bug,s种系统,每一个bug只能属于n中bug中的一种,也只能属于s种系统中的一种.一天能找一个bu ...
随机推荐
- ftp/sftp定时自动上传文件脚本(CentOS)
1.ftp自动上传文件脚本 #!/bin/bash ftp -n<<! open 192.168.220.129 user ls toor binary hash cd /path/to/ ...
- CAD(布置厨洁具)(尺寸标注)5.12
"TYTK"打开图库,找到平面厨具和洁具.双击选中的厨具,A可以不停旋转90度.给厨具选取正确的位置.画出灶台线,同理画出卫生间的家具.绘制出洗脸台的平台.浴缸的平台. 尺寸标注: ...
- bzoj1679
题解: 前缀和 显然需要排序一下 注意爆int这件事 代码: #include<bits/stdc++.h> using namespace std; typedef long long ...
- vue-router-2-动态路由配置
const User = { template: '<div>User{{ $route.params.id }}</div>' } const router = new Vu ...
- HTML--思维导图
HTML--思维导图
- sas 变量类型转换
data b2: set b1; newbl=put(oldbl,10.); run; 根据转换后的类型灵活填写
- VS2010编译Unigine_2010源码
VS2010编译Unigine_2010源码[Debug版本] 1.Laucher工程属性改为控制台项目 2.Unigine工程编译时的Warnning LNK2019 a.属性--常规-目标文件名改 ...
- [SCOI2005]栅栏
这个题...只能说比较水... 排序后,算一个前缀和,二分dfs查找答案...加上两个剪枝就过了...QVQ 我只能刷这种水题...我太菜了...QVQ #include<iostream> ...
- 自动化测试工具Telerik Test Studio发布R1 2019|附下载
Telerik Test Studio是一个用于功能性Web.桌面和移动测试的直观测试自动化工具,它能轻松地实现自动化测试.同时会为GUI.性能.加载和API测试提供完整的自动化测试解决方案. [Te ...
- git相关知识点
git add 和 git stage 有什么区别: 工作区(Working Directory).暂存区(Stage)和历史记录区(History)以及转换关系不能少: git stage 是 gi ...