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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
| #include <bits/stdc++.h> using namespace std;
const int G = 3; const int B = 19; const int N = (1 << B) | 1; const int IG = 332748118; const int MOD = 998244353; typedef long long ll;
inline int add(int a, int b) { return (a + b >= MOD) ? (a + b - MOD) : (a + b); } inline int dec(int a, int b) { return (a - b >= 0) ? (a - b) : (a - b + MOD); } inline int mul(ll a, int b) { return a * b - a * b / MOD * MOD; } inline int qpow(int x, ll y) { int res = 1; for (; y; y >>= 1, x = mul(x, x)) if (y & 1) res = mul(res, x); return res; }
struct Node { double x, y; Node() {} Node(const double &_x, const double &_y) : x(_x), y(_y) {} Node operator+(const Node &t) { return Node(x + t.x, y + t.y); } Node operator-(const Node &t) { return Node(x - t.x, y - t.y); } Node operator*(const Node &t) { return Node(x * t.x - y * t.y, x * t.y + y * t.x); } Node operator*(const double &k) { return Node(x * k, y * k); } Node operator~() { return Node(x, -y); } void MTTinit(int t) { x = t >> 15; y = t & 32767; } };
class MathMod { public: int inv(int t) { return qpow(t, MOD - 2); } };
class Poly : protected MathMod { public: Poly() { for (level = 1; level < B; ++level) { limit = 1 << level, rev = REV[level]; for (int i = 0; i < limit; ++i) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) ? (limit >> 1) : 0); } }
private: int tmp_a[N], tmp_b[N], tmp_c[N], tmp_d[N];
protected: int limit, level; int *rev, REV[B][N];
void InitLimit(const int need_len) { for (limit = 1, level = 0; limit <= need_len; limit <<= 1) ++level; rev = REV[level]; InitLimitExtra(); }
virtual void InitLimitExtra() = 0; virtual void Transform() = 0;
public: virtual void Mul(const int len_a, const int *a, const int len_b, const int *b, int *c) = 0;
void Inv(const int len, const int *a, int *b) { if (!len) { b[0] = inv(a[0]); return; } Inv(len >> 1, a, b); Mul(len, b, len, b, tmp_a); Mul(len, tmp_a, len, a, tmp_a);
for (int i = 0; i <= len; ++i) b[i] = add(add(b[i], b[i]), MOD - tmp_a[i]);
for (int i = len + 1; i < limit; ++i) b[i] = 0; for (int i = 0; i < limit; ++i) tmp_a[i] = 0; }
void Deriv(const int len, const int *a, int *b) { for (int i = 0; i < len; ++i) b[i] = mul(a[i + 1], i + 1); b[len] = 0; }
void Integral(const int len, const int *a, int *b) { for (int i = len; i > 0; --i) b[i] = mul(a[i - 1], inv(i)); b[0] = 0; }
void Ln(const int len, const int *a, int *b) { Deriv(len, a, b); Inv(len, a, tmp_b); Mul(len, b, len, tmp_b, tmp_b); Integral(len, tmp_b, b);
for (int i = len + 1; i < limit; ++i) b[i] = 0; for (int i = 0; i < limit; ++i) tmp_b[i] = 0; }
void Exp(const int len, const int *a, int *b) { if (!len) { b[0] = 1; return; } Exp(len >> 1, a, b); Ln(len, b, tmp_c); for (int i = 0; i <= len; ++i) tmp_c[i] = add(MOD - tmp_c[i], a[i]); ++tmp_c[0]; Mul(len, tmp_c, len, b, b);
for (int i = len + 1; i < limit; ++i) b[i] = 0; for (int i = 0; i < limit; ++i) tmp_c[i] = 0; }
void Sqrt(const int len, const int *a, int *b) { static int inv2 = inv(2); if (!len) { b[0] = 1; return; } Sqrt(len >> 1, a, b); Inv(len, b, tmp_b); Mul(len, a, len, tmp_b, tmp_b); for (int i = 0; i <= len; ++i) b[i] = mul(add(tmp_b[i], b[i]), inv2);
for (int i = len + 1; i < limit; ++i) b[i] = 0; for (int i = 0; i < limit; ++i) tmp_b[i] = 0; }
void Power(const int len, const int *a, const char *s, int *b) { int Len = strlen(s); int val1 = 0, val2 = 0, val3 = 0; for (int i = 0; i < Len; ++i) { val1 = (10LL * val1 + s[i] - '0') % MOD; val2 = (10LL * val2 + s[i] - '0') % (MOD - 1); } for (int i = 0; i < min(6, Len); ++i) val3 = 10 * val3 + s[i] - '0'; if (a[0] == 0 && val3 > len) { fill(b, b + len + 1, 0); return; }
int u, v, shift = 0; for (int i = 0; i <= len && a[i] == 0; ++i) shift++; if ((ll)shift * val1 > len) { fill(b, b + len + 1, 0); return; } u = qpow(a[shift], MOD - 2); v = qpow(a[shift], val2); for (int i = 0; i <= len; ++i) b[i] = mul(a[i + shift], u); Ln(len, b, tmp_d); b[0] = tmp_d[0]; for (int i = 1; i <= len; ++i) b[i] = mul(tmp_d[i], val1); Exp(len, b, tmp_d); shift *= val1; for (int i = 0; i < shift; ++i) b[i] = 0; for (int i = shift; i <= len; ++i) b[i] = mul(tmp_d[i - shift], v); for (int i = 0; i < limit; ++i) tmp_d[i] = 0; for (int i = len + 1; i < limit; ++i) b[i] = 0; } };
class FFT : public Poly { public: FFT() { const double Pi = acos(-1.0); for (level = 1; level < B; ++level) { limit = 1 << level, w = W[level]; for (int mid = 1; mid < limit; mid <<= 1) { w[mid] = Node(1, 0); for (int i = 1; i < mid; ++i) { if ((i & 31) == 1) w[mid + i] = Node(cos(Pi * i / mid), sin(Pi * i / mid)); else w[mid + i] = w[mid + i - 1] * w[mid + 1]; } } } }
private: Node tmp_a[N], tmp_b[N], tmp_c[N], tmp_d[N];
protected: Node *w, W[B][N];
void InitLimitExtra() { w = W[level]; } void Transform() {} void Transform(Node *val, int type) { if (type == 1) reverse(val + 1, val + limit); for (int i = 0; i < limit; ++i) if (i < rev[i]) swap(val[i], val[rev[i]]); for (int mid = 1; mid < limit; mid <<= 1) { int R = mid << 1; for (int i = 0; i < limit; i += R) for (int j = 0; j < mid; ++j) { Node v = w[mid + j] * val[i + mid + j]; val[i + mid + j] = val[i + j] - v; val[i + j] = val[i + j] + v; } } }
public: void Mul(const int len_a, const int *a, const int len_b, const int *b, int *c) { InitLimit(len_a + len_b); for (int i = 0; i <= len_a; ++i) tmp_a[i].MTTinit(a[i]); for (int i = 0; i <= len_b; ++i) tmp_b[i].MTTinit(b[i]); Transform(tmp_a, 1), Transform(tmp_b, 1); for (int i = 0; i < limit; ++i) { Node ft = ~tmp_a[i ? (limit - i) : 0]; Node f0 = (tmp_a[i] - ft) * Node(0, -0.5); Node f1 = (tmp_a[i] + ft) * 0.5; Node gt = ~tmp_b[i ? (limit - i) : 0]; Node g0 = (tmp_b[i] - gt) * Node(0, -0.5); Node g1 = (tmp_b[i] + gt) * 0.5; tmp_c[i] = f1 * g1, tmp_d[i] = f0 * g1 + f1 * g0 + f0 * g0 * Node(0, 1); } Transform(tmp_c, -1), Transform(tmp_d, -1); for (int i = 0; i <= len_a + len_b; ++i) { ll v1 = (ll)(tmp_c[i].x / limit + 0.5) % MOD; ll v2 = (ll)(tmp_d[i].x / limit + 0.5) % MOD; ll v3 = (ll)(tmp_d[i].y / limit + 0.5) % MOD; c[i] = ((v1 << 30) + (v2 << 15) + v3) % MOD; }
for (int i = 0; i < limit; ++i) tmp_a[i] = tmp_b[i] = tmp_c[i] = tmp_d[i] = Node(0, 0); } };
class NTT : public Poly { private: int tmp_a[N], tmp_b[N];
public: void Mul(const int len_a, const int *a, const int len_b, const int *b, int *c) { InitLimit(len_a + len_b); for (int i = 0; i <= len_a; ++i) tmp_a[i] = a[i]; for (int i = 0; i <= len_b; ++i) tmp_b[i] = b[i]; Transform(tmp_a, 1), Transform(tmp_b, 1); for (int i = 0; i < limit; ++i) c[i] = mul(tmp_a[i], tmp_b[i]); Transform(c, -1); int INV = inv(limit); for (int i = 0; i <= len_a + len_b; ++i) c[i] = mul(c[i], INV); for (int i = len_a + len_b + 1; i < limit; ++i) c[i] = 0; for (int i = 0; i < limit; ++i) tmp_a[i] = tmp_b[i] = 0; }
protected: void InitLimitExtra() {} void Transform() {} void Transform(int *val, int type) { for (int i = 0; i < limit; ++i) if (i < rev[i]) swap(val[i], val[rev[i]]); for (int mid = 1; mid < limit; mid <<= 1) { int wn = qpow(type == 1 ? G : IG, (MOD - 1) / (mid << 1)); for (int j = 0; j < limit; j += (mid << 1)) { int w = 1; for (int k = 0; k < mid; k++, w = mul(w, wn)) { int x = val[j + k], y = mul(w, val[j + k + mid]); val[j + k] = add(x, y); val[j + k + mid] = dec(x, y); } } } } };
|