"bzoj2318" Spoj4060 game with probability problem

Source: Internet
Author: User

Title Description

Alice and Bob are playing a game. There are n stones here, Alice and Bob take turns throwing coins, if the face up, then remove a stone from n stones, otherwise do nothing. The man who takes the last stone wins. Alice throws a shot at the probability of a p when she throws a coin, and, similarly, Bob has the probability of Q to throw out his congenial side.

Now Alice throws a coin, assuming they all want to win the game and ask you what the probability of Alice winning is.

Input

The first line is a positive integer t, which represents the number of data groups.

For each set of data, a row of three n,p,q.

Output

for each set of data output a single real number, which represents the probability of Alice winning, retains 6 decimal places.

Sample input

1
1 0.5 0.5

Sample output

0.666667

Tips

Data range:1<=t<=500.5<=p,q<=0.99999999for 100% of data 1<=n<=99999999 SolvingProbability DP The problem is really huge pit ... F[i] represents the probability that the first cast of a stone will win, and G[i] represents the probability of the winner of the I-block stone. Easy to launch f[i]= (p0*g[i-1]+ (1-p0) *q0*f[i-1])/(n (1-p0) * (1-q0)) g[i]= (q0*f[i-1]+ (1-q0) *p0*g[i-1])/(n (1-p0) * (1-q0)) here P Both 0 and q0 are target probabilities, and P and Q are the odds, so you need to decide if you want to face up. When F[i-1]<g[i-1], all want to face up, p0=p,q0=q, when F[i-1]>g[i-1], do not want to face up, p0=1-p,q0=1-q. But is n too big to be done? So using the probability of black technology: when n is getting bigger, f[n] gradually approaching a fixed value, and the topic only requires the retention of 6 decimal places. So the question F[1000+k] can be approximately equal to f[1000]. The time complexity is then reduced to O (1000T), which can be solved.
1#include <cstdio>2#include <cstring>3 Doublef[1001], g[1001];4 intMain ()5 {6     intT;7scanf"%d", &t);8      while(T-- )9     {Ten         intn, I; One         Doublep, q; Ascanf"%D%LF%LF", &n, &p, &q); -Memset (F,0,sizeof(f)); -Memset (G,0,sizeof(g)); the         if(N > +) -n = +; -f[0] =0; -g[0] =1; +          for(i =1; I <= N; i + + ) -         { +             if(F[i-1] > G[i-1]) Ap =1-P, q =1-Q; atF[i] = (p * g[i-1] + (1-P) * Q * f[i-1]) / (1- (1-P) * (1-q)); -G[i] = (Q * f[i-1] + (1-q) * p * g[i-1]) / (1- (1-P) * (1-q)); -             if(F[i-1] > G[i-1]) -p =1-P, q =1-Q; -         } -printf"%.6lf\n", F[n]); in     } -     return 0; to}

"bzoj2318" Spoj4060 game with probability problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.