从别的大佬处看到的模板
#include<bits/stdc++.h>
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pqueue priority_queue
#define NEW(a,b) memset(a,b,sizeof(a))
#define Pii pair<int,int>
#define VI vector<int>
const double pi=4.0*atan(1.0);
const double e=exp(1.0);
const int maxn=3e6+;
typedef long long LL;
typedef unsigned long long ULL;
const LL mod=1e9+;
const ULL base=1e7+;
using namespace std;
LL qpow(LL a,LL b){
LL ans=;
a%=mod;
while(b){
if(b&){ans=ans*a%mod;}
a=a*a%mod;
b>>=;
}
return ans;
}
LL n;
namespace linear_seq{
const int N=;
LL res[N],base[N],_c[N],_md[N];
VI Md;
void mul(LL *a,LL *b,int k){
for(int i=;i<k+k;i++) _c[i]=;
for(int i=;i<k;i++) if(a[i]) for (int j=;j<k;j++) _c[i+j]=(_c[i+j]+a[i]*b[j])%mod;
for(int i=k+k-;i>=k;i--) if(_c[i])
for(int j=;j<(int)(Md).size();j++) _c[i-k+Md[j]]=(_c[i-k+Md[j]]-_c[i]*_md[Md[j]])%mod;
for(int i=;i<k;i++) a[i]=_c[i];
}
int solve(LL n,VI a,VI b){
LL ans=,pnt=;
int k=(int)(a).size();
assert((int)(a).size()==(int)(b).size());
for(int i=;i<k;i++) _md[k--i]=-a[i];_md[k]=;
Md.clear();
for (int i=;i<k;i++) if (_md[i]!=) Md.push_back(i);
for (int i=;i<k;i++) res[i]=base[i]=;
res[]=;
while((1ll<<pnt)<=n) pnt++;
for(int p=pnt;p>=;p--){
mul(res,res,k);
if((n>>p)&){
for (int i=k-;i>=;i--) res[i+]=res[i];res[]=;
for (int j=;j<(int)(Md).size();j++) res[Md[j]]=(res[Md[j]]-res[k]*_md[Md[j]])%mod;
}
}
for(int i=;i<k;i++) ans=(ans+res[i]*b[i])%mod;
if(ans<) ans+=mod;
return ans;
}
VI BM(VI s){
VI C(,),B(,);
int L=,m=,b=;
for(int n=;n<(int)(s).size();n++){
LL d=;
for(int i=;i<L+;i++) d=(d+(LL)C[i]*s[n-i])%mod;
if(d==) ++m;
else if(*L<=n) {
VI T=C;
LL c=mod-d*qpow(b,mod-)%mod;
while ((int)(C).size()<(int)(B).size()+m) C.push_back();
for (int i=;i<(int)(B).size();i++) C[i+m]=(C[i+m]+c*B[i])%mod;
L=n+-L; B=T; b=d; m=;
} else {
LL c=mod-d*qpow(b,mod-)%mod;
while ((int)(C).size()<(int)(B).size()+m) C.push_back();
for (int i=;i<(int)(B).size();i++) C[i+m]=(C[i+m]+c*B[i])%mod;
++m;
}
}
return C;
}
int gao(VI a,LL n){
VI c=BM(a);
c.erase(c.begin());
for(int i=;i<(int)(c).size();i++) c[i]=(mod-c[i])%mod;
return solve(n,c,VI(a.begin(),a.begin()+(int)(c).size()));
}
};
int main(){
int t;
for(scanf("%d",&t);t;t--){
scanf("%lld",&n);
printf("%d\n",linear_seq::gao(VI{,,,},n-));
}
}
https://ac.nowcoder.com/acm/contest/889/A
取余可以不是质数的高级BM/LR
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <bitset>
#include <cmath>
#include <cctype>
#include <unordered_map>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef vector<long long> VI;
typedef unsigned long long ull;
const ll inff = 0x3f3f3f3f3f3f3f3f;
const ll mod=1e9;
#define FOR(i,a,b) for(int i(a);i<=(b);++i)
#define FOL(i,a,b) for(int i(a);i>=(b);--i)
#define SZ(x) ((long long)(x).size())
#define REW(a,b) memset(a,b,sizeof(a))
#define inf int(0x3f3f3f3f)
#define si(a) scanf("%d",&a)
#define sl(a) scanf("%I64d",&a)
#define sd(a) scanf("%lf",&a)
#define ss(a) scanf("%s",a)
#define pb push_back
#define eps 1e-6
#define lc d<<1
#define rc d<<1|1
#define Pll pair<ll,ll>
#define P pair<int,int>
#define pi acos(-1)
ll powmod(ll a,ll b)
{
ll res=1ll;
while(b)
{
if(b&) res=res*a%mod;
a=a*a%mod,b>>=;
}
return res;
}
namespace linear_seq {
const int N=;
using int64 = long long;
using vec = std::vector<int64>;
ll res[N],base[N],_c[N],_md[N];
vector<int> Md;
void mul(ll *a,ll *b,int k) {
FOR(i,,k+k-) _c[i]=;
FOR(i,,k-) if (a[i]) FOR(j,,k-) _c[i+j]=(_c[i+j]+a[i]*b[j])%mod;
for (int i=k+k-;i>=k;i--) if (_c[i])
FOR(j,,SZ(Md)-) _c[i-k+Md[j]]=(_c[i-k+Md[j]]-_c[i]*_md[Md[j]])%mod;
FOR(i,,k-) a[i]=_c[i];
}
int solve(ll n,VI a,VI b) { // a 系数 b 初值 b[n+1]=a[0]*b[n]+...
// printf("%d\n",SZ(b));
ll ans=,pnt=;
int k=SZ(a);
assert(SZ(a)==SZ(b));
FOR(i,,k-) _md[k--i]=-a[i];_md[k]=;
Md.clear();
FOR(i,,k-) if (_md[i]!=) Md.push_back(i);
FOR(i,,k-) res[i]=base[i]=;
res[]=;
while ((1ll<<pnt)<=n) pnt++;
for (int p=pnt;p>=;p--) {
mul(res,res,k);
if ((n>>p)&) {
for (int i=k-;i>=;i--) res[i+]=res[i];res[]=;
FOR(j,,SZ(Md)-) res[Md[j]]=(res[Md[j]]-res[k]*_md[Md[j]])%mod;
}
}
FOR(i,,k-) ans=(ans+res[i]*b[i])%mod;
if (ans<) ans+=mod;
return ans;
}
VI BM(VI s) {
VI C(,),B(,);
int L=,m=,b=;
FOR(n,,SZ(s)-) {
ll d=;
FOR(i,,L) d=(d+(ll)C[i]*s[n-i])%mod;
if (d==) ++m;
else if (*L<=n) {
VI T=C;
ll c=mod-d*powmod(b,mod-)%mod;
while (SZ(C)<SZ(B)+m) C.pb();
FOR(i,,SZ(B)-) C[i+m]=(C[i+m]+c*B[i])%mod;
L=n+-L; B=T; b=d; m=;
} else {
ll c=mod-d*powmod(b,mod-)%mod;
while (SZ(C)<SZ(B)+m) C.pb();
FOR(i,,SZ(B)-) C[i+m]=(C[i+m]+c*B[i])%mod;
++m;
}
}
return C;
}
static void extand(vec &a, size_t d, int64 value = ) {
if (d <= a.size()) return;
a.resize(d, value);
}
static void exgcd(int64 a, int64 b, int64 &g, int64 &x, int64 &y) {
if (!b) x = , y = , g = a;
else {
exgcd(b, a % b, g, y, x);
y -= x * (a / b);
}
}
static int64 crt(const vec &c, const vec &m) {
int n = c.size();
int64 M = , ans = ;
for (int i = ; i < n; ++i) M *= m[i];
for (int i = ; i < n; ++i) {
int64 x, y, g, tm = M / m[i];
exgcd(tm, m[i], g, x, y);
ans = (ans + tm * x * c[i] % M) % M;
}
return (ans + M) % M;
}
static vec ReedsSloane(const vec &s, int64 mod) {
auto inverse = [](int64 a, int64 m) {
int64 d, x, y;
exgcd(a, m, d, x, y);
return d == ? (x % m + m) % m : -;
};
auto L = [](const vec &a, const vec &b) {
int da = (a.size() > || (a.size() == && a[])) ? a.size() - : -;
int db = (b.size() > || (b.size() == && b[])) ? b.size() - : -;
return std::max(da, db + );
};
auto prime_power = [&](const vec &s, int64 mod, int64 p, int64 e) {
// linear feedback shift register mod p^e, p is prime
std::vector<vec> a(e), b(e), an(e), bn(e), ao(e), bo(e);
vec t(e), u(e), r(e), to(e, ), uo(e), pw(e + );;
pw[] = ;
for (int i = pw[] = ; i <= e; ++i) pw[i] = pw[i - ] * p;
for (int64 i = ; i < e; ++i) {
a[i] = {pw[i]}, an[i] = {pw[i]};
b[i] = {}, bn[i] = {s[] * pw[i] % mod};
t[i] = s[] * pw[i] % mod;
if (t[i] == ) {
t[i] = , u[i] = e;
} else {
for (u[i] = ; t[i] % p == ; t[i] /= p, ++u[i]);
}
}
for (size_t k = ; k < s.size(); ++k) {
for (int g = ; g < e; ++g) {
if (L(an[g], bn[g]) > L(a[g], b[g])) {
ao[g] = a[e - - u[g]];
bo[g] = b[e - - u[g]];
to[g] = t[e - - u[g]];
uo[g] = u[e - - u[g]];
r[g] = k - ;
}
}
a = an, b = bn;
for (int o = ; o < e; ++o) {
int64 d = ;
for (size_t i = ; i < a[o].size() && i <= k; ++i) {
d = (d + a[o][i] * s[k - i]) % mod;
}
if (d == ) {
t[o] = , u[o] = e;
} else {
for (u[o] = , t[o] = d; t[o] % p == ; t[o] /= p, ++u[o]);
int g = e - - u[o];
if (L(a[g], b[g]) == ) {
extand(bn[o], k + );
bn[o][k] = (bn[o][k] + d) % mod;
} else {
int64 coef = t[o] * inverse(to[g], mod) % mod * pw[u[o] - uo[g]] % mod;
int m = k - r[g];
extand(an[o], ao[g].size() + m);
extand(bn[o], bo[g].size() + m);
for (size_t i = ; i < ao[g].size(); ++i) {
an[o][i + m] -= coef * ao[g][i] % mod;
if (an[o][i + m] < ) an[o][i + m] += mod;
}
while (an[o].size() && an[o].back() == ) an[o].pop_back();
for (size_t i = ; i < bo[g].size(); ++i) {
bn[o][i + m] -= coef * bo[g][i] % mod;
if (bn[o][i + m] < ) bn[o][i + m] -= mod;
}
while (bn[o].size() && bn[o].back() == ) bn[o].pop_back();
}
}
}
}
return std::make_pair(an[], bn[]);
};
std::vector<std::tuple<int64, int64, int>> fac;
for (int64 i = ; i * i <= mod; ++i)
if (mod % i == ) {
int64 cnt = , pw = ;
while (mod % i == ) mod /= i, ++cnt, pw *= i;
fac.emplace_back(pw, i, cnt);
}
if (mod > ) fac.emplace_back(mod, mod, );
std::vector<vec> as;
size_t n = ;
for (auto &&x: fac) {
int64 mod, p, e;
vec a, b;
std::tie(mod, p, e) = x;
auto ss = s;
for (auto &&x: ss) x %= mod;
std::tie(a, b) = prime_power(ss, mod, p, e);
as.emplace_back(a);
n = std::max(n, a.size());
}
vec a(n), c(as.size()), m(as.size());
for (size_t i = ; i < n; ++i) {
for (size_t j = ; j < as.size(); ++j) {
m[j] = std::get<>(fac[j]);
c[j] = i < as[j].size() ? as[j][i] : ;
}
a[i] = crt(c, m);
}
return a;
}
ll gao(VI a,ll n,ll mod,bool prime=true) {
VI c;
if(prime) c=BM(a);
else c=ReedsSloane(a,mod);
c.erase(c.begin());
FOR(i,,SZ(c)-) c[i]=(mod-c[i])%mod;
return solve(n,c,VI(a.begin(),a.begin()+SZ(c)));
}
};
ll gmod(ll a,ll b)
{
ll res=;
while(b)
{
if(b&) res=res*a%mod;
a=a*a%mod,b>>=;
}
return res;
}
int main()
{
cin.tie();
cout.tie();
ll n, m;
cin >> n >> m;
std::vector<long long> f = {, };
for (int i = ; i < ; i++)
f.push_back((f[i - ] + f[i - ]) % mod);
for (auto& t : f) t = gmod(t, m);
for (int i = ; i < ; i++)
f[i] = (f[i - ] + f[i]) % mod;
//LinearRecurrence qw(f,mod,false);
printf("%lld\n",linear_seq::gao(f,n,mod,false));
return ;
}