[CSP-S Simulation Test]: Simple desired (DP)

Title Description

Once there the variable $ x $, its initial value is given.
You will perform followed by $ n $ operations, each operation has a $ p \% probability of $ make $ x = x \ times 2 $ , $ (100-p) \% probability of $ make $ x = x + 1 $ .
The resulting value is assumed $ $ W, so W $ $ $ D $ of prime factorization of the number of $ 2 $, $ D $ find desired.


Input Format

Read data from files $ exp.in $ in.
The first row of three integers $ x, n, p $, have the meaning described in the subject.


Output Format

Output to a file in $ exp.out $.
A real line, indicates a desired $ D $.
If your answer with the standard error of the answer does not exceed $ 10 ^ {- 6} $, is determined to be correct.


Sample

Sample input 1:

1 1 50

Sample output 1:

1.0000000000

Sample input 2:

5 3 0

Sample Output 2:

3.0000000000

Sample input 3:

5 3 25

Sample output 3:

1.9218750000


Data range and tips

For data $ 20 \% $ a, $ n \ leqslant 20 $;
for $ 30 \% $ data, $ n \ leqslant 50 $;
for $ 50 \% $ data, $ n \ leqslant 100 $;
for $ 100 \% $ data, $ x \ leqslant 10 ^ 9 , n \ leqslant 200,0 \ leqslant p \ leqslant 100 $.


answer

First, the prime factorization of the number of $ 2 $ is the binary representation of the number of end $ 0 $.

Consider $ DP $, set $ dp [i] [s] [j] [0/1] $ $ expressed after operations of $ i, the binary representation of the current number of bits is $ 8 $ $ s $, ninth $ 0 / $ 1, beginning from the ninth consecutive identical probability forward position J $ $.

After the first to explain the $ 8 $ bit of reason, because at least $ 1 $ 2 ^ 8 + operations before they can carry the $ 9 $, but only $ 200 $ operands, so it will not cause the contribution to answer.

J $ $ again explain the purpose, if the bit is $ 8 $ $ $ 1 $ 1 $ is the ninth bit, we performed the operation of $ 1 + $ 1000 $ ...... will become $ and $ J $ is to calculate out of this position $ 1 $.

Transfer a total of $ 8 $ a, is not detailed, concrete can look at the code.

Time complexity: $ \ Theta (2 ^ 8n ^ 2) $.

Expectations score: $ 100 $ points.

Actual score: $ 100 $ points.


Code time

#include<bits/stdc++.h>
using namespace std;
int X,N,P,mx;
double gl1,gl2;
double dp[201][257][300][2];
double ans;
void pre_work()
{
	int flag=X>>8,sum=0;
	for(int i=flag&1;(flag&1)==i&&flag;sum++)flag>>=1;
	if(!sum)sum++;mx=N+sum;
	dp[0][X&255][sum][(X>>8)&1]=1;
}
int judge(int x)
{
	int res=0;
	while(!(x&1)){res++;x>>=1;}
	return res;
}
int main()
{
	scanf("%d%d%d",&X,&N,&P);
	gl1=(double)P/100;
	gl2=(double)(100-P)/100;
	pre_work();
	for(int i=0;i<N;i++)
		for(int s=0;s<256;s++)
			for(int j=1;j<=mx;j++)
			{
				if(!(((s<<1)&256)>>8))dp[i+1][(s<<1)&255][j+1][0]=dp[i+1][(s<<1)&255][j+1][0]+dp[i][s][j][0]*gl1;
				else dp[i+1][(s<<1)&255][1][1]=dp[i+1][(s<<1)&255][1][1]+dp[i][s][j][0]*gl1;
				if(((s<<1)&256)>>8)dp[i+1][(s<<1)&255][j+1][1]=dp[i+1][(s<<1)&255][j+1][1]+dp[i][s][j][1]*gl1;
				else dp[i+1][(s<<1)&255][1][0]=dp[i+1][(s<<1)&255][1][0]+dp[i][s][j][1]*gl1;
				if(s==255)dp[i+1][0][1][1]=dp[i+1][0][1][1]+dp[i][255][j][0]*gl2;
				else dp[i+1][s+1][j][0]=dp[i+1][s+1][j][0]+dp[i][s][j][0]*gl2;
				if(s==255)dp[i+1][0][j][0]=dp[i+1][0][j][0]+dp[i][255][j][1]*gl2;
				else dp[i+1][s+1][j][1]=dp[i+1][s+1][j][1]+dp[i][s][j][1]*gl2;
			}
	for(int s=1;s<256;s++)
		for(int j=1;j<=mx;j++)
			ans+=(dp[N][s][j][0]+dp[N][s][j][1])*judge(s);
	for(int j=1;j<=mx;j++)ans+=dp[N][0][j][0]*(j+8)+dp[N][0][j][1]*8;
	printf("%.6lf",ans);
	return 0;
}

rp++

Guess you like

Origin www.cnblogs.com/wzc521/p/11670746.html