Easy to get: dp[n] = dp[n-1] * p + dp[n-2] * (1-P); (1)
If there is a ray in position I, then: Dp[i + 1] = dp[i-1] * (1-P);
How to obtain Dp[i]?
We can solve the characteristic equation (1) and get:
Dp[n] = A * (p-1) ^ (n-1) + B;
After A and b have been obtained, do a quick power.
We can also get the answer directly with the matrix fast power.
1#include <algorithm>2#include <iostream>3#include <cstring>4#include <cstdio>5 using namespacestd;6 7 Const intN = One;8 intMine[n];9 intN;Ten DoubleA, B, p; One A DoubleQuickpow (DoubleDintm) - { - DoubleAns =1.0, W =D; the while(m) - { - if(M &1 ) - { +Ans = ans *W; - } +w = w *W; Am = M >>1; at } - returnans; - } - - DoubledpintN) - { in if(n = =0)return 0; - returnA * QUICKPOW (P-1N1) +b; to } + - intMain () the { * while(SCANF ("%D%LF", &n, &p)! =EOF) $ {Panax NotoginsengA = (P-1.0)/(P-2.0 ); -b =1.0/ (2-p); the for(inti =0; I < n; i++ ) + { Ascanf"%d", Mine +i); the } +Sort (mine, mine +n); - DoubleAns =1.0; $ intCur =1; $ for(inti =0; I < n; i++ ) - { - intdis = mine[i]-cur; theAns = ans * DP (DIS) * (1-p); -Cur = mine[i] +1;Wuyi } theprintf"%.7f\n", ans); - } Wu return 0; -}
POJ 3744 characteristic equation + fast power