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?
Input
The first line of the input contains three integers x, k, p (1 ≤ x ≤ 109; 1 ≤ k ≤ 200; 0 ≤ p
Print the required expected value. Your answer will be considered correct if the absolute or relative error doesn‘t exceed 10 - 6.
Examples Input1 1 50Output
1.0000000000000Input
5 3 0Output
3.0000000000000Input
5 3 25Output
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[210][410],P; int main() { int X,K; scanf("%d%d%lf",&X,&K,&P); P/=100.0; rep(i,0,K) dp[0][i]=__builtin_ctz(X+i); rep(i,1,K){ rep(j,0,K){ dp[i][j]+=(dp[i-1][j+1])*(1.0-P); dp[i][j<<1]+=(dp[i-1][j]+1)*P; } } printf("%.10lf\n",dp[K][0]); return 0; }
CodeForces - 441E:Valera and Number (DP&數學期望&二進制)