QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#662588 | #8351. Ruin the legend | hcywoi | 100 ✓ | 72ms | 5644kb | C++23 | 2.1kb | 2024-10-21 06:49:41 | 2024-10-21 06:49:41 |
Judging History
answer
#include <bits/stdc++.h>
using i64 = long long;
constexpr int N = 5005;
constexpr int p = 998244353;
int fac[N];
void init() {
fac[0] = 1;
for (int i = 1; i < N; i ++ ) {
fac[i] = 1LL * fac[i - 1] * i % p;
}
}
void inc(int &a, int b) {
a += b;
if (a >= p) {
a -= p;
}
}
void sub(int &a, int b) {
a -= b;
if (a < 0) {
a += p;
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
init();
int n, k;
std::cin >> n >> k;
std::vector<int> a(n);
for (int i = 0; i < n; i ++ ) {
std::cin >> a[i];
}
std::vector<int> cnt;
std::vector<bool> vis(n);
for (int i = 0; i < n; i ++ ) {
if (!vis[i]) {
int x = 0;
for (int j = i; j < n; j ++ ) {
if (a[j] == a[i] + x * k) {
x ++ ;
vis[j] = true;
}
}
cnt.push_back(x);
}
}
std::vector<int> f(n);
f[0] = 1;
for (auto x : cnt) {
std::vector dp(x, std::vector<std::array<int, 2>>(x));
dp[0][0][0] = 1;
for (int i = 1; i < x; i ++ ) {
for (int j = 0; j <= i; j ++ ) {
inc(dp[i][j][0], dp[i - 1][j][0]);
inc(dp[i][j][0], dp[i - 1][j][1]);
if (j > 0) {
inc(dp[i][j][1], dp[i - 1][j - 1][1]);
inc(dp[i][j][1], dp[i - 1][j - 1][0] * 2 % p);
}
}
}
std::vector<int> g(n);
for (int i = 0; i < n; i ++ ) {
for (int j = 0; j < x && i + j < n; j ++ ) {
inc(g[i + j], 1LL * f[i] * dp.back()[j][0] % p);
inc(g[i + j], 1LL * f[i] * dp.back()[j][1] % p);
}
}
f = std::move(g);
}
int ans = 0;
for (int i = 0; i < n; i ++ ) {
if (i % 2) {
sub(ans, 1LL * f[i] * fac[n - i] % p);
} else {
inc(ans, 1LL * f[i] * fac[n - i] % p);
}
}
std::cout << ans << "\n";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 20
Accepted
Test #1:
score: 20
Accepted
time: 1ms
memory: 3564kb
input:
10 361915 1186738 1548653 1910568 2272483 2634398 2996313 4082058 4443973 4805888 5167803
output:
605624
result:
ok 1 number(s): "605624"
Test #2:
score: 20
Accepted
time: 1ms
memory: 3564kb
input:
10 503576 1412863 3427167 3694544 3930743 4198120 4434319 4937895 5441471 5945047 6212424
output:
919056
result:
ok 1 number(s): "919056"
Subtask #2:
score: 30
Accepted
Test #3:
score: 30
Accepted
time: 1ms
memory: 3676kb
input:
400 246137 1245623 1491760 1737897 1930990 1984034 2177127 2230171 2423264 2476308 2669401 2722445 2915538 2968582 3161675 3214719 3407812 3460856 3653949 3706993 3900086 3953130 4146223 4199267 4392360 4445404 4638497 4691541 4884634 4937678 5130771 5183815 5376908 5429952 5623045 5676089 5869182 5...
output:
672078497
result:
ok 1 number(s): "672078497"
Test #4:
score: 30
Accepted
time: 1ms
memory: 3600kb
input:
400 595353 60469 655822 1251175 1846528 1994258 2441881 2589611 3037234 3184964 3576288 3632587 3755516 3780317 4171641 4227940 4350869 4375670 4766994 4823293 4946222 4971023 5362347 5418646 5541575 5566376 5702177 5957700 6013999 6136928 6161729 6297530 6553053 6609352 6732281 6757082 6892883 7148...
output:
955108635
result:
ok 1 number(s): "955108635"
Test #5:
score: 30
Accepted
time: 1ms
memory: 3652kb
input:
400 599588 698120 881310 894243 953566 1297708 1300366 1314706 1463444 1480898 1493831 1553154 1563240 1603860 1658458 1897296 1899954 1914294 2063032 2080486 2093419 2104932 2152742 2162828 2203448 2234734 2258046 2334676 2496884 2499542 2513882 2662620 2680074 2693007 2704520 2752330 2762416 28030...
output:
208730170
result:
ok 1 number(s): "208730170"
Subtask #3:
score: 20
Accepted
Test #6:
score: 20
Accepted
time: 3ms
memory: 3708kb
input:
1000 567438 1434354 2001792 2569230 2944075 3136668 3392405 3511513 3704106 3959843 4078951 4271544 4527281 4646389 4838982 5094719 5213827 5406420 5662157 5781265 5973858 6229595 6348703 6541296 6797033 6916141 7108734 7364471 7483579 7676172 7931909 8051017 8243610 8499347 8618455 8811048 9066785 ...
output:
488672186
result:
ok 1 number(s): "488672186"
Test #7:
score: 20
Accepted
time: 3ms
memory: 3708kb
input:
1000 144937 189260 200510 334197 345447 388260 457616 479134 490384 504698 533197 561098 573991 602553 624071 628013 628499 635321 649635 678134 706035 718928 747490 769008 772950 773436 780258 794572 823071 850972 863865 864659 892427 913945 917887 918373 922788 925195 939509 968008 995909 1008802 ...
output:
979151496
result:
ok 1 number(s): "979151496"
Test #8:
score: 20
Accepted
time: 3ms
memory: 3804kb
input:
1000 955267 79983 995514 1035250 1719775 1855674 1913421 1950781 1957960 1966589 1968351 1990517 2101118 2202581 2442504 2595814 2675042 2703029 2727524 2810941 2868688 2906048 2913227 2921856 2923618 2945784 2996383 3043340 3056385 3157848 3177959 3397771 3551081 3554246 3630309 3658296 3682791 376...
output:
692731442
result:
ok 1 number(s): "692731442"
Subtask #4:
score: 30
Accepted
Test #9:
score: 30
Accepted
time: 72ms
memory: 5644kb
input:
5000 909960 263288 1173248 1534296 2083208 2309676 2385789 2444256 2993168 3219636 3295749 3354216 3903128 4129596 4205709 4264176 4813088 5039556 5115669 5174136 5723048 5949516 6025629 6084096 6633008 6859476 6935589 6994056 7542968 7769436 7845549 7904016 8205603 8452928 8679396 8755509 8813976 9...
output:
731681576
result:
ok 1 number(s): "731681576"
Test #10:
score: 30
Accepted
time: 70ms
memory: 4000kb
input:
5000 633078 314538 474845 518803 573627 907164 947616 1028632 1107923 1151881 1199740 1206705 1437881 1492463 1540242 1580694 1593600 1622784 1628346 1661324 1661710 1741001 1782655 1784959 1788594 1832818 1839783 1852212 2000185 2070959 2125541 2173320 2179195 2198152 2209332 2213772 2226678 225586...
output:
922398112
result:
ok 1 number(s): "922398112"
Extra Test:
score: 0
Extra Test Passed