QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#455222 | #5697. 萨菲克斯·阿瑞 | rzh123 | 100 ✓ | 675ms | 7632kb | C++20 | 4.1kb | 2024-06-25 21:14:38 | 2024-06-25 21:14:39 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
constexpr int N=505,P=1e9+7;
int n,m,c[N],fac[N],fi[N];
int f[2][N][N],ad[N][N],fs[N][N];
constexpr inline int qp(int a,int b){
int c{1};
for(;b;a=1ll*a*a%P,b>>=1) if(b&1) c=1ll*c*a%P;
return c;
}
constexpr inline int inv(int x){return qp(x,P-2);}
inline void add(int &a,int b){
if((a+=b)>=P) a-=P;
}
inline int cc(int n,int m){
if(m<0||n<m) return 0;
return 1ll*fac[n]*fi[n-m]%P*fi[m]%P;
}
int main(){
//ifstream cin("suffix.in"); ofstream cout("suffix.out");
cin.tie(0)->sync_with_stdio(0);
int m0;
cin>>n>>m0;
while(m0--){
int t; cin>>t;
if(t) c[++m]=t;
}
fac[0]=1; for(int i{1};i<N;++i) fac[i]=1ll*fac[i-1]*i%P;
fi[N-1]=inv(fac[N-1]); for(int i{N-2};i>=0;--i) fi[i]=1ll*fi[i+1]*(i+1)%P;
f[0][0][0]=1;
int ans{0};
for(int i{1};i<=m;++i){
memset(ad,0,sizeof ad),memset(fs,0,sizeof fs);
for(int j{0};j<=n;++j) for(int k{0};k<=n;++k){
if(j>0) fs[j][k]=fs[j-1][k];
add(fs[j][k],f[!(i&1)][j][k]);
}
for(int jl{1};jl<=n;++jl){
for(int k{0};k<=jl;++k){
if(jl-c[i]<=0)
add(f[i&1][jl][jl],1ll*fs[jl-1][k]*fi[jl-k]%P);
else
add(f[i&1][jl][jl],1ll*(fs[jl-1][k]-fs[max(0,jl-c[i]-1)][k]+P)*fi[jl-k]%P);
}
}
for(int j{0};j<=n;++j){
for(int k{0};k<=n;++k){
int &me{f[!(i&1)][j][k]};
if(!me) continue;
if(j+c[i]<=n) add(f[i&1][j+c[i]][k],me);
int lmx=min(c[i],n-j);
if(lmx>=1){
add(ad[j+1][k],(P-me)%P);
if(j+lmx+1<=n) add(ad[j+lmx+1][k],me);
}
me=0;
}
}
for(int j{1};j<=n;++j){
for(int k{0};k<=n;++k){
if(j>0) add(ad[j][k],ad[j-1][k]);
add(f[i&1][j][k],ad[j][k]);
}
}
add(ans,f[i&1][n][n]);
}
ans=1ll*ans*fac[n]%P;
cout<<ans<<'\n';
return 0;
}
/*
bananas:
2 4 6 1 3 5 7
ananans
anas
as
bananas
nanas
nas
s
考虑判定后缀数组
直接考虑定义,suf(sa[i])<suf(sa[i+1])
(定义 rk[n+1]=0)
s[sa[i]]<=s[sa[i+1]] (rk[sa[i]+1]<rk[sa[i+1]+1])
s[sa[i]]<s[sa[i+1]] (rk[sa[i]+1]>rk[sa[i+1]+1])
显然是充要条件
每个后缀数组对应一个不等式链 s[sa[1]]<(=)s[sa[2]]<(=)s[sa[3]]<(=)...<(=)s[sa[n]]
判定后缀数组合法:构造不等式链,判定不等式链是否能构造出来(合法)
判定一个不等式链合法:
如果没有 c 的限制,只需要 m>=cnt(<)+1
有,根据 < 分段,从左往右贪心,每段尽量用当前字符填满,填不满就全填上到后一个字符
统计有多少个 sa 对应某个不等式链:
定义"秩":最少的字符种类数,使得可以构造出这个后缀数组
后缀数组的秩等于不等式链中 < 的个数+1=分成的段数
考虑统计秩为 m 的满足某个不等式链后缀数组数量
用 m 种字符构造的后缀数组,秩 <=m
(或说:考虑每一段 <= 的字符都相同的情况,满足这个条件的每个字符串都对应不同的后缀数组)
方案为 C(n,len1,len2,len3,...,len[k])=C(n,len1)*C(n-len1,len2)*C(n-len1-len2,len3)*...*C(n-len1-len2-...-len[k-1],len[k])
这样需要减去 <m 的
枚举哪个应该为 < 的地方实际是 <=
例如:[len1] < [len2] < [len3]
如果第一个 < 是 <=,方案 C(n,len1+len2,len3)
第二个是,方案 C(n,len1,len2+len3)
多减了都是 < 的,C(n,len1+len2+len3)
。。。
容斥,cnt[秩为 m] = sum (i=1..m) sum(a[1..i],s.t. sum a=n) (-1)^(m-k)*C(n,a[1],a[2],...,a[i])
(约分) = sum(...) (-1)^(m-k)*fac[n]*prod fi[a[i]]
= fac[n]*sum(...) (-1)^(m-k)*prod fi[a[i]]
原问题:
考虑统计秩=1..m 的后缀数组数量
有 c 的限制,不影响,多种字符可以合并成一段(但是一种字符不能分到多段)
dp,
f[i][j][k]: 考虑了前 i 种字符,当前填了的所有段的长度和为 j,上一个 < 在第 k 个字符之后,对答案的贡献
拼接在当前段之后,不结束当前段:f[i][j+c[i]][k]+=f[i-1][j][k]
拼接在当前段之后并结束,填 <:f[i][j+l][j+l]+=fi[j+l-k]*f[i-1][j][k]
拼接在当前段之后并结束,填 <=:f[i][j+l][k]+=(-1)*f[i-1][j][k]
ans=fac[n]*sum(i=1..m) f[i][n][n]
O(n^3m)
用前缀和和差分优化 O(n^2m)
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 5
Accepted
time: 1ms
memory: 6628kb
input:
6 3 2 3 4
output:
270
result:
ok 1 number(s): "270"
Test #2:
score: 5
Accepted
time: 1ms
memory: 5960kb
input:
10 7 1 3 4 2 8 4 4
output:
3395650
result:
ok 1 number(s): "3395650"
Test #3:
score: 5
Accepted
time: 1ms
memory: 6136kb
input:
10 7 5 2 4 7 2 9 9
output:
3521671
result:
ok 1 number(s): "3521671"
Test #4:
score: 5
Accepted
time: 0ms
memory: 7576kb
input:
490 2 397 150
output:
156076820
result:
ok 1 number(s): "156076820"
Test #5:
score: 5
Accepted
time: 5ms
memory: 7592kb
input:
499 3 365 104 84
output:
359276756
result:
ok 1 number(s): "359276756"
Test #6:
score: 5
Accepted
time: 355ms
memory: 7592kb
input:
495 181 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 495 ...
output:
367030087
result:
ok 1 number(s): "367030087"
Test #7:
score: 5
Accepted
time: 347ms
memory: 7616kb
input:
497 174 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 497 ...
output:
842700950
result:
ok 1 number(s): "842700950"
Test #8:
score: 5
Accepted
time: 0ms
memory: 7276kb
input:
47 37 24 47 34 37 32 25 42 33 27 47 47 23 35 46 41 27 2 0 2 3 0 2 4 0 2 1 2 1 4 1 3 44 44 35 25 24 26
output:
997241548
result:
ok 1 number(s): "997241548"
Test #9:
score: 5
Accepted
time: 4ms
memory: 7020kb
input:
50 33 44 32 32 31 40 38 37 35 29 29 30 25 40 45 36 46 36 4 1 2 1 2 3 5 5 5 4 3 4 2 5 3 29
output:
985463988
result:
ok 1 number(s): "985463988"
Test #10:
score: 5
Accepted
time: 4ms
memory: 6400kb
input:
45 35 40 28 26 34 3 4 1 4 2 1 4 2 4 4 4 3 0 1 1 37 22 33 23 30 39 39 30 39 44 31 29 38 44 39 23
output:
246027250
result:
ok 1 number(s): "246027250"
Test #11:
score: 5
Accepted
time: 52ms
memory: 7192kb
input:
192 149 123 184 177 147 156 148 141 158 185 172 100 150 134 157 156 127 140 128 130 156 139 130 105 172 162 119 122 174 166 189 107 189 104 172 168 163 99 100 113 188 176 157 139 167 166 185 143 105 148 130 187 115 145 163 140 186 145 139 185 166 161 178 185 124 153 182 191 174 136 133 170 106 154 1...
output:
18950292
result:
ok 1 number(s): "18950292"
Test #12:
score: 5
Accepted
time: 51ms
memory: 6460kb
input:
199 145 176 134 152 179 103 162 197 126 112 100 173 106 177 165 173 148 189 180 157 175 132 105 180 190 190 120 152 130 196 193 147 194 121 159 118 145 149 149 145 171 171 194 151 193 187 107 132 189 185 146 139 130 179 155 180 171 157 104 171 101 119 189 120 165 124 136 163 150 196 195 161 187 128 ...
output:
211123272
result:
ok 1 number(s): "211123272"
Test #13:
score: 5
Accepted
time: 52ms
memory: 6768kb
input:
198 153 124 132 193 111 184 197 150 163 195 169 99 162 112 153 169 165 183 175 136 192 161 171 180 12 19 2 17 9 11 18 0 12 17 4 2 12 9 7 109 181 1 13 7 16 9 5 5 12 12 16 6 10 6 15 5 102 113 101 126 157 177 189 191 134 124 145 126 170 144 122 192 110 161 183 196 167 127 161 150 188 188 111 158 194 18...
output:
921172591
result:
ok 1 number(s): "921172591"
Test #14:
score: 5
Accepted
time: 52ms
memory: 7324kb
input:
191 159 133 115 96 114 146 165 138 121 124 100 164 188 138 142 150 115 137 139 186 164 149 109 191 145 140 120 125 162 142 191 101 155 117 150 182 184 186 137 97 120 173 177 120 100 168 182 110 115 174 96 150 162 127 133 181 155 176 188 183 167 125 163 172 125 124 105 149 172 163 179 119 149 147 106...
output:
174115008
result:
ok 1 number(s): "174115008"
Test #15:
score: 5
Accepted
time: 675ms
memory: 7624kb
input:
486 391 416 380 330 466 464 404 405 399 345 362 440 413 461 469 442 250 379 281 333 376 417 453 470 450 353 324 427 323 345 293 378 373 470 328 420 408 374 373 248 470 286 328 457 276 444 474 466 328 322 456 435 420 12 26 25 16 35 35 35 5 42 3 23 15 7 17 21 479 381 401 289 463 339 289 374 332 275 33...
output:
61471107
result:
ok 1 number(s): "61471107"
Test #16:
score: 5
Accepted
time: 596ms
memory: 7632kb
input:
482 370 455 359 363 398 476 318 462 466 476 41 7 1 5 6 28 35 16 35 12 19 31 16 37 11 261 312 473 281 269 334 378 461 453 246 266 430 470 281 407 251 280 268 334 344 254 437 328 316 287 314 357 423 466 341 360 299 465 297 450 351 261 329 408 481 340 433 254 265 395 282 383 383 295 455 375 398 328 309...
output:
656550309
result:
ok 1 number(s): "656550309"
Test #17:
score: 5
Accepted
time: 589ms
memory: 7504kb
input:
486 363 344 293 362 446 364 332 289 413 249 462 261 401 461 253 295 439 358 395 457 269 457 321 366 332 282 384 322 243 282 295 252 448 389 483 486 398 261 435 443 351 331 432 317 442 354 244 305 293 386 443 316 371 329 349 360 364 363 442 411 450 306 458 444 372 384 382 430 323 348 352 378 262 321 ...
output:
819792353
result:
ok 1 number(s): "819792353"
Test #18:
score: 5
Accepted
time: 584ms
memory: 7564kb
input:
481 357 242 301 426 437 334 292 366 256 342 310 407 247 424 375 284 276 354 432 288 268 324 463 405 369 413 354 421 366 363 383 343 278 331 358 390 425 310 455 453 321 367 372 352 315 473 259 373 424 270 476 310 382 384 264 394 448 275 414 31 43 38 22 18 45 11 5 35 22 44 13 12 30 25 437 445 429 478 ...
output:
402601428
result:
ok 1 number(s): "402601428"
Test #19:
score: 5
Accepted
time: 650ms
memory: 7556kb
input:
480 383 364 323 402 326 358 360 242 400 448 401 284 265 383 270 426 267 355 375 308 416 334 246 280 344 374 242 324 306 332 454 263 435 287 326 359 267 290 445 369 255 392 381 298 383 428 280 417 419 308 364 415 314 28 2 25 5 37 12 4 31 39 39 32 36 47 9 39 334 404 285 433 424 434 298 404 300 423 344...
output:
873285991
result:
ok 1 number(s): "873285991"
Test #20:
score: 5
Accepted
time: 587ms
memory: 7516kb
input:
481 367 303 420 425 352 405 426 292 296 250 308 276 375 341 317 384 316 351 471 403 371 244 449 324 343 392 340 454 333 481 480 393 390 283 431 365 396 423 396 371 385 442 247 475 256 270 391 464 432 249 339 356 280 304 251 327 316 423 362 249 363 440 441 308 340 365 34 29 31 42 22 31 40 8 27 6 5 27...
output:
854884498
result:
ok 1 number(s): "854884498"
Extra Test:
score: 0
Extra Test Passed