Berlekamp-Massey algorithm

Keywords: less

Big Blacks Blog:
https://blog.csdn.net/qq_39972971/article/details/80725873
https://blog.csdn.net/oi_Konnyaku/article/details/85805426
https://blog.csdn.net/As_A_Kid/article/details/86286891
http://www.cnblogs.com/zzqsblog/p/6877339.html
The zzq gangster's template should be the most correct, there is no picture to choose just the one before, but I don't know if it will break what one gangster said:

"Note that when the optimal recursive order is less than or equal to half the length of a given number of columns, the BM algorithm can guarantee the shortest recursive solution, otherwise it can only guarantee that a feasible solution can be found."

Urgent, offline, etc.

We haven't described any algorithm yet, but I don't need to after reading these blogs.
It is a clever construction to use the former illegality to construct the later legitimacy.

Template:

#include<bits/stdc++.h>
#define mod 998244353
using namespace std;

int Pow(int base,int k){
	int ret = 1;
	for(;k;k>>=1,base=1ll*base*base%mod)
		if(k&1)
			ret=1ll*ret*base%mod;
	return ret;
}

#define vi vector<int>
vi BM(vi arr){
	vi now,pre,bst;
	int fail,dtfail;
	for(int i=0,sz=arr.size();i<sz;i++){
		int dt = arr[i];
		for(int j=0;j<pre.size();j++) dt = (dt - 1ll * pre[j] * arr[i-j-1]) % mod;
		if(!dt) continue;
		if(pre.empty()) now.clear() , now.resize(i+1) , fail = i , dtfail = dt;
		else{
			int K = dt * 1ll * Pow(dtfail,mod-2) % mod;
			now.clear() , now.resize(i-fail-1),
			now.push_back(K);
			for(int j=0;j<bst.size();j++) now.push_back(-K*1ll*bst[j]%mod);
			if(now.size()<pre.size()) now.resize(pre.size());
			for(int j=0;j<pre.size();j++) now[j] = (now[j] + pre[j]) % mod;
			if(i - fail + bst.size() >= pre.size()) bst = pre , fail = i , dtfail = dt;
		}	
		swap(now,pre);
	}
	return pre;
}

int main(){
	int n;
	scanf("%d",&n);
	vi a;a.resize(n);
	for(int i=0;i<n;i++) scanf("%d",&a[i]);
	vi b = BM(a);
	printf("%d\n",b.size());
	for(int i=0;i<b.size();i++) printf("%d ",(b[i]+mod)%mod);
}

Posted by sanlove on Thu, 07 Nov 2019 07:15:49 -0800