Bzoj 4204 ball Game Loop matrix optimization expectation recursion

Source: Internet
Author: User
Tags mul

Test Instructions:Link Method:Cyclic matrix optimization is expected to be recursive. parsing:This topic recursive not what, mainly is the cyclic matrix optimization we found that if the direct matrix optimization is n^3log, so the certainty is not over, and then observe the problem we ask for power of the Matrix, found that he is this form 1 1 0 0 00 1 1 0 00 0 1 1 00 0 0 1 11 0 0 0 1 Each row is the last line to the right. So we can use a one-dimensional array to represent the loop matrix and the loop matrix to sum, product or loop matrix so we can use the loop matrix to optimize the one n complexity to become n^2log. Code:
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define N 1010using namespace STD;intN,m,k;intA[n];structmatrix{Double Map[N];}; Matrix Ori; Matrix BAS; Matrix Bask; Matrix ans; Matrix C;///F[I][J] Indicates the expectation of the ball of the first time to take out the marking J//f[i][j]=f[i-1][j]+1/m*f[i-1][j-1]-1/m*f[i-1][j];Matrix Mul (Matrix A,matrix b) { for(intI=1; i<=n;i++) {c.Map[i]=0; for(intj=1; j<=n;j++) {c.Map[I]+=a.Map[J]*b.Map[(i-j+n+1)%n==0? N: (i-j+n+1)%n]; }    }returnC;} Matrix Quick_my (Matrix A,intY) {Matrix ret;memset(Ret.Map,0,sizeof(Ret.Map)); Ret.Map[1]=1; while(y) {if(y&1) {Ret=mul (ret,a);        } a=mul (A,a); y>>=1; }returnRET;}intMain () {scanf("%d%d%d", &n,&m,&k); for(intI=1; i<=n;i++)scanf("%d", &a[i]); for(intI=1; i<=n;i++) the Ori.Map[I]= (Double) A[i]; Bas.Map[1]=(Double* (n1)/(Double) m; Bas.Map[2]=1.0/(Double) m;    Bask=quick_my (BAS,K); Ans=mul (Ori,bask); for(intI=1; i<=n;i++)printf("%.3lf\n", ans.Map[i]);}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Bzoj 4204 ball Game Loop matrix optimization expectation recursion

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.