POJ 3744 matrix Acceleration--probability DP

Source: Internet
Author: User

http://poj.org/problem?id=3744


Commit two,, recursive, matrix power what will, but I push to be different from others,,, should be on the matrix to understand the problem,, and then look at

#include <cstdio> #include <cstring> #include <algorithm> #include <string> #include < iostream> #include <iomanip> #include <cmath> #include <map> #include <set> #include <

Queue> using namespace std; #define LS (RT) rt*2 #define RS (RT) rt*2+1 #define LL long #define ULL unsigned long long #define REP (i,s,e) for (int i =s;i<e;i++) #define REPE (i,s,e) for (int i=s;i<=e;i++) #define CL (A, B) memset (A,b,sizeof (a)) #define in (s) freopen (
S, "R", stdin) #define OUT (s) freopen (S, "w", stdout) const int maxn=3;
Double MTR[MAXN][MAXN];
Double ANSM[MAXN][MAXN];
int sz=2;
    void mulmtr (double x[maxn][maxn], double Y[MAXN][MAXN]) {double TMP[MAXN][MAXN];
            for (int i=0;i<sz;i++) for (int j=0;j<sz;j++) {tmp[i][j]=0;
        for (int k=0;k<sz;k++) TMP[I][J]+=X[I][K]*Y[K][J];
} for (int i=0;i<sz;i++) for (int j=0;j<sz;j++) Y[I][J]=TMP[I][J];
}
void Mtrmi (double mtr[maxn][maxn],int n) {for (Int. i=0;i<sz;i++) for (int j=0;j<sz;j++) {
            if (i = = j) ansm[i][j]=1;
        else ansm[i][j]=0;
        } while (n) {if (n&1) {mulmtr (MTR,ANSM);
        } mulmtr (MTR,MTR);
    n/=2;
}} int bomb[maxn*10];
    int main () {//in ("poj3744.txt");
    int n;
    int last;
    Double p;
    Double ans=1.0;
    int POS;
        while (~SCANF ("%d%lf", &n,&p)) {ans=1.0;
        Last=1;
        for (int i=0;i<n;i++) scanf ("%d", &bomb[i]);
        Sort (bomb,bomb+n);
            for (int i=0;i<n;i++) {pos=bomb[i]-last;
            /*if (pos = = 0) {last=pos+1;continue;}
            if (pos = = 1) {ans*= (1-p); last=pos+1;continue;} if (pos = = 2) {ans*=p;last=pos+1;continue;}
            *///pos-=2;
            Mtr[0][0]=p;
            Mtr[0][1]=1.0-p;
            mtr[1][0]=1.0;
mtr[1][1]=0.0;            Mtrmi (Mtr,pos);
            ANS*=1.0-ANSM[0][0];
        last=bomb[i]+1;
    } cout << Fixed << setprecision (7) << ans << Endl;
} return 0;
 }


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.