QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#688878 | #8351. Ruin the legend | Moyou | 100 ✓ | 425ms | 200076kb | C++14 | 1.1kb | 2024-10-30 14:04:09 | 2024-10-30 14:04:10 |
Judging History
answer
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 5e3 + 10, mod = 998244353;
int n, k, a[N], p[N], f[N][N], s[N], fac[N];
map<int, int> id;
vector<int> t[N];
int idx, L[N], b[N], tot;
signed main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> k, fac[0] = 1;
for(int i = 1; i <= n; i ++) fac[i] = fac[i - 1] * i % mod;
for(int i = 1; i <= n; i ++) {
cin >> a[i];
if(id[a[i] - k]) t[id[a[i] - k]].push_back(i), id[a[i]] = id[a[i] - k];
else t[++ idx].push_back(i), id[a[i]] = idx;
}
for(int i = 1; i <= idx; i ++) {
int ll = tot + 1;
for(auto j : t[i]) L[++ tot] = ll;
}
f[0][0] = 1;
for(int j = 1; j <= n; j ++) {
s[0] = f[0][j - 1];
for(int i = 1; i <= n; i ++)
s[i] = (s[i - 1] + f[i][j - 1]) % mod;
for(int i = 1; i <= n; i ++) {
f[i][j] = f[i - 1][j - 1];
if(i >= 2) f[i][j] += 2 * (s[i - 2] - (L[i] >= 2 ? s[L[i] - 2] : 0)) % mod;
f[i][j] %= mod;
}
}
int ans = 0;
for(int i = 1; i <= n; i ++) {
if(n - i & 1) ans = (ans - fac[i] * f[n][i]) % mod;
else ans = (ans + fac[i] * f[n][i]) % mod;
}
cout << (ans + mod) % mod << '\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: 0ms
memory: 3832kb
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: 0ms
memory: 3844kb
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: 2ms
memory: 8760kb
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: 0ms
memory: 6628kb
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: 0ms
memory: 6700kb
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: 13ms
memory: 17028kb
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: 13ms
memory: 15720kb
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: 10ms
memory: 15716kb
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: 402ms
memory: 200000kb
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: 425ms
memory: 200076kb
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