結果
| 問題 | No.3080 Colonies on Line |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-01-26 21:07:37 |
| 言語 | C++17 (gcc 15.2.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 1,609 ms / 6,500 ms |
| コード長 | 23,358 bytes |
| 記録 | |
| コンパイル時間 | 4,416 ms |
| コンパイル使用メモリ | 285,456 KB |
| 実行使用メモリ | 12,708 KB |
| 最終ジャッジ日時 | 2026-01-26 21:08:03 |
| 合計ジャッジ時間 | 24,986 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 35 |
ソースコード
// BEGIN: main.cpp
#line 1 "main.cpp"
#ifndef i_am_noob
#include <bits/allocator.h>
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,popcnt")
#endif
#include<bits/stdc++.h>
using namespace std;
#define all(a) a.begin(),a.end()
#define pb push_back
#define sz(a) ((int)a.size())
using ll=long long;
using u32=unsigned int;
using u64=unsigned long long;
using i128=__int128;
using u128=unsigned __int128;
using f128=__float128;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
template<typename T> using vc=vector<T>;
template<typename T> using vvc=vc<vc<T>>;
template<typename T> using vvvc=vc<vvc<T>>;
using vi=vc<int>;
using vll=vc<ll>;
using vvi=vc<vi>;
using vvll=vc<vll>;
#define vv(type,name,n,...) \
vector<vector<type>> name(n,vector<type>(__VA_ARGS__))
#define vvv(type,name,n,m,...) \
vector<vector<vector<type>>> name(n,vector<vector<type>>(m,vector<type>(__VA_ARGS__)))
template<typename T> using min_heap=priority_queue<T,vector<T>,greater<T>>;
template<typename T> using max_heap=priority_queue<T>;
// https://trap.jp/post/1224/
#define rep1(n) for(ll i=0; i<(ll)(n); ++i)
#define rep2(i,n) for(ll i=0; i<(ll)(n); ++i)
#define rep3(i,a,b) for(ll i=(ll)(a); i<(ll)(b); ++i)
#define rep4(i,a,b,c) for(ll i=(ll)(a); i<(ll)(b); i+=(c))
#define cut4(a,b,c,d,e,...) e
#define rep(...) cut4(__VA_ARGS__,rep4,rep3,rep2,rep1)(__VA_ARGS__)
#define per1(n) for(ll i=((ll)n)-1; i>=0; --i)
#define per2(i,n) for(ll i=((ll)n)-1; i>=0; --i)
#define per3(i,a,b) for(ll i=((ll)a)-1; i>=(ll)(b); --i)
#define per4(i,a,b,c) for(ll i=((ll)a)-1; i>=(ll)(b); i-=(c))
#define per(...) cut4(__VA_ARGS__,per4,per3,per2,per1)(__VA_ARGS__)
#define rep_subset(i,s) for(ll i=(s); i>=0; i=(i==0?-1:(i-1)&(s)))
template<typename T, typename S> constexpr T ifloor(const T a, const S b){return a/b-(a%b&&(a^b)<0);}
template<typename T, typename S> constexpr T iceil(const T a, const S b){return ifloor(a+b-1,b);}
template<typename T>
void sort_unique(vector<T> &vec){
sort(vec.begin(),vec.end());
vec.resize(unique(vec.begin(),vec.end())-vec.begin());
}
template<typename T, typename S> constexpr bool chmin(T &a, const S b){if(a>b) return a=b,true; return false;}
template<typename T, typename S> constexpr bool chmax(T &a, const S b){if(a<b) return a=b,true; return false;}
template<typename T, typename S> istream& operator >> (istream& i, pair<T,S> &p){return i >> p.first >> p.second;}
template<typename T, typename S> ostream& operator << (ostream& o, const pair<T,S> &p){return o << p.first << ' ' << p.second;}
#ifdef i_am_noob
#define bug(...) cerr << "#" << __LINE__ << ' ' << #__VA_ARGS__ << "- ", _do(__VA_ARGS__)
template<typename T> void _do(vector<T> x){for(auto i: x) cerr << i << ' ';cerr << "\n";}
template<typename T> void _do(set<T> x){for(auto i: x) cerr << i << ' ';cerr << "\n";}
template<typename T> void _do(unordered_set<T> x){for(auto i: x) cerr << i << ' ';cerr << "\n";}
template<typename T> void _do(T && x) {cerr << x << endl;}
template<typename T, typename ...S> void _do(T && x, S&&...y) {cerr << x << ", "; _do(y...);}
#else
#define bug(...) 777771449
#endif
template<typename T> void print(vector<T> x){for(auto i: x) cout << i << ' ';cout << "\n";}
template<typename T> void print(set<T> x){for(auto i: x) cout << i << ' ';cout << "\n";}
template<typename T> void print(unordered_set<T> x){for(auto i: x) cout << i << ' ';cout << "\n";}
template<typename T> void print(T && x) {cout << x << "\n";}
template<typename T, typename... S> void print(T && x, S&&... y) {cout << x << ' ';print(y...);}
template<typename T> istream& operator >> (istream& i, vector<T> &vec){for(auto &x: vec) i >> x; return i;}
vvi read_graph(int n, int m, int base=1){
vvi adj(n);
for(int i=0,u,v; i<m; ++i){
cin >> u >> v,u-=base,v-=base;
adj[u].pb(v),adj[v].pb(u);
}
return adj;
}
vvi read_tree(int n, int base=1){return read_graph(n,n-1,base);}
template<typename T, typename S> pair<T,S> operator + (const pair<T,S> &a, const pair<T,S> &b){return {a.first+b.first,a.second+b.second};}
template<typename T> constexpr T inf=0;
template<> constexpr int inf<int> = 0x3f3f3f3f;
template<> constexpr ll inf<ll> = 0x3f3f3f3f3f3f3f3f;
template<typename T> vector<T> operator += (vector<T> &a, int val){for(auto &i: a) i+=val; return a;}
template<typename T> T isqrt(const T &x){T y=sqrt(x+2); while(y*y>x) y--; return y;}
#define ykh mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
// BEGIN: library/mod/modint.hpp
#line 1 "library/mod/modint.hpp"
// BEGIN: library/nt/extgcd.hpp
#line 1 "library/nt/extgcd.hpp"
// ax + by = gcd(a,b), {gcd(a,b),x,y}
template<typename T>
array<T,3> extgcd(T a, T b){
T x1=1,y1=0,x2=0,y2=1;
while(b!=0){
T q=a/b;
a%=b;
swap(a,b);
T x3=x1-x2*q,y3=y1-y2*q;
x1=x2,y1=y2,x2=x3,y2=y3;
}
return {a,x1,y1};
}
template<typename T>
T modinv(T x, T m){
auto [g,val1,val2]=extgcd<T>(x,m);
assert(g==1);
if(val1<0) val1+=m;
return val1;
}// END: library/nt/extgcd.hpp
#line 4 "library/mod/modint.hpp"
template<int64_t m, bool is_prime, typename word, typename dword>
struct Modint{
using mint=Modint;
static constexpr word mod=m;
static constexpr word get_mod(){
return mod;
}
word x;
constexpr Modint():x(0){}
constexpr Modint(int _x):x((_x%m+m)%m){}
constexpr Modint(int64_t _x):x((_x%m+m)%m){}
constexpr Modint(uint64_t _x):x(_x%m){}
mint &operator += (const mint &o){
if((x+=o.x)>=mod) x-=mod;
return *this;
}
mint &operator -= (const mint &o){
if((x-=o.x)<0) x+=mod;
return *this;
}
mint &operator *= (const mint &o){
x=((dword)x)*o.x%mod;
return *this;
}
mint &operator /= (const mint &o){
return (*this)*=o.inv();
}
mint operator + (const mint &o) const {return mint(*this)+=o;}
mint operator - (const mint &o) const {return mint(*this)-=o;}
mint operator * (const mint &o) const {return mint(*this)*=o;}
mint operator / (const mint &o) const {return mint(*this)/=o;}
mint operator - () const {return mint(0)-*this;}
mint pow(int64_t n) const {
assert(n>=0);
mint res=1,b=*this;
for(; n; n>>=1,b*=b) if(n&1) res*=b;
return res;
}
inline mint inv1() const {
return pow(m-2);
}
inline mint inv2() const {
auto [g,val1,val2]=extgcd<word>(x,mod);
assert(g==1);
return mint(val1);
}
mint inv() const {
if(is_prime) return inv1();
return inv2();
}
bool operator == (const mint &o) const {
return x==o.x;
}
bool operator != (const mint &o) const {
return x!=o.x;
}
friend istream& operator >> (istream& is, mint &b){
int64_t y;
is >> y;
b=mint(y);
return is;
}
friend ostream& operator << (ostream& os, const mint &b){
return os << b.x;
}
// v2(m-1), 2^(v2(m-1))-th root
static constexpr pair<int,int> ntt_data(){
if(m==998244353) return {23,31};
return {-1,-1};
}
};
template<int64_t m, bool is_prime=true> using modint=Modint<m,is_prime,int32_t,int64_t>;
template<int64_t m, bool is_prime=true> using modint_64=Modint<m,is_prime,int64_t,__int128>;// END: library/mod/modint.hpp
#line 113 "main.cpp"
using mint=modint<998244353>;
//using mint=modint<1000000007>;
// BEGIN: library/poly/linear_recursion.hpp
#line 1 "library/poly/linear_recursion.hpp"
// BEGIN: library/poly/poly.hpp
#line 1 "library/poly/poly.hpp"
// BEGIN: library/mod/modint_basic.hpp
#line 1 "library/mod/modint_basic.hpp"
template<typename mint>
mint fac(int n){
static const int mod=mint::get_mod();
static vector<mint> res={1,1};
if(n>=mod) return 0;
assert(n>=0);
while(n>=(int)res.size()) res.push_back(res.back()*res.size());
return res[n];
}
template<typename mint>
mint inv(int n){
static const int mod=mint::get_mod();
static vector<mint> res={0,1};
assert(n>=0&&n<mod);
while(n>=(int)res.size()) res.push_back(res[mod%res.size()]*(mod-mod/res.size()));
return res[n];
}
template<typename mint>
mint ifac(int n){
static const int mod=mint::get_mod();
static vector<mint> res={1,1};
if(n>=mod) return 0;
assert(n>=0);
while(n>=(int)res.size()) res.push_back(res.back()*inv<mint>(res.size()));
return res[n];
}
template<typename mint>
mint C(int n, int m){
if(m<0||m>n) return 0;
return fac<mint>(n)*ifac<mint>(m)*ifac<mint>(n-m);
}
template<typename mint>
mint stars_and_bars(int n, int m){
if(n<0||m<0) return 0;
if(n==0){
if(m==0) return 1;
return 0;
}
return C<mint>(m+n-1,n-1);
}// END: library/mod/modint_basic.hpp
#line 4 "library/poly/poly.hpp"
// BEGIN: library/poly/convolution.hpp
#line 1 "library/poly/convolution.hpp"
// BEGIN: library/poly/ntt.hpp
#line 1 "library/poly/ntt.hpp"
template<typename mint>
struct NTT{
static constexpr int mod=mint::get_mod(),N=mint::ntt_data().first,g=mint::ntt_data().second;
mint w[2][N+1],w1[2][N],w2[2][N];
using u64=uint64_t;
NTT(){
w[0][N]=g,w[1][N]=w[0][N].inv();
for(int i=N-1; i>=0; --i){
w[0][i]=w[0][i+1]*w[0][i+1];
w[1][i]=w[1][i+1]*w[1][i+1];
}
mint prod=1,iprod=1;
for(int i=0; i<N-1; ++i){
w1[0][i]=w[0][i+2]*prod;
w1[1][i]=w[1][i+2]*iprod;
prod*=w[1][i+2];
iprod*=w[0][i+2];
}
prod=iprod=1;
for(int i=0; i<N-2; ++i){
w2[0][i]=w[0][i+3]*prod;
w2[1][i]=w[1][i+3]*iprod;
prod*=w[1][i+3];
iprod*=w[0][i+3];
}
}
void trans(vector<mint> &a, int k, bool inv=false){
assert((int)a.size()==(1<<k));
int n=1<<k;
if(!inv){
int len=0;
while(len<k){
if(k-len==1){
int p=1<<(k-len-1);
mint rot=1;
for(int s=0; s<(1<<len); ++s){
int offset=s<<(k-len);
for(int i=0; i<p; ++i){
mint l=a[i+offset],r=a[i+offset+p]*rot;
a[i+offset]+=r,a[i+offset+p]=l-r;
}
rot*=w1[0][__lg(~s&-~s)];
}
len++;
}
else{
int p=1<<(k-len-2);
mint rot=1,imag=w[0][2];
u64 mod2=((u64)mod)*mod;
for(int s=0; s<(1<<len); ++s){
mint rot2=rot*rot,rot3=rot2*rot;
int offset=s<<(k-len);
for(int i=0; i<p; ++i){
u64 a0=a[i+offset].x,a1=u64(a[i+offset+p].x)*rot.x,a2=u64(a[i+offset+2*p].x)*rot2.x,a3=u64(a[i+offset+3*p].x)*rot3.x;
u64 tmp=(a1+mod2-a3)%mod*imag.x;
u64 na2=mod2-a2;
a[i+offset]=a0+a2+a1+a3;
a[i+offset+p]=a0+a2+(2*mod2-(a1+a3));
a[i+offset+2*p]=a0+na2+tmp;
a[i+offset+3*p]=a0+na2+(mod2-tmp);
}
rot*=w2[0][__lg(~s&-~s)];
}
len+=2;
}
}
}
else{
int len=k;
while(len){
if(len==1){
len--;
int p=1<<(k-len-1);
mint rot=1;
for(int s=0; s<(1<<len); ++s){
int offset=s<<(k-len);
for(int i=0; i<p; ++i){
u64 l=a[i+offset].x,r=a[i+offset+p].x;
a[i+offset]=l+r,a[i+offset+p]=(mod+l-r)*rot.x;
}
rot*=w1[1][__lg(~s&-~s)];
}
}
else{
len-=2;
int p=1<<(k-len-2);
mint rot=1,imag=w[1][2];
for(int s=0; s<(1<<len); ++s){
mint rot2=rot*rot,rot3=rot2*rot;
int offset=s<<(k-len);
for(int i=0; i<p; ++i){
u64 a0=a[i+offset].x,a1=a[i+offset+p].x,a2=a[i+offset+2*p].x,a3=a[i+offset+3*p].x;
u64 tmp=(mod+a2-a3)*imag.x%mod;
a[i+offset]=a0+a1+a2+a3;
a[i+offset+p]=(a0+mod-a1+tmp)*rot.x;
a[i+offset+2*p]=(a0+a1+(2*mod-(a2+a3)))*rot2.x;
a[i+offset+3*p]=(a0+2*mod-a1-tmp)*rot3.x;
}
rot*=w2[1][__lg(~s&-~s)];
}
}
}
mint inv=mint(n).inv();
for(int i=0; i<n; ++i) a[i]*=inv;
}
}
};// END: library/poly/ntt.hpp
#line 4 "library/poly/convolution.hpp"
template<typename mint>
vector<mint> convolution_naive(vector<mint> a, vector<mint> b){
if(a.empty()||b.empty()) return {};
int n=((int)a.size())+((int)b.size())-1;
vector<mint> res(n);
for(int i=0; i<((int)a.size()); ++i) for(int j=0; j<((int)b.size()); ++j){
res[i+j]+=a[i]*b[j];
}
return res;
}
template<typename mint>
vector<mint> convolution_ntt(vector<mint> a, vector<mint> b){
if(a.empty()||b.empty()) return {};
static NTT<mint> ntt;
int n=((int)a.size())+((int)b.size())-1;
int m=1,k=0;
while(m<n) m<<=1,k++;
a.resize(m),b.resize(m);
ntt.trans(a,k),ntt.trans(b,k);
for(int i=0; i<m; ++i) a[i]*=b[i];
ntt.trans(a,k,true);
a.resize(n);
return a;
}
template<typename mint>
vector<mint> convolution(vector<mint> a, vector<mint> b){
if(a.empty()||b.empty()) return {};
int n=((int)a.size())+((int)b.size())-1;
if(mint::ntt_data().first<0||n<49||min((int)a.size(),(int)b.size())<=5) return convolution_naive(a,b);
return convolution_ntt(a,b);
}// END: library/poly/convolution.hpp
#line 5 "library/poly/poly.hpp"
template<typename mint>
struct poly: vector<mint>{
using vector<mint>::vector;
poly(const vector<mint> &v):vector<mint>(v){}
poly operator += (const poly &o){
if(o.size()>this->size()) this->resize(o.size());
for(int i=0; i<(int)o.size(); ++i) (*this)[i]+=o[i];
return *this;
}
poly operator += (const mint &o){
if(this->empty()) this->resize(1);
(*this)[0]+=o;
return *this;
}
poly operator -= (const poly &o){
if(o.size()>this->size()) this->resize(o.size());
for(int i=0; i<(int)o.size(); ++i) (*this)[i]-=o[i];
return *this;
}
poly operator -= (const mint &o){
if(this->empty()) this->resize(1);
(*this)[0]-=o;
return *this;
}
poly operator *= (const poly &o){
return *this=convolution<mint>(*this,o);
}
poly operator *= (const mint &o){
for(int i=0; i<(int)this->size(); ++i) (*this)[i]*=o;
return *this;
}
poly operator + (const poly &o) const {return poly(*this)+=o;}
poly operator + (const mint &o) const {return poly(*this)+=o;}
poly operator - (const poly &o) const {return poly(*this)-=o;}
poly operator - (const mint &o) const {return poly(*this)-=o;}
poly operator * (const poly &o) const {return poly(*this)*=o;}
poly operator * (const mint &o) const {return poly(*this)*=o;}
poly operator - () const {return poly()-*this;}
poly interval(int l, int r){
assert(l<=r&&r<=(int)this->size());
poly res(this->begin()+l,this->begin()+r);
return res;
}
poly topos(){
while((this->size())&&(this->back())==mint(0)){
this->pop_back();
}
return *this;
}
poly inverse(){
int n=this->size();
assert((*this)[0]!=0);
poly res(1,(*this)[0].inv());
poly b=*this;
for(int m=1; m<n; m<<=1){
if(n<m*2) b.resize(m*2);
poly v1=b.interval(0,m*2),v2=res;
v1*=v2;
v1.resize(m*2);
v1*=v2;
res.resize(m*2);
for(int i=0; i<m; ++i) res[i]+=res[i];
for(int i=0; i<m*2; ++i) res[i]-=v1[i];
}
res.resize(n);
return res;
}
pair<poly,poly> divide(poly b){
// return {quotient, remainder}
poly a=*this;
int n=a.size(),m=b.size(),k=n-m+1;
if(n<m) return {poly({0}),a};
if(mint::ntt_data().first<0||min(m,k)<=50){
poly q(k),r;
mint tmp=b[m-1].inv();
for(int i=k-1; i>=0; --i){
q[i]=a[m-1+i]*tmp;
for(int j=0; j<m; ++j){
a[i+j]-=q[i]*b[j];
}
assert(a[m-1+i]==mint(0));
}
a.resize(m-1);
r=a;
return {q,r};
}
poly ra=a,rb=b;
reverse(all(ra)),reverse(all(rb));
ra.resize(k),rb.resize(k);
poly q=ra*rb.inverse();
q.resize(k);
reverse(all(q));
poly r=a-b*q;
r.resize(m-1);
return {q,r};
}
poly derivative(){
int n=this->size();
poly res(n-1);
for(int i=0; i<n-1; ++i) res[i]=(*this)[i+1]*mint(i+1);
return res;
}
poly integral(){
int n=this->size();
poly res(n+1);
for(int i=0; i<n; ++i) res[i+1]=(*this)[i]*(inv<mint>(i+1));
return res;
}
poly ln(){
// a[0] = 1
int n=this->size();
if(n==1) return poly({0});
poly d=derivative();
poly b=*this;
b.pop_back();
poly res=d*b.inverse();
res.resize(n-1);
return res.integral();
}
poly exp(){
// a[0] = 0
int n=this->size();
poly q(1,1);
poly b=*this;
b[0]+=1;
for(int m=1; m<n; m<<=1){
if(n<m*2) b.resize(m*2);
poly g=b.interval(0,m*2),h=q;
h.resize(m*2),h=h.ln();
g-=h;
q*=g;
q.resize(m*2);
}
q.resize(n);
return q;
}
poly pow_naive(ll k){
int n=this->size();
poly b=*this,res={1};
for(; k; b*=b,k>>=1,b.resize(n)) if(k&1) res*=b,res.resize(n);
return res;
}
int low(){
int n=this->size(),m=0;
while(m<n&&(*this)[m]==0) m++;
if(m>=n) return -1;
return m;
}
poly shift(int n){
poly res(n,0);
res.insert(res.end(),this->begin(),this->end());
return res;
}
poly pow(ll k){ // 0^0 = 1
int n=this->size();
if(k==0){
poly res(n);
return res[0]=1,res;
}
int m=low();
if(m){
if(m==-1||k>=n||k*m>=n) return poly(n);
int lft=n-k*m;
poly b=interval(m,m+lft);
b=b.pow(k);
b=b.shift(k*m);
return b;
}
poly b=*this;
mint base=b[0].pow(k),inv=b[0].inv();
b*=inv;
b=b.ln();
if(b.empty()) b.pb(0);
b*=k;
b=b.exp();
b*=base;
return b;
}
poly pow_sparse(int k, int n){ // 0^0 = 1
if(k==0){
poly res(n);
return res[0]=1,res;
}
int t=this->size(),m=low();
if(m){
if(m==-1||k>=n||1ll*k*m>=n) return poly(n);
int lft=n-k*m;
poly b=interval(m,t);
b=b.pow_sparse(k,lft);
b=b.shift(k*m);
return b;
}
poly res(n,0);
res[0]=(*this)[0].pow(k);
mint inv_a0=(*this)[0].inv();
for(int i=1; i<n; ++i){
for(int j=1; j<t; ++j){
if(i-j>=0) res[i]-=res[i-j]*(i-j)*(*this)[j];
}
for(int j=1; j<t; ++j){
if(i-j>=0) res[i]+=res[i-j]*(*this)[j]*j*k;
}
res[i]*=inv_a0*inv<mint>(i);
}
return res;
}
friend ostream& operator << (ostream& os, const poly &P){
int n=P.size();
for(int i=0; i<n; ++i){
os << P[i];
if(i+1<n) os << ' ';
}
return os;
}
};// END: library/poly/poly.hpp
#line 4 "library/poly/linear_recursion.hpp"
template<typename T>
T bostan_mori(poly<T> P, poly<T> Q, ll k){
// [x^k] a/b
for(; k; k>>=1){
poly<T> Q_neg=Q;
for(int i=0; i<(int)Q_neg.size(); ++i){
if(i&1) Q_neg[i]=-Q_neg[i];
}
P*=Q_neg;
Q*=Q_neg;
for(int i=1; i<(int)Q.size(); i+=2){
assert(Q[i]==T(0));
}
poly<T> nw_P,nw_Q;
for(int i=0; i<(int)Q.size(); i+=2){
nw_Q.push_back(Q[i]);
}
for(int i=k&1; i<(int)P.size(); i+=2){
nw_P.push_back(P[i]);
}
P=nw_P,Q=nw_Q;
}
return P[0]/Q[0];
}
template<typename T>
pair<poly<T>,poly<T>> get_genfunc(vector<T> a, vector<T> b){
// linear recursion to rational function
// a: base case
// a_m = \sum_{i=0}^{n-1} b_i*a_{m-1-i}
int n=a.size();
assert(a.size()==b.size());
poly<T> Pa(a),Pb(b);
Pb.insert(Pb.begin(),T(0));
poly<T> P=Pa*Pb-Pa,Q=Pb;
P.resize(n);
Q[0]-=1;
return {P,Q};
}
template<typename T>
T linear_recursion_kth(vector<T> a, vector<T> b, ll k){
int n=a.size();
assert(a.size()==b.size());
if(k<0) return 0;
if(k<n) return a[k];
auto [P,Q]=get_genfunc(a,b);
// a = P/Q
return bostan_mori(P,Q,k);
}
/*
template<typename T>
vector<T> linear_recursion_consecutive(vector<T> a, vector<T> b, ll k, int m){
// a: base case
// a_m = \sum_{i=0}^{n-1} b_i*a_{m-1-i}
// return k-th to (k+m-1)-th term
int n=a.size();
assert(a.size()==b.size());
poly<T> Pa(a),Pb(b);
Pb.insert(Pb.begin(),T(0));
poly<T> diff=Pa*Pb-Pa;
diff.resize(n);
Pb[0]-=1;
Pb.resize(n+m);
diff*=Pb.inverse();
a=vector<T>(diff);
if(k<n){
vector<T> ans(m);
for(int i=0; i<m; ++i){
if(i+k>=0) ans[i]=a[i+k];
}
return ans;
}
poly<T> tar(n+1,1);
for(int i=0; i<n; ++i) tar[i]=-b[n-1-i];
auto get=[&](auto &self, ll _k) -> poly<T>{
if(_k==0) return poly<T>({1});
poly<T> P=self(self,_k>>1);
P*=P;
P=P.divide(tar).second;
if(_k&1){
P*=poly<T>({0,1});
P=P.divide(tar).second;
}
return P;
};
poly<T> res=get(get,k);
assert((int)res.size()==n);
reverse(res.begin(),res.end());
res*=diff;
vector<T> ans(m);
for(int i=0; i<m; ++i) ans[i]=res[i+n-1];
return ans;
}
*/// END: library/poly/linear_recursion.hpp
#line 117 "main.cpp"
void mango(){
ll n; int k; cin >> n >> k;
vc<mint> b(k*2+1);
b[0]=2;
b[k]=-1;
rep(i,1,k+1){
b[k+i]+=1;
}
vc<mint> a(k*2+1,1);
rep(k) a[i]=0;
print(linear_recursion_kth(a,b,n+k*2));
}
signed main(){
ios_base::sync_with_stdio(0),cin.tie(0);
cout << fixed << setprecision(20);
int t=1;
//cin >> t;
while(t--) mango();
}
// END: main.cpp