QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#32301#1423. BinWu_RenAC ✓1737ms48660kbC++112.3kb2022-05-18 22:15:392022-05-18 22:15:41

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-05-18 22:15:41]
  • 评测
  • 测评结果:AC
  • 用时:1737ms
  • 内存:48660kb
  • [2022-05-18 22:15:39]
  • 提交

answer

#include <bits/stdc++.h>
const int mod=998244353,G=3,inv2=(mod+1)/2,U=2097160;
typedef unsigned long long ull;
using namespace std;
int n,k,f[1000010],A[U],B[U];
int lim,len,w[2][U],rev[U];
void qmo(int &x){
	x+=(x>>31)&mod;
}
int ksm(int a,int k){
	int res=1;
	for(;k;k>>=1,a=1ll*a*a%mod) if(k&1) res=1ll*res*a%mod;
	return res;
}
void calc(int N){
	for(lim=1,len=0;lim<=N;lim<<=1,len++);
	for(int i=0;i<lim;i++) rev[i]=rev[i>>1]>>1|((i&1)<<(len-1)); 
}
void init(int N){
	calc(N);
	for(int mid=1;mid<lim;mid<<=1){
		w[0][mid]=w[1][mid]=1,w[0][mid+1]=ksm(G,(mod-1)/(mid<<1));
		for(int j=2;j<mid;j++) w[0][mid+j]=1ll*w[0][mid+j-1]*w[0][mid+1]%mod;
		for(int j=1;j<mid;j++) w[1][mid+j]=mod-w[0][2*mid-j];
	}
}
void NTT(int *a,int fl){
	static ull A[U];
	for(int i=0;i<lim;i++) A[i]=a[i];
	for(int i=0;i<lim;i++) if(i<rev[i]) swap(A[i],A[rev[i]]);
	for(int mid=1;mid<lim;mid<<=1){
		for(int j=0;j<lim;j+=(mid<<1)){
			for(int k=0;k<mid;k++){
				ull x=A[j+k],y=w[fl][mid+k]*A[mid+j+k]%mod;
				A[j+k]=x+y,A[mid+j+k]=x-y+mod;
			}
		}
	}
	for(int i=0;i<lim;i++) a[i]=A[i]%mod;
	if(fl>0) return;
	int inv=ksm(lim,mod-2);
	for(int i=0;i<lim;i++) a[i]=1ll*a[i]*inv%mod;
}
void sol(int l,int r){
	if(l==r) return;
	int mid=(l+r)>>1;
	sol(l,mid);
	if(l==1){
		for(int i=1;i<=mid;i++) A[i]=f[i];
		calc(2*mid);
		NTT(A,1);
		for(int i=0;i<lim;i++) A[i]=1ll*A[i]*A[i]%mod;
		NTT(A,0);
		for(int i=1;i<=mid;i++) A[2*i]=(A[2*i]+1ll*f[i]*f[i])%mod;
		for(int i=0;i<lim;i++) A[i]=1ll*A[i]*inv2%mod;
		for(int i=mid+1;i<=r;i++) qmo(f[i]+=A[i]-mod);
		for(int i=1;i<=mid;i++) for(int j=i+1;j<=i+k&&j<=mid;j++){
			if(i+j>mid&&i+j<=r) f[i+j]=(f[i+j]+1ll*f[i]*f[j])%mod;
		}
		for(int i=0;i<lim;i++) A[i]=0;
	}
	else{
		assert(r-l<l);
		for(int i=l;i<=mid;i++) A[i-l]=f[i];
		for(int i=0;i<=r-l;i++) B[i]=f[i];
		calc(r-l+mid-l);
		for(int i=r-l;i>=0&&i+k>=l;i--) for(int j=l;j<=mid&&i+k>=j;j++){
			if(i+j>mid&&i+j<=r) f[i+j]=(f[i+j]+1ll*f[i]*f[j])%mod;
		}
		NTT(A,1),NTT(B,1);
		for(int i=0;i<lim;i++) A[i]=1ll*A[i]*B[i]%mod;
		NTT(A,0);
		for (int i=mid+1;i<=r;i++) qmo(f[i]+=A[i-l]-mod);
		for(int i=0;i<lim;i++) A[i]=B[i]=0;
	}
	sol(mid+1,r);
}
int main(){
	scanf("%d%d",&n,&k);
	init(2*n);
	f[1]=1;
	sol(1,n);
	printf("%d\n",f[n]);
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 14116kb

input:

2 0

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: 0
Accepted
time: 2ms
memory: 14048kb

input:

3 0

output:

1

result:

ok 1 number(s): "1"

Test #3:

score: 0
Accepted
time: 0ms
memory: 14012kb

input:

3 1

output:

2

result:

ok 1 number(s): "2"

Test #4:

score: 0
Accepted
time: 2ms
memory: 14048kb

input:

4 0

output:

2

result:

ok 1 number(s): "2"

Test #5:

score: 0
Accepted
time: 0ms
memory: 14028kb

input:

4 1

output:

3

result:

ok 1 number(s): "3"

Test #6:

score: 0
Accepted
time: 2ms
memory: 14008kb

input:

4 2

output:

5

result:

ok 1 number(s): "5"

Test #7:

score: 0
Accepted
time: 2ms
memory: 14048kb

input:

6 2

output:

23

result:

ok 1 number(s): "23"

Test #8:

score: 0
Accepted
time: 2ms
memory: 13896kb

input:

7 42

output:

132

result:

ok 1 number(s): "132"

Test #9:

score: 0
Accepted
time: 0ms
memory: 14012kb

input:

10 1

output:

400

result:

ok 1 number(s): "400"

Test #10:

score: 0
Accepted
time: 1ms
memory: 14100kb

input:

13 4

output:

42003

result:

ok 1 number(s): "42003"

Test #11:

score: 0
Accepted
time: 5ms
memory: 14008kb

input:

239 17

output:

385818773

result:

ok 1 number(s): "385818773"

Test #12:

score: 0
Accepted
time: 69ms
memory: 14800kb

input:

50216 58

output:

744498776

result:

ok 1 number(s): "744498776"

Test #13:

score: 0
Accepted
time: 1607ms
memory: 47916kb

input:

787788 78

output:

394429402

result:

ok 1 number(s): "394429402"

Test #14:

score: 0
Accepted
time: 2ms
memory: 14032kb

input:

5 92

output:

14

result:

ok 1 number(s): "14"

Test #15:

score: 0
Accepted
time: 2ms
memory: 14012kb

input:

88 79

output:

931161641

result:

ok 1 number(s): "931161641"

Test #16:

score: 0
Accepted
time: 1ms
memory: 14036kb

input:

749 77

output:

615055472

result:

ok 1 number(s): "615055472"

Test #17:

score: 0
Accepted
time: 2ms
memory: 14076kb

input:

2281 89

output:

282226588

result:

ok 1 number(s): "282226588"

Test #18:

score: 0
Accepted
time: 11ms
memory: 14128kb

input:

5765 35

output:

293680396

result:

ok 1 number(s): "293680396"

Test #19:

score: 0
Accepted
time: 36ms
memory: 14792kb

input:

43189 12

output:

805295564

result:

ok 1 number(s): "805295564"

Test #20:

score: 0
Accepted
time: 1578ms
memory: 48308kb

input:

806855 5

output:

593114139

result:

ok 1 number(s): "593114139"

Test #21:

score: 0
Accepted
time: 1680ms
memory: 48656kb

input:

994514 45

output:

678426421

result:

ok 1 number(s): "678426421"

Test #22:

score: 0
Accepted
time: 1672ms
memory: 48640kb

input:

999921 62

output:

752162081

result:

ok 1 number(s): "752162081"

Test #23:

score: 0
Accepted
time: 1737ms
memory: 48600kb

input:

995033 94

output:

130314865

result:

ok 1 number(s): "130314865"

Test #24:

score: 0
Accepted
time: 1671ms
memory: 48520kb

input:

901438 97

output:

809128292

result:

ok 1 number(s): "809128292"

Test #25:

score: 0
Accepted
time: 1637ms
memory: 48660kb

input:

993020 0

output:

926771801

result:

ok 1 number(s): "926771801"

Test #26:

score: 0
Accepted
time: 1720ms
memory: 48636kb

input:

1000000 100

output:

470305140

result:

ok 1 number(s): "470305140"