QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#56933 | #4812. Counting Sequence | KING_UT | AC ✓ | 15369ms | 229188kb | C++20 | 2.4kb | 2022-10-21 22:04:40 | 2022-10-21 22:06:38 |
Judging History
answer
#ifndef LOCAL
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#endif
#include <bits/stdc++.h>
#define SIZE 40005
#define MX 300
#define MX2 2200
using namespace std;
using ll=long long;
using uint=unsigned;
const uint mod=998244353;
struct mint{
uint v;
mint(ll vv=0){s(vv%mod+mod);}
mint& s(uint vv){
v=vv<mod?vv:vv-mod;
return *this;
}
mint operator -()const{return mint()-*this;}
mint& operator+=(const mint&r){return s(v+r.v);}
mint& operator-=(const mint&r){return s(v+mod-r.v);}
mint& operator*=(const mint&r){v=(unsigned ll)v*r.v%mod;return *this;}
mint& operator/=(const mint&r){return *this*=r.inv();}
mint operator+(const mint&r)const{return mint(*this)+=r;}
mint operator-(const mint&r)const{return mint(*this)-=r;}
mint operator*(const mint&r)const{return mint(*this)*=r;}
mint operator/(const mint&r)const{return mint(*this)/=r;}
mint pow(ll n)const{
if(n<0)return inv().pow(-n);
mint res(1),x(*this);
while(n){
if(n&1)res*=x;
x*=x;
n>>=1;
}
return res;
}
mint inv()const{return pow(mod-2);}
bool operator<(mint r)const{return v<r.v;}
};
mint dp1[2][SIZE][MX],dp2[2][SIZE][MX];
mint dp[MX2][MX2],bef[MX2][MX2];
int main(){
int n,c;
scanf("%d%d",&n,&c);
int k=min(n,225);
int pos=0;
mint ret=0;
for(int L=1;L<=k;L++){
int mx=min(n,L*(L+1)/2);
pos=1-pos;
if(L==1){
dp1[pos][1][1]=1;
} else{
for(int s=1;s<=mx;s++){
for(int i=1;i<=min(s,L);i++){
if(i>1) dp1[pos][s][i]=dp1[pos^1][s-i][i+1]*c;
else dp1[pos][s][i]=0;
if(i>1&&(L==2||i>2)) dp1[pos][s][i]+=dp1[pos^1][s-i][i-1];
dp2[pos][s][i]=dp2[pos^1][s-i][i+1];
if(i>1) dp2[pos][s][i]+=dp2[pos^1][s-i][i-1]*c;
}
}
}
for(int s=1;s<=mx;s++){
for(int i=1;i<=min(s,L);i++){
dp2[pos][s][1]+=dp1[pos][s][i];
}
if((n-s)%L==0){
for(int i=1;i<=min(s,L);i++){
ret+=dp2[pos][s][i];
}
}
}
}
int mx=n/(k+1)+k;
//while((k+1)*(2*mx-k)/2>n) mx--;
//mx+=2;
for(int s=k;s<=n;s++){
int id=s%(mx+1);
for(int i=1;i<=min(mx,s);i++){
dp[id][i]=dp[(s-i+mx+1)%(mx+1)][i+1];
if(i>1) dp[id][i]+=dp[(s-i+mx+1)%(mx+1)][i-1]*c;
if(i<=k&&s<=k*(k+1)/2) bef[id][i]=dp2[pos][s][i];
else bef[id][i]=0;
bef[id][i]+=bef[(s-k+mx+1)%(mx+1)][i-1];
dp[id][i]+=bef[(s-i+mx+1)%(mx+1)][i+1];
if(i>1) dp[id][i]+=bef[(s-i+mx+1)%(mx+1)][i-1]*c;
if(s==n) ret+=dp[id][i];
}
}
printf("%u\n",ret.v);
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 31ms
memory: 229096kb
input:
5 3
output:
8
result:
ok 1 number(s): "8"
Test #2:
score: 0
Accepted
time: 44ms
memory: 229040kb
input:
1 0
output:
1
result:
ok 1 number(s): "1"
Test #3:
score: 0
Accepted
time: 449ms
memory: 229088kb
input:
2022 39
output:
273239559
result:
ok 1 number(s): "273239559"
Test #4:
score: 0
Accepted
time: 32ms
memory: 229028kb
input:
1 998244352
output:
1
result:
ok 1 number(s): "1"
Test #5:
score: 0
Accepted
time: 40ms
memory: 229032kb
input:
1 12345678
output:
1
result:
ok 1 number(s): "1"
Test #6:
score: 0
Accepted
time: 59ms
memory: 229184kb
input:
20 998998
output:
643731701
result:
ok 1 number(s): "643731701"
Test #7:
score: 0
Accepted
time: 24ms
memory: 229024kb
input:
23 123
output:
947753998
result:
ok 1 number(s): "947753998"
Test #8:
score: 0
Accepted
time: 32ms
memory: 229096kb
input:
50 5555
output:
745339864
result:
ok 1 number(s): "745339864"
Test #9:
score: 0
Accepted
time: 31ms
memory: 229188kb
input:
60 6666
output:
690992218
result:
ok 1 number(s): "690992218"
Test #10:
score: 0
Accepted
time: 32ms
memory: 229184kb
input:
100 50
output:
169678588
result:
ok 1 number(s): "169678588"
Test #11:
score: 0
Accepted
time: 113ms
memory: 229112kb
input:
500 88888
output:
216149701
result:
ok 1 number(s): "216149701"
Test #12:
score: 0
Accepted
time: 226ms
memory: 229024kb
input:
1000 213456
output:
270989457
result:
ok 1 number(s): "270989457"
Test #13:
score: 0
Accepted
time: 418ms
memory: 229176kb
input:
2000 119988
output:
756425375
result:
ok 1 number(s): "756425375"
Test #14:
score: 0
Accepted
time: 651ms
memory: 229096kb
input:
3000 998244352
output:
71841227
result:
ok 1 number(s): "71841227"
Test #15:
score: 0
Accepted
time: 648ms
memory: 229028kb
input:
3000 555555555
output:
79880116
result:
ok 1 number(s): "79880116"
Test #16:
score: 0
Accepted
time: 897ms
memory: 229096kb
input:
4321 1234
output:
949603993
result:
ok 1 number(s): "949603993"
Test #17:
score: 0
Accepted
time: 1058ms
memory: 229024kb
input:
5000 0
output:
5
result:
ok 1 number(s): "5"
Test #18:
score: 0
Accepted
time: 1080ms
memory: 229104kb
input:
5000 88888777
output:
833064960
result:
ok 1 number(s): "833064960"
Test #19:
score: 0
Accepted
time: 1038ms
memory: 229120kb
input:
5000 35557777
output:
696388498
result:
ok 1 number(s): "696388498"
Test #20:
score: 0
Accepted
time: 2313ms
memory: 229088kb
input:
10000 123456
output:
434296902
result:
ok 1 number(s): "434296902"
Test #21:
score: 0
Accepted
time: 4351ms
memory: 229108kb
input:
20000 555555
output:
34806915
result:
ok 1 number(s): "34806915"
Test #22:
score: 0
Accepted
time: 4628ms
memory: 229184kb
input:
30000 777888999
output:
58443551
result:
ok 1 number(s): "58443551"
Test #23:
score: 0
Accepted
time: 4802ms
memory: 229088kb
input:
50000 2
output:
90102905
result:
ok 1 number(s): "90102905"
Test #24:
score: 0
Accepted
time: 5163ms
memory: 229188kb
input:
70000 77998866
output:
202638568
result:
ok 1 number(s): "202638568"
Test #25:
score: 0
Accepted
time: 5749ms
memory: 228972kb
input:
100000 998244352
output:
360520717
result:
ok 1 number(s): "360520717"
Test #26:
score: 0
Accepted
time: 5722ms
memory: 229140kb
input:
100000 555555555
output:
613886009
result:
ok 1 number(s): "613886009"
Test #27:
score: 0
Accepted
time: 7188ms
memory: 228972kb
input:
150000 233333
output:
381065878
result:
ok 1 number(s): "381065878"
Test #28:
score: 0
Accepted
time: 7143ms
memory: 228972kb
input:
150000 20050117
output:
269891864
result:
ok 1 number(s): "269891864"
Test #29:
score: 0
Accepted
time: 9123ms
memory: 229100kb
input:
200000 114514
output:
262861613
result:
ok 1 number(s): "262861613"
Test #30:
score: 0
Accepted
time: 9246ms
memory: 229144kb
input:
200000 1919810
output:
77872388
result:
ok 1 number(s): "77872388"
Test #31:
score: 0
Accepted
time: 15149ms
memory: 229116kb
input:
300000 0
output:
12
result:
ok 1 number(s): "12"
Test #32:
score: 0
Accepted
time: 14854ms
memory: 229104kb
input:
300000 1
output:
298803948
result:
ok 1 number(s): "298803948"
Test #33:
score: 0
Accepted
time: 14943ms
memory: 229100kb
input:
300000 2
output:
106751203
result:
ok 1 number(s): "106751203"
Test #34:
score: 0
Accepted
time: 14891ms
memory: 229116kb
input:
300000 1234
output:
427045479
result:
ok 1 number(s): "427045479"
Test #35:
score: 0
Accepted
time: 14735ms
memory: 228972kb
input:
300000 2345
output:
441593553
result:
ok 1 number(s): "441593553"
Test #36:
score: 0
Accepted
time: 14681ms
memory: 229024kb
input:
300000 20041115
output:
580367993
result:
ok 1 number(s): "580367993"
Test #37:
score: 0
Accepted
time: 14962ms
memory: 229108kb
input:
300000 20050117
output:
579859619
result:
ok 1 number(s): "579859619"
Test #38:
score: 0
Accepted
time: 15369ms
memory: 229112kb
input:
300000 22223333
output:
596066085
result:
ok 1 number(s): "596066085"
Test #39:
score: 0
Accepted
time: 15220ms
memory: 229144kb
input:
300000 175846372
output:
660364393
result:
ok 1 number(s): "660364393"
Test #40:
score: 0
Accepted
time: 15275ms
memory: 229096kb
input:
299999 9999999
output:
954865020
result:
ok 1 number(s): "954865020"
Test #41:
score: 0
Accepted
time: 14991ms
memory: 229176kb
input:
299998 55556666
output:
904862432
result:
ok 1 number(s): "904862432"
Test #42:
score: 0
Accepted
time: 8849ms
memory: 229108kb
input:
190733 31756136
output:
880544587
result:
ok 1 number(s): "880544587"
Test #43:
score: 0
Accepted
time: 9142ms
memory: 229084kb
input:
198497 463488298
output:
185220207
result:
ok 1 number(s): "185220207"
Test #44:
score: 0
Accepted
time: 14868ms
memory: 229100kb
input:
299997 0
output:
16
result:
ok 1 number(s): "16"
Test #45:
score: 0
Accepted
time: 7810ms
memory: 229184kb
input:
168168 296157813
output:
798716760
result:
ok 1 number(s): "798716760"
Test #46:
score: 0
Accepted
time: 6171ms
memory: 229064kb
input:
114514 1919810
output:
783513290
result:
ok 1 number(s): "783513290"