QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#789337 | #9526. Subsequence Counting | ucup-team298 | TL | 1784ms | 72448kb | C++23 | 2.6kb | 2024-11-27 19:56:38 | 2024-11-27 19:56:39 |
Judging History
answer
#include <bits/stdc++.h>
typedef std::vector<int> vector;
typedef std::vector<vector> matrix;
constexpr int mod = 998244353;
void inc (int &x, int y) {if ((x += y) >= mod) x -= mod;}
int m; matrix identity;
matrix operator * (matrix A, matrix B) {
matrix C(m + 1, vector(m + 1));
for (int i = 0; i <= m; i++) for (int k = 0; k <= m; k++) for (int j = 0; j <= m; j++) C[i][k] = (C[i][k] + 1LL * A[i][j] * B[j][k]) % mod;
return C;
}
matrix power (matrix A, int k) {
matrix B = identity;
for (; k; k >>= 1, A = A * A) if (k & 1) B = B * A;
return B;
}
void exgcd (int a, int b, int &x, int &y) {
if (!b) {x = 1; y = 0; return;}
exgcd(b, a % b, y, x); y -= a / b * x;
}
int inv (int r, int L) {int x, y; exgcd(r, L, x, y); return (x + L) % L;}
matrix solve (int r, int L, std::deque<std::pair<int, matrix>> q) {
if (L == 1) return q[0].second;
if (2 * r > L) {
auto [l, v] = q.front(); q.pop_front();
if (l > 1) q.push_front({l - 1, v});
std::reverse(q.begin(), q.end()); q.push_front({1, v});
return solve(L - r, L, q);
}
int n = q.size(), m = 1; while (m < n) m <<= 1;
std::vector<int> c(n); std::vector<matrix> tr(m << 1, identity), P(n);
for (int i = 0; i < n; i++) P[i] = q[i].second;
std::vector<std::tuple<int, int, int>> events;
for (int i = 0, p = 0; i < n; i++) {
if (p % r) events.emplace_back(p % r, i, 1);
events.emplace_back(0, i, (p + q[i].first + r - 1) / r - (p + r - 1) / r);
p += q[i].first;
if (p % r) events.emplace_back(p % r, i, -1);
}
std::sort(events.begin(), events.end()); events.emplace_back(r, -1, 0);
q.clear();
for (int ql = 0, qr; ql < (int)events.size() - 1; ql = qr) {
for (qr = ql; qr < (int)events.size() and std::get<0>(events[qr]) == std::get<0>(events[ql]); qr++) ;
for (int i = ql; i < qr; i++) if (auto [t, p, v] = events[i]; ~p) {
c[p] += v; tr[m + p] = power(P[p], c[p]);
for (int k = (m + p) >> 1; k; k >>= 1) tr[k] = tr[k << 1] * tr[k << 1 | 1];
}
q.emplace_back(std::get<0>(events[qr]) - std::get<0>(events[ql]), tr[1]);
}
return solve(r - L % r, r, q);
}
signed main () {
std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr);
int n, r, L; std::cin >> n >> m >> r >> L; r = inv(r, L);
identity = matrix(m + 1, vector(m + 1)); for (int i = 0; i <= m; i++) identity[i][i] = 1;
std::array<matrix, 1001> tr; std::fill(tr.begin(), tr.end(), identity);
for (int i = 0, v; i < m; i++) std::cin >> v, tr[v][i][i + 1] = 1;
std::deque<std::pair<int, matrix>> q(n);
for (int i = 0, v; i < n; i++) std::cin >> q[i].first >> v, q[i].second = tr[v];
std::cout << solve(r, L, q)[0][m] << std::endl;
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3800kb
input:
4 2 17 27 3 1 10 3 6 1 10 3 1 1
output:
76
result:
ok single line: '76'
Test #2:
score: 0
Accepted
time: 1ms
memory: 3928kb
input:
5 3 1789 15150 555 718 726 72 555 1029 718 5807 726 1002 718 7240 555
output:
390415327
result:
ok single line: '390415327'
Test #3:
score: 0
Accepted
time: 1ms
memory: 3788kb
input:
1 1 1 1000000000 1000 1000000000 1000
output:
1755647
result:
ok single line: '1755647'
Test #4:
score: 0
Accepted
time: 273ms
memory: 16480kb
input:
1999 10 999999999 1000000000 944 901 986 915 979 988 947 999 969 946 198832 985 235662 982 367137 938 219700 949 166086 906 488084 905 891250 984 243743 971 253382 987 181971 935 2382 948 462701 981 4681 925 113363 916 119397 921 337742 982 427128 921 285959 986 197975 978 140753 907 167150 974 4576...
output:
211590728
result:
ok single line: '211590728'
Test #5:
score: 0
Accepted
time: 1688ms
memory: 70236kb
input:
2000 10 207560381 499173270 382 246 825 688 810 66 333 717 903 444 242322 825 303194 246 266460 66 133293 444 499376 688 175256 333 260041 717 202138 444 84931 688 353443 825 137750 382 333307 66 450617 810 27484 246 349436 717 45179 444 146073 810 107846 717 416816 246 255378 825 433826 688 273215 ...
output:
686508296
result:
ok single line: '686508296'
Test #6:
score: 0
Accepted
time: 1784ms
memory: 72448kb
input:
2000 10 261469558 497769147 990 38 906 66 751 758 913 137 187 724 253600 187 50741 758 58978 724 358384 751 258090 137 423638 990 121415 137 162742 758 93886 724 279287 913 392322 38 495784 758 195957 906 47122 913 87008 751 17599 66 22711 38 58373 724 116467 990 495160 724 471978 758 24585 724 3064...
output:
989869378
result:
ok single line: '989869378'
Test #7:
score: -100
Time Limit Exceeded
input:
2000 10 321529781 512205698 105 216 706 872 564 639 983 85 153 396 319747 706 46163 639 134011 983 293977 153 176149 983 214154 216 45308 706 270691 396 491463 216 207041 639 118670 983 207549 639 430999 706 294263 872 137031 983 270440 85 480934 153 56566 85 462772 872 53771 639 455400 105 496369 8...