[Codeforces441E]Valera and Number

Problem

给定一个数x,有p%的概率乘2,有1-p%的概率加1,问操作k次,其二进制数下末尾零的个数的期望。

Solution

每次操作只会影响到最后的8位
我们用dp[i][j]表示i个操作后,后面的操作还需要加j
对于+1的操作:dp[i][j-1]+=dp[i-1][j](1-p)
对于
2的操作:dp[i][j*2]+=(dp[i-1][j]+1)*p

Notice

需要预处理

Code

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define sqz main
#define ll long long
#define reg register int
#define rep(i, a, b) for (reg i = a; i <= b; i++)
#define per(i, a, b) for (reg i = a; i >= b; i--)
#define travel(i, u) for (reg i = head[u]; i; i = edge[i].next)
const int INF = 1e9;
const double eps = 1e-6, phi = acos(-1.0);
ll mod(ll a, ll b) {if (a >= b || a < 0) a %= b; if (a < 0) a += b; return a;}
ll read(){ ll x = 0; int zf = 1; char ch; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;}
void write(ll y) { if (y < 0) putchar('-'), y = -y; if (y > 9) write(y / 10); putchar(y % 10 + '0');}
double p;
int x, k;
double f[205][205];
int sqz()
{
    int x = read(), k = read();
    scanf("%lf", &p), p /= 100;
    rep(i, 0, k)
    {
        int t = x + i;
        while (1)
        {
            if (t & 1) break;
            f[0][i]++;
            t /= 2;
        }
    }
    rep(i, 1, k)
    {
        memset(f[i], 0, sizeof f[i]);
        rep(j, 0, k)
        {
            if (j) f[i][j - 1] += f[i - 1][j] * (1 - p);
            if (j * 2 <= k) f[i][j * 2] += (f[i - 1][j] + 1) * p;
        }
    }
    printf("%.10lf\n", f[k][0]);
}

转载于:https://www.cnblogs.com/WizardCowboy/p/7688831.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值