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
|
#include <cstdio> #include <algorithm> using namespace std;
const int MAXN = 131075 << 1; const int P = 167772161, G = 3, iG = 55924054;
int fac[MAXN], ifac[MAXN], inv[MAXN];
int fpow(int base, int b, int m = P) { int ret = 1; while (b > 0) { if (b & 1) ret = 1LL * ret * base % m; base = 1LL * base * base % m, b >>= 1; } return ret; }
namespace Poly { int r[MAXN]; inline void init(const int& Lim, const int& L) { for (int i = 1; i < Lim; ++i) r[i] = (r[i>>1] >> 1) | ((i & 1) << (L-1)); }
void NTT(int* f, int Lim, int type) { for (int i = 1; i < Lim; ++i) if (i < r[i]) swap(f[i], f[r[i]]); for (int Mid = 1; Mid < Lim; Mid <<= 1) { int unit = fpow(type > 0? G: iG, (P-1) / (Mid << 1)); for (int i = 0; i < Lim; i += Mid << 1) { int w = 1; for (int j = 0; j < Mid; ++j, w = 1LL * w * unit % P) { int f0 = f[i+j], f1 = 1LL * w * f[i+j+Mid] % P; f[i+j] = (f0 + f1) % P, f[i+j+Mid] = (f0 - f1 + P) % P; } } } if (type < 0) { int inv = fpow(Lim, P-2); for (int i = 0; i < Lim; ++i) f[i] = 1LL * inv * f[i] % P; } }
void Inv(int* f, int* g, int n) { static int A[MAXN], B[MAXN]; g[0] = fpow(f[0], P-2); for (int L = 0, Lim = 1, Mid = 2; Mid < 2*n; Mid <<= 1) { while (Lim < 2*Mid) Lim <<= 1, ++L; for (int i = 0; i < Mid; ++i) A[i] = f[i], B[i] = g[i]; for (int i = Mid; i < Lim; ++i) A[i] = B[i] = 0; init(Lim, L), NTT(A, Lim, 1), NTT(B, Lim, 1); for (int i = 0; i < Lim; ++i) g[i] = ((B[i] + B[i]) % P - 1LL * A[i] * B[i] % P * B[i] % P + P) % P; NTT(g, Lim, -1); for (int i = min(Mid, n); i < Lim; ++i) g[i] = 0; } }
void Der(int* f, int* g, int n) { for (int i = 1; i < n; ++i) g[i-1] = 1LL * i * f[i] % P; g[n-1] = 0; } void Int(int* f, int* g, int n) { g[0] = 0; for (int i = n-1; i; --i) g[i] = 1LL * inv[i] * f[i-1] % P; } void Ln(int* f, int* g, int n) { static int A[MAXN], B[MAXN]; Der(f, A, n), Inv(f, B, n); int Lim = 1, L = 0; while (Lim < 2*n) Lim <<= 1, ++L; for (int i = n; i < Lim; ++i) A[i] = B[i] = 0; init(Lim, L), NTT(A, Lim, 1), NTT(B, Lim, 1); for (int i = 0; i < Lim; ++i) A[i] = 1LL * A[i] * B[i] % P; NTT(A, Lim, -1), Int(A, g, n); }
void Exp(int* f, int* g, int n) { static int A[MAXN], B[MAXN], lng[MAXN]; g[0] = 1; for (int L = 0, Lim = 1, Mid = 2; Mid < 2*n; Mid <<= 1) { Ln(g, lng, Mid); while (Lim < 2*Mid) Lim <<= 1, ++L; for (int i = 0; i < Mid; ++i) A[i] = (f[i] - lng[i] + P) % P, B[i] = g[i]; A[0] = (A[0] + 1) % P; for (int i = Mid; i < Lim; ++i) A[i] = B[i] = 0; init(Lim, L), NTT(A, Lim, 1), NTT(B, Lim, 1); for (int i = 0; i < Lim; ++i) g[i] = 1LL * A[i] * B[i] % P; NTT(g, Lim, -1); for (int i = min(Mid, n); i < Lim; ++i) g[i] = 0; } }
void Pow(int* f, int* g, int n, int K) { static int lng[MAXN]; int pos = 0; while (!f[pos]) ++pos; int Mid = n - pos; for (int i = 0; i < Mid; ++i) g[i] = f[i+pos]; int base = g[0], inv = fpow(base, P-2); for (int i = 0; i < Mid; ++i) g[i] = 1LL * g[i] * inv % P; Ln(g, lng, Mid); for (int i = 0; i < Mid; ++i) lng[i] = 1LL * lng[i] * K % P; Exp(lng, g, Mid); base = fpow(base, K); for (int i = 0; i < Mid; ++i) g[i] = 1LL * g[i] * base % P; pos = min(pos * K, n); for (int i = n-1; i >= pos; --i) g[i] = g[i-pos]; for (int i = 0; i < pos; ++i) g[i] = 0; } }
int n, K; int f[MAXN];
int main() { #ifndef ONLINE_JUDGE freopen("input.in", "r", stdin); #endif scanf("%d%d", &n, &K); int N = n; ifac[0] = fac[0] = inv[1] = 1; for (int i = 2; i <= N; ++i) inv[i] = 1LL * (P - P / i) * inv[P % i] % P; for (int i = 1; i <= N; ++i) fac[i] = 1LL * i * fac[i-1] % P; ifac[N] = fpow(fac[N], P-2); for (int i = N; i; --i) ifac[i-1] = 1LL * i * ifac[i] % P; f[0] = f[1] = 1; Poly::Ln(f, f, n+1); Poly::Pow(f, f, n+1, K); for (int i = 0; i <= n; ++i) f[i] = 1LL * f[i] * ifac[K] % P; for (int i = 0; i <= n; ++i) f[i] = 1LL * f[i] * fac[i] % P * (((i-K) & 1)? P-1: 1) % P; for (int i = 0; i <= n; ++i) printf("%d%c", f[i], " \n"[i==n]); return 0; }
|