首页 > 编程语言 > 详细

Berlekamp-Massey算法模板

时间:2021-07-20 09:23:48      阅读:26      评论:0      收藏:0      [点我收藏+]

Berlekamp-Massey算法模板

#include <cstdio>
#include <vector>
using namespace std;
typedef long long Lint;
const Lint mod = 1e9 + 7;
Lint fpow(Lint a, Lint b) {
    Lint res = 1;
    for (; b; b >>= 1) {
        if (b & 1) res = res * a % mod;
        a = a * a % mod;
    }
    return res;
}
Lint inv(Lint a) { return fpow(a, mod - 2); }
vector<Lint> BM(const vector<Lint>& a) {
    vector<Lint> last, cur;
    int fail;
    Lint delta;
    for (int i = 0; i < a.size(); i++) {
        Lint t = a[i];
        for (int j = 0; j < cur.size(); j++) t = (t + mod - a[i - j - 1] * cur[j] % mod) % mod;
        if (t == 0) continue;
        if (cur.empty()) {
            cur.resize(i + 1);
            fail = i;
            delta = t;
            continue;
        }
        Lint p = t * inv(delta) % mod;
        vector<Lint> nxt(i - fail - 1);
        nxt.push_back(p);
        for (int j = 0; j < last.size(); j++) nxt.push_back((mod - p) * last[j] % mod);
        if (nxt.size() < cur.size()) nxt.resize(cur.size());
        for (int j = 0; j < cur.size(); j++) nxt[j] = (nxt[j] + cur[j]) % mod;
        if (i - fail + last.size() >= (int)cur.size()) last = cur, fail = i, delta = t;
        cur = nxt;
    }
    return cur;
}

int main() {
    vector<Lint> res = BM(vector<Lint>{1, 1, 2, 3, 5, 8, 13, 21});
    return 0;
}

Berlekamp-Massey算法模板

原文:https://www.cnblogs.com/Bamboo-Wind/p/15032756.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!