POJ 3744 Scout yyf I matrix rapid Power Optimization-probability dp

Source: Internet
Author: User

POJ 3744 Scout yyf I matrix rapid Power Optimization-probability dp
Click Open Link

Scout YYF I
Time Limit:1000 MS Memory Limit:65536 K
Total Submissions:5416 Accepted:1491

Description

YYF is a couragous scout. now he is on a dangerous mission which is to penetrate into the enemy's base. after overcoming a series difficulties, YYF is now at the start of enemy's famous "mine road ". this is a very long road, on which there are numbers of mines. at first, YYF is at step one. for each step after that, YYF will walk one step with a probabilityP, Or jump two step with a probality of 1-P. Here is the task, given the place of each mine, please calculate the probality that YYF can go through the "mine road" safely.

Input

The input contains into test cases endedEOF.
Each test case contains two lines.
The First line of each test case isN(1 ≤N≤ 10) andP(Less than or equal to 0.25P≤ 0.75) seperated by a single blank, standing for the number of mines and the probability to walk one step.
The Second line of each test case is N integer standing for the place of N mines. Each integer is in the range of [1, 100000000].

Output

For each test case, output the probabilty in a single line with the precision to 7 digits after the decimal point.

Sample Input

1 0.522 0.52 4

Sample Output

0.50000000.2500000

Source

POJ Monthly Contest-2009.08.23, Simon

There are n mines in 1 ~ In the 100000000 position, the initial position is 1, the probability of taking one step is p, and the probability of taking two steps is 1-p, and the probability of passing through all mines is obtained. State transition equation: dp [I] = p * dp [I-1] + (1-p) * dp [I-2], if it is calculated as MLE or TLE, it can be converted to a matrix-based quick power solution.Authorization + Cgo8cHJlIGNsYXNzPQ = "brush: java;"> # include # Include Using namespace std; double p; int pos [17]; struct Matrax {double m [2] [2];} a, per, tmp; void init () // create a matrix {. m [0] [0] = p;. m [0] [1] = 1-p;. m [1] [0] = 1;. m [1] [1] = 0; per. m [0] [0] = 1; per. m [0] [1] = 0; per. m [1] [0] = 0; per. m [1] [1] = 1;} Matrax multi (Matrax a, Matrax B) // matrix multiplication {Matrax c; for (int I = 0; I <2; I ++) for (int j = 0; j <2; j ++) {c. m [I] [j] = 0; for (int k = 0; k <2; k ++) c. m [I] [j] + =. m [I] [k] * B. m [k] [j];} return c;} Matrax power (int k) // Matrix fast power {Matrax pp = a, ans = per; while (k) {if (k & 1) {ans = multi (ans, pp); k --;} else {k >>= 1; pp = multi (pp, pp) ;}} return ans ;}int main () {int n; while (scanf ("% d % lf", & n, & p )! = EOF) {bool no = false; double ans = 1; memset (pos, 0, sizeof (pos); for (int I = 1; I <= n; I ++) scanf ("% d", & pos [I]); sort (pos + 1, pos + n + 1); for (int I = 1; I <= n; I ++) if (pos [I] = 1 | pos [I] = pos [I-1]) {no = true; break ;} if (no) {printf ("%. 7lf \ n ", 0); continue;} init (); tmp = power (pos [1]-1 ); // calculate the probability ans * = (1-tmp.m [0] [0]) from 1 to the first ray; // the probability for (int I = 2; I <= n; I ++) {if (pos [I] = pos [I-1]) continue; tmp = power (pos [I)-pos [I-1]-1 ); // calculate the probability ans * = (1-tmp.m [0] [0]) from the next step of the first I-1 Ray; // less than the probability of the I-th ray} printf ("%. 7lf \ n ", ans);} return 0 ;}

Related Article

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.