BM Du teach recursive version is in the case of integers modulo,
This real number coefficients can be solved, but may have a precision error.
If the conclusion is a problem to solve by pushing recursive linear, taking into account the actual situation, template BM algorithm can be used, and then turn to enter a number of entries, the more the more accurate term (Logically, the first k items recursive, only 2 * k starting terms can be determined)
#include <bits/stdc++.h> using namespace std; #define rep(i,a,b) for(int i=int(a);i<int(b);++i) #define mem(a,p) memset(a,p,sizeof(a)) #define MAXN 1005 struct BM { int n{}; vector<double> ps[MAXN]; int pn{},fail[MAXN]{}; double delta[MAXN]{}; void Solve(const double *x,int n) { pn=0; mem(fail,0); mem(delta,0); ps[0].clear(); rep(i,1,n+1) { double dt=-x[i]; rep(j,0,ps[pn].size()) dt+=x[i-j-1]*ps[pn][j]; delta[i]=dt; if(fabs(dt)<=1e-8)continue; fail[pn]=i; if(!pn) { ps[++pn].resize(1); continue; } vector<double> &ls=ps[pn-1]; double k=-dt/delta[fail[pn-1]]; vector<double> cur; cur.resize(i-fail[pn-1]-1); cur.push_back(-k); rep(j,0,ls.size())cur.push_back(ls[j]*k); if(cur.size()<ps[pn].size())cur.resize(ps[pn].size()); rep(j,0,ps[pn].size())cur[j]+=ps[pn][j]; ps[++pn]=cur; } } void print() { cout<<setiosflags(ios::fixed)<<setprecision(10); for(int i = 0;i<ps[pn].size();++i) { cout<<ps[pn][i]<<" "; } cout<<endl; } }B; double x[MAXN]; int main() { int n; while(cin>>n) { for(int i = 1;i<=n;++i) { cin>>x[i]; } B.Solve(x,n); B.print(); } }
Code From: