[POJ3682] King Arthur's Birthday Celebration [desirable the DP]

Perhaps a better reading experience \ (\ mathcal {Description} \ )

Toss a coin every day, the probability of coin-side up is p, k times until it raises the front until the end of the first \ (i \) takes days for the coin toss (2i-1 \) \ , the number of days determined the coin toss expectations and expectations of cost.

\(\mathcal{Solution}\)

The double experience entitled question, which would see

collect stamps (Forgive my laziness, but this can be considered the same title)

Here we find \ (F \) of the array to process the probability \ (P \) , seeking \ (G \) , the inside of the \ (f [i] \) and \ (f [i + 1] \ ) all can be multiplied by 2

Note that this problem output is limited! ! ! Otherwise would have been inexplicable WA, as well as the compiler select \ (C ++ \)

\(\mathcal{Code}\)

/*******************************
Author:Morning_Glory
LANG:C++
Created Time:2019年07月22日 星期一 20时27分43秒
*******************************/
#include <cstdio>
#include <fstream>
using namespace std;
const int maxn = 1005;
int n,fir;
double p;
double f[maxn],g[maxn];
int main()
{
    while (true){
        scanf("%d",&n);
        if (!n) return 0;
        if (fir)    printf("\n");
        ++fir;
        scanf("%lf",&p);
        f[n]=g[n]=0;
        for (int i=n-1;i>=0;--i)    f[i]=f[i+1]+1/p;
        for (int i=n-1;i>=0;--i)    g[i]=(1+2*f[i])/p-2*f[i]+g[i+1]+2*f[i+1];
        printf("%.3lf %.3lf",f[0],g[0]);
    }
    return 0;
}

If not quite understand where to put it or there is an error, please correct me
if you like, you may wish to point a collection of praise about it

Guess you like

Origin www.cnblogs.com/Morning-Glory/p/11228459.html