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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
#include <cstring> #include <cstdio> #include <algorithm>
using namespace std;
const int maxn = 32010; const int mod = 998244353; const int g = 3;
int n, k; int f[maxn], a[maxn];
int (int x, int y) { int ret = 1; while (y) { if (y & 1) ret = 1LL*ret*x%mod; x = 1LL*x*x%mod; y >>= 1; } return ret; }
struct poly { int *a, len; poly(int l_ = 0) { len = l_; a = new int[len]; for (int i = 0; i < len; i++) a[i] = 0; } } P;
int wa[maxn*8], wb[maxn*8], wc[maxn*8], rev[maxn*8];
void ntt(int *a, int l, int ty) { for (int i = 1; i < (1<<l); i++) rev[i] = ((rev[i>>1]>>1) | ((i&1)<<(l-1))); for (int i = 0; i < (1<<l); i++) if (i < rev[i]) swap(a[i], a[rev[i]]); for (int len = 2; len <= (1<<l); len <<= 1) { int wn = qpow(g, (mod-1)/len); for (int s = 0; s < (1<<l); s += len) { int w = 1; for (int i = s; i < s + (len>>1); ++ i) { int v1 = a[i], v2 = 1LL*w*a[i+(len>>1)]%mod; a[i] = (v1 + v2) % mod; a[i + (len >> 1)] = (v1 + mod - v2) % mod; w = 1LL*w*wn%mod; } } } if (ty == -1) { int inv = qpow((1<<l), mod-2); for (int i = 0; i < (1<<l); i++) a[i] = 1LL*a[i]*inv%mod; for (int i = 1; i < (1<<(l-1)); i++) swap(a[i], a[(1<<l)-i]); } }
poly operator*(const poly &p1, const poly &p2) { poly ret(p1.len + p2.len - 1); int l = 0; while ((1<<l) < ret.len) ++ l; for (int i = 0; i < (1<<l); i++) wa[i] = wb[i] = 0; for (int i = 0; i < p1.len; i++) wa[i] = p1.a[i]; for (int i = 0; i < p2.len; i++) wb[i] = p2.a[i]; ntt(wa, l, 1); ntt(wb, l, 1); for (int i = 0; i < (1<<l); i++) wc[i] = 1LL*wa[i]*wb[i]%mod; ntt(wc, l, -1); for (int i = 0; i < ret.len; i++) ret.a[i] = wc[i]; return ret; }
poly polyInv(const poly &p) { if (p.len == 1) { poly ret(1); ret.a[0] = qpow(p.a[0], mod-2); return ret; } int nlen = (p.len + 1) / 2; poly np(nlen); for (int i = 0; i < nlen; i++) np.a[i] = p.a[i]; poly f0 = polyInv(np); poly t1 = p*f0; poly t2(p.len); for (int i = 0; i < p.len; i++) { if (i < t1.len) { t2.a[i] = (mod - t1.a[i]) % mod; } } t2.a[0] = (t2.a[0] + 2) % mod; poly res = f0*t2; poly ret(p.len); for (int i = 0; i < p.len; i++) ret.a[i] = res.a[i]; return ret; }
poly polyMod(const poly &p, const poly &q) { if (p.len < q.len) return p; poly rp(p.len-q.len+1), rq(p.len-q.len+1); for (int i = 0; i < p.len; i++) if (q.len-1-i < rp.len) rp.a[p.len-1-i] = p.a[i]; for (int i = 0; i < q.len; i++) if (q.len-1-i < rq.len) rq.a[q.len-1-i] = q.a[i]; poly t1 = rp*polyInv(rq); poly t2(p.len-q.len+1); for (int i = 0; i < t2.len; i++) { t2.a[i] = t1.a[p.len-q.len-i]; } poly t3 = t2*q; poly ret(q.len-1); for (int i = 0; i < ret.len; i++) ret.a[i] = (p.a[i] + mod - t3.a[i]) % mod; return ret; }
poly calMod(int y) { poly ret(1); ret.a[0] = 1; poly x(2); x.a[1] = 1; while (y) { if (y & 1) ret = polyMod(ret * x, P); x = polyMod(x * x, P); y >>= 1; } return ret; }
void polyPrint(const poly &p) { printf("%d :n", p.len); for (int i = 0; i < p.len; i++) { printf("%d ", p.a[i]); } printf("n"); }
int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= k; i++) {scanf("%d", &a[i]); a[i] = (a[i] + mod) % mod;} for (int i = 0; i < k; i++) {scanf("%d", &f[i]); f[i] = (f[i] + mod) % mod;} int ans = 0; P = poly(k+1); for (int i = 1; i <= k; i++) P.a[k-i] = (mod-a[i])%mod; P.a[k] = 1; poly pol = calMod(n); for (int i = 0; i < pol.len; i++) { ans = (ans + 1LL*pol.a[i]*f[i]%mod)%mod; } printf("%dn", ans); return 0; }
|
近期评论