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