QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#659423 | #9488. Do Not Turn Back | ucup-team3734# | WA | 1ms | 3852kb | C++23 | 5.3kb | 2024-10-19 20:08:13 | 2024-10-19 20:08:15 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long i64;
typedef unsigned long long u64;
typedef double lf;
const int mod = 998244353;
template<typename T>
T add(T x) {
return x;
}
template<typename T, typename... Ts>
T add(T x, Ts... y) {
T res = x + add(y...);
if (res >= mod)
res -= mod;
return res;
}
template<typename T, typename... Ts>
T sub(T x, Ts... y) {
return add(x, mod - add(y...));
}
template<typename T, typename... Ts>
void udd(T &x, Ts... y) {
x = add(x, y...);
}
template<typename T>
T mul(T x) {
return x;
}
template<typename T, typename... Ts>
T mul(T x, Ts... y) {
return (x * 1ll * mul(y...)) % mod;
}
template<typename T, typename... Ts>
void uul(T &x, Ts... y) {
x = mul(x, y...);
}
typedef vector<vector<int>> matrix;
matrix mul(const matrix &a, const matrix &b) {
int n = (int) a.size();
matrix c(n, vector<int>(n, 0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
udd(c[i][j], mul(a[i][k], b[k][j]));
}
}
}
return c;
}
matrix bin(matrix a, i64 deg) {
matrix r(a.size(), vector<int>(a.size(), 0));
for (int i = 0; i < (int) a.size(); i++)
r[i][i] = 1;
while (deg) {
if (deg & 1) {
r = mul(r, a);
}
deg >>= 1;
a = mul(a, a);
}
return r;
}
vector<int> poly_mul(const vector<int> &a, const vector<int> &b, const vector<int> &f) {
vector<int> ret(a.size() + b.size() - 1);
// ret = a * b
for (size_t i = 0; i < a.size(); i++) {
for (size_t j = 0; j < b.size(); j++) {
udd(ret[i + j], mul(a[i], b[j]));
}
}
// reducing ret mod f(x)
int n = f.size();
for (int i = (int) ret.size()-1; i >= n; i--) {
for (int j = n - 1; j >= 0; j--) {
udd(ret[i - j - 1], mul(ret[i], f[j]));
}
}
ret.resize(min((int) ret.size(), n));
return ret;
}
int rec_eval(const vector<int> &c, const vector<int> &s, long long k) {
int n = (int) c.size();
assert(c.size() <= s.size());
vector<int> a = (n == 1) ? vector<int>{c[0]} : vector<int>{0, 1};
vector<int> x{1};
for (; k > 0; k /= 2) {
if (k % 2)
x = poly_mul(x, a, c);
a = poly_mul(a, a, c);
}
x.resize(n);
int ret = 0;
for (int i = 0; i < n; i++) {
udd(ret, mul(x[i], s[i]));
}
return ret;
}
namespace BerlekampMassey {
int L, m, b, n;
vector<int> s, C, B;
void init() {
s.clear();
C.clear();
B.clear();
C.push_back(1);
B.push_back(1);
L = n = 0;
m = b = 1;
}
int pow_mod(int a, int k) {
int s = 1;
while (k) {
if (k & 1)
s = 1ll * s * a % mod;
a = 1ll * a * a % mod;
k >>= 1;
}
return s;
}
void update(int d) {
s.push_back(d);
for (int i = 1; i <= L; ++i)
d = (d + 1ll * C[i] * s[n - i] % mod) % mod;
if (d == 0)
++m;
else if (2 * L <= n) {
vector<int> T = C;
C.resize(n + 1 - L + 1);
for (int i = L + 1; i <= n + 1 - L; ++i)
C[i] = 0;
for (int i = 0; i < B.size(); ++i)
C[i + m] = (C[i + m] + mod - 1ll * d * pow_mod(b, mod - 2) % mod * B[i] % mod) % mod;
L = n + 1 - L;
B = T;
b = d;
m = 1;
} else {
for (int i = 0; i < B.size(); ++i)
C[i + m] = (C[i + m] + mod - 1ll * d * pow_mod(b, mod - 2) % mod * B[i] % mod) % mod;
++m;
}
++n;
}
vector<int> get_min_rec() {
vector<int> ret;
for (int i = 1; i < C.size(); ++i) {
int output = (mod - C[i]) % mod;
ret.push_back(output);
// cerr << output << ",";
}
return ret;
}
};
void solve() {
int n, m, k;
cin >> n >> m >> k;
matrix a(n, vector<int>(n, 0));
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
u--, v--;
a[u][v]++;
a[v][u]++;
}
vector<int> v(n * n);
vector<int> nv(n * n);
v[0] = 1;
BerlekampMassey::init();
int need = 5 * n;
vector<int> answers;
answers.push_back(0);
for (int it = 0; it < need; ++it) {
fill(nv.begin(), nv.end(), 0);
for (int c = 0; c < n * n; ++c) {
if (v[c] == 0) {
continue;
}
int prev = c % n;
int cur = c / n;
for (int to = 0; to < n; to++) {
if (to != prev && a[cur][to]) {
udd(nv[to * n + cur], v[c]);
}
}
}
v.swap(nv);
int ans = 0;
for (int i = 0; i + 1 < n; ++i) {
udd(ans, v[(n - 1) * n + i]);
}
BerlekampMassey::update(ans);
answers.push_back(ans);
}
auto coeffs = BerlekampMassey::get_min_rec();
// cerr << coeffs.size() << endl;
cout << rec_eval(coeffs, answers, k) << '\n';
}
signed main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
ios_base::sync_with_stdio(false);
int t = 1;
// cin >> t;
for (int i = 0; i < t; i++) {
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3848kb
input:
6 8 5 1 2 1 3 2 3 2 4 3 5 4 5 4 6 5 6
output:
2
result:
ok "2"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3668kb
input:
11 11 2023 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 1 11
output:
1
result:
ok "1"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3628kb
input:
7 21 1000000000 1 2 1 3 1 4 1 5 1 6 1 7 2 3 2 4 2 5 2 6 2 7 3 4 3 5 3 6 3 7 4 5 4 6 4 7 5 6 5 7 6 7
output:
405422475
result:
ok "405422475"
Test #4:
score: 0
Accepted
time: 1ms
memory: 3500kb
input:
12 56 78144853 1 2 1 3 1 4 1 6 1 7 1 9 1 10 1 11 1 12 2 4 2 5 2 6 2 8 2 9 2 10 2 11 2 12 3 4 3 5 3 6 3 7 3 8 3 9 3 11 3 12 4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 12 5 6 5 7 5 8 5 9 5 10 5 11 5 12 6 8 6 9 6 10 7 8 7 9 7 11 7 12 8 9 8 10 8 11 8 12 9 10 9 11 9 12 10 11 11 12
output:
843326021
result:
ok "843326021"
Test #5:
score: 0
Accepted
time: 1ms
memory: 3852kb
input:
13 61 268435455 1 2 1 3 1 4 1 5 1 6 1 8 1 9 1 10 1 11 1 12 1 13 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 12 2 13 3 4 3 5 3 6 3 7 3 8 3 9 3 10 4 6 4 7 4 9 4 12 4 13 5 6 5 7 5 9 5 11 5 13 6 7 6 8 6 9 6 11 6 12 6 13 7 8 7 10 7 11 7 12 8 9 8 10 8 11 8 12 8 13 9 10 9 12 9 13 10 11 10 12 10 13 11 12 11 13 12 13
output:
69651169
result:
ok "69651169"
Test #6:
score: 0
Accepted
time: 1ms
memory: 3612kb
input:
14 79 33554431 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 2 3 2 4 2 5 2 6 2 7 2 8 2 11 2 12 2 14 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 3 14 4 5 4 6 4 7 4 8 4 9 4 11 4 12 5 6 5 7 5 10 5 11 5 12 5 13 5 14 6 7 6 8 6 9 6 10 6 11 6 12 6 13 6 14 7 8 7 9 7 10 7 11 7 12 7 13 7 14 8 10 8 11 8...
output:
793621538
result:
ok "793621538"
Test #7:
score: 0
Accepted
time: 1ms
memory: 3572kb
input:
15 92 439487671 1 2 1 3 1 5 1 7 1 8 1 9 1 10 1 11 1 13 1 14 1 15 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 2 14 2 15 3 4 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 3 14 3 15 4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 12 4 13 4 14 4 15 5 7 5 8 5 10 5 11 5 12 5 13 5 14 6 7 6 8 6 9 6 10 6 11 6 12 6 13 6 14 6 1...
output:
196201187
result:
ok "196201187"
Test #8:
score: 0
Accepted
time: 0ms
memory: 3568kb
input:
5 10 230391930 1 2 1 3 1 4 1 5 2 3 2 4 2 5 3 4 3 5 4 5
output:
552614127
result:
ok "552614127"
Test #9:
score: 0
Accepted
time: 0ms
memory: 3572kb
input:
5 8 268435455 1 2 1 3 1 4 2 3 2 4 2 5 3 4 4 5
output:
666456772
result:
ok "666456772"
Test #10:
score: 0
Accepted
time: 0ms
memory: 3572kb
input:
6 13 67108863 1 2 1 3 1 4 1 5 2 4 2 5 2 6 3 4 3 5 3 6 4 5 4 6 5 6
output:
317571510
result:
ok "317571510"
Test #11:
score: 0
Accepted
time: 0ms
memory: 3632kb
input:
7 18 67108863 1 2 1 3 1 4 1 5 1 6 1 7 2 3 2 4 2 5 2 6 2 7 3 5 3 6 4 5 4 6 4 7 5 6 6 7
output:
921436359
result:
ok "921436359"
Test #12:
score: 0
Accepted
time: 1ms
memory: 3624kb
input:
11 43 115349891 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 2 3 2 6 2 7 2 8 2 9 2 11 3 4 3 5 3 8 3 9 3 10 4 5 4 6 4 7 4 8 4 9 4 11 5 6 5 7 5 9 5 10 5 11 6 7 6 9 6 10 6 11 7 8 7 9 7 10 7 11 8 9 8 11 9 10 9 11
output:
853336717
result:
ok "853336717"
Test #13:
score: 0
Accepted
time: 1ms
memory: 3676kb
input:
14 78 758166229 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 2 3 2 4 2 5 2 6 2 7 2 8 2 10 2 11 2 14 3 5 3 6 3 7 3 8 3 9 3 10 3 11 4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 13 4 14 5 6 5 7 5 8 5 9 5 10 5 11 5 13 5 14 6 7 6 8 6 9 6 10 6 11 6 12 6 14 7 8 7 9 7 10 7 11 7 12 7 13 7 14 8 9 8 10 8 11 8 1...
output:
949739691
result:
ok "949739691"
Test #14:
score: 0
Accepted
time: 1ms
memory: 3680kb
input:
52 51 376665160 1 2 1 3 2 4 4 5 5 6 5 7 3 8 5 9 5 10 5 11 6 12 4 13 2 14 6 15 14 16 9 17 8 18 15 19 11 20 9 21 1 22 21 23 3 24 24 25 24 26 10 27 7 28 24 29 16 30 6 31 4 32 5 33 10 34 33 35 32 36 19 37 29 38 18 39 17 40 13 41 39 42 3 43 12 44 34 45 20 46 38 47 27 48 43 49 5 50 6 51 4 52
output:
0
result:
ok "0"
Test #15:
score: -100
Wrong Answer
time: 0ms
memory: 3620kb
input:
29 28 4 1 2 1 3 2 4 1 5 3 6 3 7 6 8 4 9 3 10 6 11 10 12 4 13 3 14 9 15 6 16 14 17 16 18 11 19 19 20 8 21 6 22 18 23 18 24 3 25 2 26 8 27 1 28 8 29
output:
0
result:
wrong answer 1st words differ - expected: '1', found: '0'