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
| #include <cstdio> #include <vector> #include <algorithm> using namespace std; typedef long long LL; const int N = 1e6 + 10, mod = 998244353; int rev[N]; int binpow (int b, int k = mod - 2) { int res = 1; for (; k; k >>= 1, b = (LL)b * b % mod) if (k & 1) res = (LL)res * b % mod; return res; } void ntt (int *x, int bit, int op) { int tot = 1 << bit; for (int i = 1; i < tot; ++i) if ((rev[i] = rev[i >> 1] >> 1 | (i & 1) << bit - 1) > i) swap(x[rev[i]], x[i]); for (int mid = 1; mid < tot; mid <<= 1) { int w1 = binpow(3, (mod - 1) / (mid << 1)); if (!~op) w1 = binpow(w1); for (int i = 0; i < tot; i += mid << 1) for (int j = 0, k = 1; j < mid; ++j, k = (LL)k * w1 % mod) { int p = x[i | j], q = (LL)k * x[i | j | mid] % mod; x[i | j] = (p + q) % mod, x[i | j | mid] = (p - q) % mod; } } if (~op) return; int itot = binpow(tot); for (int i = 0; i < tot; ++i) x[i] = (LL)x[i] * itot % mod; } class LargeNumber { int sign; vector <LL> w; friend void adj(LargeNumber &x) { while (!x.w.empty() && !x.w.back()) x.w.pop_back(); if (x.w.empty()) x.sign = 0; } friend int cmp(LargeNumber &x, LargeNumber &y) { if (x.sign ^ y.sign) return x.sign < y.sign ? -1 : 1; if (x.size() ^ y.size()) return x.size() < y.size() ? -1 : 1; for (int i = x.size() - 1; ~i; i--) if (x[i] ^ y[i]) return x[i] < y[i] ? -1 : 1; return 0; } friend LargeNumber add(LargeNumber &x, LargeNumber &y) { LargeNumber res; res.sign = 1; res.w.resize(max(x.size(), y.size()) + 1); for (int i = 0; i + 1 < res.size(); i++) { if (i < x.size()) res[i] += x[i]; if (i < y.size()) res[i] += y[i]; if (res[i] > 9) res[i + 1]++, res[i] -= 10; } adj(res); return res; } friend LargeNumber dec(LargeNumber &x, LargeNumber &y) { LargeNumber res; res.sign = 1; res.w.resize(max(x.size(), y.size())); for (int i = 0; i < res.size(); i++) { if (i < x.size()) res[i] += x[i]; if (i < y.size()) res[i] -= y[i]; if (res[i] < 0) res[i + 1]--, res[i] += 10; } adj(res); return res; } friend void div2(LargeNumber &x) { bool flag = false; for (int i = x.size() - 1; ~i; i--) { int t = x[i] + flag * 10; flag = t & 1; x[i] = t >> 1; } adj(x); } public: LargeNumber() {} template <class Type> LargeNumber(Type x) { if (x < 0) sign = -1, x = ~x + 1; else if (x > 0) sign = 1; else sign = 0; while (x) w.push_back(x % 10), x /= 10; } void input() { sign = 1; char c; while ((c = getchar()) < '0' || c > '9') c == '-' && (sign = -1); w.push_back(c - '0'); while ((c = getchar()) >= '0' && c <= '9') w.push_back(c - '0'); reverse(w.begin(), w.end()); } void output() { if (!sign) putchar('0'); if (sign == -1) putchar('-'); for (int i = w.size() - 1; ~i; i--) putchar(w[i] + '0'); } int size () { return w.size(); } LL &operator[](LL x) { return w[x]; } friend bool operator!(LargeNumber x) { return !x.sign; } friend LargeNumber operator-(LargeNumber x) { x.sign *= -1; return x; } friend bool operator!=(LargeNumber x, LargeNumber y) { return cmp(x, y); } friend bool operator==(LargeNumber x, LargeNumber y) { return !cmp(x, y); } friend bool operator<(LargeNumber x, LargeNumber y) { return cmp(x, y) == -1; } friend bool operator>(LargeNumber x, LargeNumber y) { return cmp(x, y) == 1; } friend bool operator<=(LargeNumber x, LargeNumber y) { return cmp(x, y) ^ 1; } friend bool operator>=(LargeNumber x, LargeNumber y) { return ~cmp(x, y); } friend LargeNumber operator*(LargeNumber x, LargeNumber y) { LargeNumber res; res.sign = x.sign * y.sign; res.w.resize(x.size() + y.size()); static int A[N], B[N], C[N]; int bit = 0; while (1 << bit < res.size()) ++bit; int tot = 1 << bit; for (int i = 0; i < x.size(); ++i) A[i] = x.w[i]; for (int i = 0; i < y.size(); ++i) B[i] = y.w[i]; for (int i = x.size(); i < tot; i++) A[i] = 0; for (int i = y.size(); i < tot; i++) B[i] = 0; ntt(A, bit, 1), ntt(B, bit, 1); for (int i = 0; i < tot; i++) C[i] = (LL)A[i] * B[i] % mod; ntt(C, bit, -1); for (int i = 0; i < res.size(); i++) res[i] = (C[i] + mod) % mod; for (int i = 0; i + 1 < res.size(); i++) { res[i + 1] += res[i] / 10; res[i] %= 10; } adj(res); return res; } friend LargeNumber operator/(LargeNumber x, int k) { LargeNumber res = x; if (k < 0) k = -k, res.sign ^= 1; for (int i = res.size() - 1; ~i; --i) { if (i) res[i - 1] += res[i] % k * 10; res[i] /= k; } adj(res); return res; } friend LargeNumber operator%(LargeNumber x, int k) { return x - x / k * k; } friend LargeNumber operator+(LargeNumber x, LargeNumber y) { if (!x.sign) return y; if (!y.sign) return x; if (x.sign == 1 && y.sign == 1) return add(x, y); if (x.sign == -1 && y.sign == -1) { LargeNumber res = add(x, y); res.sign = -1; return res; } if (x.sign == -1 && y.sign == 1) swap(x, y); y.sign = 1; if (x < y) { LargeNumber res = dec(y, x); res.sign = -1; return res; } return dec(x, y); } friend LargeNumber operator-(LargeNumber x, LargeNumber y) { return x + (-y); } friend LargeNumber operator^(LargeNumber b, LargeNumber k) { LargeNumber res(1); for (; k.sign == 1; b = b * b, div2(k)) if (k[0] & 1) res = res * b; return res; } friend LargeNumber operator++(LargeNumber &x) { return x = (x + LargeNumber(1)); } friend LargeNumber operator--(LargeNumber &x) { return x = (x - LargeNumber(1)); } friend LargeNumber operator+=(LargeNumber &x, const LargeNumber y) { return x = (x + y); } friend LargeNumber operator-=(LargeNumber &x, const LargeNumber y) { return x = (x - y); } friend LargeNumber operator*=(LargeNumber &x, const LargeNumber y) { return x = (x * y); } friend LargeNumber operator/=(LargeNumber &x, const int y) { return x = (x / y); } friend LargeNumber operator%=(LargeNumber &x, const int y) { return x = (x % y); } friend LargeNumber operator^=(LargeNumber &x, const LargeNumber y) { return x = (x ^ y); } friend void swap(LargeNumber &x, LargeNumber &y) { LargeNumber tmp = x; x = y, y = tmp; } friend LargeNumber abs(LargeNumber x) { if (x.sign < 0) x.sign = 1; return x; } friend void chkmin(LargeNumber &x, LargeNumber k) { if (k < x) x = k; } friend void chkmax(LargeNumber &x, LargeNumber k) { if (k > x) x = k; } friend LargeNumber min(const LargeNumber &x, const LargeNumber &y) { return x > y ? y : x; } friend LargeNumber max(const LargeNumber &x, const LargeNumber &y) { return x > y ? x : y; } };
|