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
| #include<bits/stdc++.h> #define LL long long using namespace std; const int N = 3e5 + 5, Mod = 998244353, G = 3; int rev[N], a[N], b[N], n; inline void calc(int bit) { for(int i = 0;i < (1 << bit);i ++) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (bit - 1)); } inline LL mod(LL x) { return (x %= Mod) < 0 ? x + Mod : x; } LL qpow(LL a, LL k) { LL res = 1; while(k) { if(k & 1) res = (res * a) % Mod; k >>= 1; a = (a * a) % Mod; } return res; } void NTT(int a[], int bit, int inv) { calc(bit); int tot = (1 << bit); for(int i = 0;i < tot;i ++) if(rev[i] < i) swap(a[rev[i]], a[i]); for(int mid = 1;mid < tot; mid <<= 1) { int gn = qpow(G, (Mod - 1) / (mid << 1)); if(inv == -1) gn = qpow(gn, Mod - 2); for(int i = 0; i < tot; i += mid * 2) { int g = 1; for(int j = 0;j < mid;j ++, g = 1ll * g * gn % Mod) { int x = a[i + j], y = 1ll * g * a[i + j + mid] % Mod; a[i + j] = (x + y) % Mod, a[i + j + mid] = mod(x - y); } } } if(inv == 1) return ; LL Inv = qpow(tot, Mod - 2); for(int i = 0;i < tot;i ++) a[i] = 1ll * a[i] % Mod * Inv % Mod; }
void get_ni(int len, int a[], int b[]) { static int c[N]; if(len == 1) { b[0] = qpow(a[0], Mod - 2); return ; } get_ni((len + 1) >> 1, a, b); int bit = 0, tot; while((1 << bit) < (len << 1)) bit ++; tot = (1 << bit); for(int i = 0;i < tot;i ++) if(i < len) c[i] = a[i]; else c[i] = 0; NTT(c, bit, 1), NTT(b, bit, 1); for(int i = 0;i < tot;i ++) b[i] = mod(mod(2ll - 1ll * c[i] * b[i] % Mod) * b[i]); NTT(b, bit, -1); for(int i = len;i < tot;i ++) b[i] = 0; } int main() { cin >> n; for(int i = 0;i < n;i ++) scanf("%lld", &a[i]); get_ni(n, a, b); for(int i = 0;i < n;i ++) printf("%d ", b[i]); return 0; }
|