QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#40497 | #1423. Bin | Crysfly | AC ✓ | 1955ms | 35496kb | C++11 | 4.6kb | 2022-07-22 11:45:41 | 2022-07-22 11:45:42 |
Judging History
answer
#include<bits/stdc++.h>
#define For(i,a,b) for(register int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(register int i=(a);i>=(b);--i)
//#define int long long
using namespace std;
inline int read()
{
char c=getchar();int x=0;bool f=0;
for(;!isdigit(c);c=getchar())f^=!(c^45);
for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
if(f)x=-x;return x;
}
#define mod 998244353
struct modint{
int x;
modint(int o=0){x=o;}
modint &operator = (int o){return x=o,*this;}
modint &operator +=(modint o){return x=x+o.x>=mod?x+o.x-mod:x+o.x,*this;}
modint &operator -=(modint o){return x=x-o.x<0?x-o.x+mod:x-o.x,*this;}
modint &operator *=(modint o){return x=1ll*x*o.x%mod,*this;}
modint &operator ^=(int b){
modint a=*this,c=1;
for(;b;b>>=1,a*=a)if(b&1)c*=a;
return x=c.x,*this;
}
modint &operator /=(modint o){return *this *=o^=mod-2;}
friend modint operator +(modint a,modint b){return a+=b;}
friend modint operator -(modint a,modint b){return a-=b;}
friend modint operator *(modint a,modint b){return a*=b;}
friend modint operator /(modint a,modint b){return a/=b;}
friend modint operator ^(modint a,int b){return a^=b;}
friend bool operator ==(modint a,int b){return a.x==b;}
friend bool operator !=(modint a,int b){return a.x!=b;}
bool operator ! () {return !x;}
modint operator - () {return x?mod-x:0;}
bool operator <(const modint&b)const{return x<b.x;}
};
inline modint qpow(modint x,int y){return x^y;}
vector<modint> fac,ifac,iv;
inline void initC(int n)
{
if(iv.empty())fac=ifac=iv=vector<modint>(2,1);
int m=iv.size(); ++n;
if(m>=n)return;
iv.resize(n),fac.resize(n),ifac.resize(n);
For(i,m,n-1){
iv[i]=iv[mod%i]*(mod-mod/i);
fac[i]=fac[i-1]*i,ifac[i]=ifac[i-1]*iv[i];
}
}
inline modint C(int n,int m){
if(m<0||n<m)return 0;
return initC(n),fac[n]*ifac[m]*ifac[n-m];
}
inline modint sign(int n){return (n&1)?(mod-1):(1);}
#define poly vector<modint>
const modint G=3,Ginv=modint(1)/3;
inline poly one(){poly a;a.push_back(1);return a;}
vector<int>rev;
vector<modint>rts;
inline int ext(int n){
int k=0;
while((1<<k)<n)++k;return k;
}
inline void init(int k){
int n=1<<k;
if(rev.size()==n)return;
rev.resize(n);
For(i,0,n-1)rev[i]=(rev[i>>1]>>1)|((i&1)<<(k-1));
if(rts.size()>=n)return;
int lst=max(1,(int)rts.size()); rts.resize(n);
for(int mid=lst;mid<n;mid<<=1){
modint wn=G^((mod-1)/(mid<<1));
rts[mid]=1;
For(i,1,mid-1)rts[i+mid]=rts[i+mid-1]*wn;
}
}
void ntt(poly&a,int k,int typ)
{
int n=1<<k;
if(typ<0) reverse(a.begin()+1,a.end());
For(i,0,n-1)if(i<rev[i])swap(a[i],a[rev[i]]);
for(int mid=1;mid<n;mid<<=1)
for(int r=mid<<1,j=0;j<n;j+=r)
for(int k=0;k<mid;++k){
modint x=a[j+k],y=rts[mid+k]*a[j+k+mid];
a[j+k]=x+y,a[j+k+mid]=x-y;
}
if(typ<0){
modint inv=modint(1)/n;
For(i,0,n-1)a[i]*=inv;
}
}
poly operator +(poly a,poly b){
int n=max(a.size(),b.size());a.resize(n),b.resize(n);
For(i,0,n-1)a[i]+=b[i];return a;
}
poly operator -(poly a,poly b){
int n=max(a.size(),b.size());a.resize(n),b.resize(n);
For(i,0,n-1)a[i]-=b[i];return a;
}
poly operator *(poly a,modint b){
int n=a.size();
For(i,0,n-1)a[i]*=b;return a;
}
poly operator *(poly a,poly b)
{
if((int)a.size()<=64 && (int)b.size()<=64){
poly c(a.size()+b.size()-1,0);
for(int i=0;i<a.size();++i)
for(int j=0;j<b.size();++j)
c[i+j]+=a[i]*b[j];
return c;
}
int n=(int)a.size()+(int)b.size()-1,k=ext(n);
a.resize(1<<k),b.resize(1<<k),init(k);
ntt(a,k,1),ntt(b,k,1);
For(i,0,(1<<k)-1)a[i]*=b[i];
ntt(a,k,-1),a.resize(n);return a;
}
#define fi first
#define se second
#define mkp make_pair
#define pb push_back
typedef pair<int,int>pii;
typedef vector<int>vi;
#define maxn 2000005
#define inf 0x3f3f3f3f
int n,k;
modint f[maxn],inv2=(mod+1)/2;
void solve(int l,int r)
{
if(l==r)return;
int mid=l+r>>1;
solve(l,mid);
if(l==1){
poly a;
a.resize(mid+1);
For(i,1,mid)a[i]=f[i];
a=a*a;
For(i,1,mid)a[i*2]+=f[i]*f[i];
For(i,mid+1,r)f[i]+=a[i]*inv2;
For(i,1,mid)
For(j,i+1,min(i+k,mid))
if(i+j>mid && i+j<=r)
f[i+j]+=f[i]*f[j];
}else{
poly a,b;
a.resize(mid-l+1),b.resize(r-l+1);
For(i,l,mid)a[i-l]=f[i];
For(i,0,r-l)b[i]=f[i];
a=a*b;
For(i,mid+1,r)f[i]+=a[i-l];
Rep(i,r-l,max(1,l-k))
For(j,l,min(mid,i+k))
if(i+j>mid && i+j<=r)
f[i+j]+=f[i]*f[j];
}
solve(mid+1,r);
}
signed main()
{
n=read(),k=read();
f[1]=1,solve(1,n);
cout<<f[n].x;
return 0;
}
/*
j<=i f[i+j]+=f[i]*f[j]
i<j<=i+k f[i+j]+=f[i]*f[j]
f[1]=1;
For(i,1,n){
For(j,1,min(i-1,(i+k)/2))
f[i]+=f[j]*f[i-j];
}
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 11436kb
input:
2 0
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 5ms
memory: 11544kb
input:
3 0
output:
1
result:
ok 1 number(s): "1"
Test #3:
score: 0
Accepted
time: 2ms
memory: 11500kb
input:
3 1
output:
2
result:
ok 1 number(s): "2"
Test #4:
score: 0
Accepted
time: 1ms
memory: 11364kb
input:
4 0
output:
2
result:
ok 1 number(s): "2"
Test #5:
score: 0
Accepted
time: 4ms
memory: 11368kb
input:
4 1
output:
3
result:
ok 1 number(s): "3"
Test #6:
score: 0
Accepted
time: 2ms
memory: 11388kb
input:
4 2
output:
5
result:
ok 1 number(s): "5"
Test #7:
score: 0
Accepted
time: 0ms
memory: 11504kb
input:
6 2
output:
23
result:
ok 1 number(s): "23"
Test #8:
score: 0
Accepted
time: 4ms
memory: 11436kb
input:
7 42
output:
132
result:
ok 1 number(s): "132"
Test #9:
score: 0
Accepted
time: 2ms
memory: 11440kb
input:
10 1
output:
400
result:
ok 1 number(s): "400"
Test #10:
score: 0
Accepted
time: 4ms
memory: 11508kb
input:
13 4
output:
42003
result:
ok 1 number(s): "42003"
Test #11:
score: 0
Accepted
time: 5ms
memory: 11376kb
input:
239 17
output:
385818773
result:
ok 1 number(s): "385818773"
Test #12:
score: 0
Accepted
time: 75ms
memory: 12352kb
input:
50216 58
output:
744498776
result:
ok 1 number(s): "744498776"
Test #13:
score: 0
Accepted
time: 1822ms
memory: 34064kb
input:
787788 78
output:
394429402
result:
ok 1 number(s): "394429402"
Test #14:
score: 0
Accepted
time: 2ms
memory: 11460kb
input:
5 92
output:
14
result:
ok 1 number(s): "14"
Test #15:
score: 0
Accepted
time: 2ms
memory: 11460kb
input:
88 79
output:
931161641
result:
ok 1 number(s): "931161641"
Test #16:
score: 0
Accepted
time: 0ms
memory: 11484kb
input:
749 77
output:
615055472
result:
ok 1 number(s): "615055472"
Test #17:
score: 0
Accepted
time: 2ms
memory: 11416kb
input:
2281 89
output:
282226588
result:
ok 1 number(s): "282226588"
Test #18:
score: 0
Accepted
time: 10ms
memory: 11688kb
input:
5765 35
output:
293680396
result:
ok 1 number(s): "293680396"
Test #19:
score: 0
Accepted
time: 36ms
memory: 12312kb
input:
43189 12
output:
805295564
result:
ok 1 number(s): "805295564"
Test #20:
score: 0
Accepted
time: 1754ms
memory: 34228kb
input:
806855 5
output:
593114139
result:
ok 1 number(s): "593114139"
Test #21:
score: 0
Accepted
time: 1873ms
memory: 35496kb
input:
994514 45
output:
678426421
result:
ok 1 number(s): "678426421"
Test #22:
score: 0
Accepted
time: 1883ms
memory: 35408kb
input:
999921 62
output:
752162081
result:
ok 1 number(s): "752162081"
Test #23:
score: 0
Accepted
time: 1918ms
memory: 35284kb
input:
995033 94
output:
130314865
result:
ok 1 number(s): "130314865"
Test #24:
score: 0
Accepted
time: 1874ms
memory: 34788kb
input:
901438 97
output:
809128292
result:
ok 1 number(s): "809128292"
Test #25:
score: 0
Accepted
time: 1810ms
memory: 35368kb
input:
993020 0
output:
926771801
result:
ok 1 number(s): "926771801"
Test #26:
score: 0
Accepted
time: 1955ms
memory: 35200kb
input:
1000000 100
output:
470305140
result:
ok 1 number(s): "470305140"