1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| #include <bits/stdc++.h> #define rep(i, a, b) for(int i = (a); i <= (b); i++) #define per(i, a, b) for(int i = (a); i >= (b); i--) #define pb push_back using namespace std; typedef long long ll; const int mod = 998244353;
inline int Max(const int &x, const int &y) { return x > y ? x : y; } inline int Min(const int &x, const int &y) { return x < y ? x : y; } inline int add(const int &x, const int &y) { return x + y < mod ? x + y : x + y - mod; } inline int sub(const int &x, const int &y) { return x - y < 0 ? x - y + mod : x - y; } inline int mul(const int &x, const int &y) { return (int)((ll)x * y % mod); } inline int ksm(int x, int y = mod - 2) { int ss = 1; for(; y; y >>= 1, x = mul(x, x)) if(y & 1) ss = mul(ss, x); return ss; } namespace Poly { inline int Get(int x) { int ss = 1; for(; ss <= x; ss <<= 1); return ss; } void ntt(vector<int> &A, int lmt, int opt) { A.resize(lmt + 5); for(int i = 0, j = 0; i < lmt; i++) { if(i > j) swap(A[i], A[j]); for(int k = lmt >> 1; (j ^= k) < k; k >>= 1); } vector<int> w(lmt >> 1); for(int mid = 1; mid < lmt; mid <<= 1) { w[0] = 1; int w0 = ksm(opt == 1 ? 3 : (mod + 1) / 3, (mod - 1) / mid / 2); for(int j = 1; j < mid; j++) w[j] = mul(w[j - 1], w0); for(int R = mid << 1, j = 0; j < lmt; j += R) for(int k = 0; k < mid; k++) { int x = A[j + k], y = mul(w[k], A[j + mid + k]); A[j + k] = add(x, y), A[j + mid + k] = sub(x, y); } } if(opt == -1) for(int i = 0, inv = ksm(lmt); i < lmt; i++) A[i] = mul(A[i], inv); } vector<int> Mul(const vector<int> &a, const vector<int> &b) { vector<int> A = a, B = b; int lmt = Get(a.size() + b.size() - 2); ntt(A, lmt, 1), ntt(B, lmt, 1); for(int i = 0; i < lmt; i++) A[i] = mul(A[i], B[i]); ntt(A, lmt, -1); return A.resize(a.size() + b.size() - 1), A; } vector<int> Inv(const vector<int> &A, int sz = -1) { if(sz == -1) sz = A.size(); vector<int> res; if(sz == 1) return res.pb(ksm(A[0])), res; res = Inv(A, (sz + 1) / 2); vector<int> tmp(A.begin(), A.begin() + sz); int lmt = Get(sz * 2 - 2); ntt(tmp, lmt, 1), ntt(res, lmt, 1); for(int i = 0; i < lmt; i++) res[i] = mul(sub(2, mul(res[i], tmp[i])), res[i]); ntt(res, lmt, -1); return res.resize(sz), res; } void Div(const vector<int> &A, const vector<int> &B, vector<int> &D, vector<int> &R) { if(B.size() > A.size()) return (void)(D.clear(), D.pb(0), R = A); vector<int> a = A, b = B, iB; int n = A.size(), m = B.size(); reverse(a.begin(), a.end()), reverse(b.begin(), b.end()); b.resize(n - m + 1), iB = Inv(b, n - m + 1); D = Mul(a, iB), D.resize(n - m + 1), reverse(D.begin(), D.end()); R = Mul(B, D); for(int i = 0; i < m; i++) R[i] = (mod + A[i] - R[i]) % mod; R.resize(m); } } int n, m, ans; vector<int> f, h, res, b, tmp;
vector<int> Mul(const vector<int> &a, const vector<int> &b) { vector<int> t = Poly::Mul(a, b), r; Poly::Div(t, f, tmp, r); return r; } int main() { scanf("%d%d", &n, &m); if(n <= m) { rep(i, 1, m) { int x; scanf("%d", &x); if(i == n) { printf("%d\n", (x % mod + mod) % mod); return 0; } } return 0; } f.resize(m + 1), h.resize(m), f[m] = 1; rep(i, 1, m) { scanf("%d", &f[m - i]); f[m - i] = ((mod - f[m - i]) % mod + mod) % mod; } rep(i, 0, m - 1) scanf("%d", &h[i]), h[i] = (h[i] % mod + mod) % mod; res.pb(1), b.pb(0), b.pb(1); for(; n; n >>= 1, b = Mul(b, b)) if(n & 1) res = Mul(res, b); rep(i, 0, m - 1) ans = (ans + 1ll * res[i] * h[i]) % mod; printf("%d\n", ans); return 0; }
|