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
| #include <bits/stdc++.h>
using namespace std;
namespace Poly { #define int long long #define vec vector <int> const int mod = 1004535809; const int g = 3; const int gi = 334845270; const int N = 6e5 + 10;
int recover[N]; int qpow(int a, int b) { int t = 1; while(b != 0) { if(b & 1)t = t * a % mod; a = a * a % mod; b >>= 1; } return t; } int inv(int x) { return qpow(x, mod - 2);} void init(int n, int m, int &len) { len = 1; int cnt = 0; while(len <= (n + m))len <<= 1, cnt ++; for(int i = 0; i < len; i++) recover[i] = (recover[i >> 1] >> 1) | ((i & 1) << (cnt - 1)); } void NTT(vec &a, int len, int type) { for(int i = 0; i < len; i++) if(i < recover[i])swap(a[i], a[recover[i]]); for(int k = 1; k < len; k <<= 1) { int x = qpow(type == 1 ? g : gi, (mod - 1) / (k << 1)); for(int i = 0; i < len; i += (k << 1)) { int w = 1; for(int j = 0; j < k; j++) { int y = a[i + j] % mod; int z = w * a[i + j + k] % mod; a[i + j] = (y + z) % mod; a[i + j + k] = ((y - z) % mod + mod) % mod; w = w * x % mod; } } } if(type == -1) { int iv = inv(len); for(int i = 0; i < len; i++) a[i] = a[i] * iv % mod; } } struct poly { vector <int> v; int len; poly(){v.resize(N); len = 0;} void clear(int n){v.clear(); v.resize(N); len = n;} void length(int n){len = n;} void memset0(int l, int r){for(int i = l; i < r; i++)v[i] = 0;} void print(int n){for(int i = 0; i < n; i++)printf("%lld ", v[i]); printf("\n");} friend poly operator + (poly A, poly B) { A.length(max(A.len, B.len)); for(int i = 0; i <= A.len; i++) A.v[i] = (A.v[i] + B.v[i]) % mod; return A; } friend poly operator - (poly A, poly B) { A.length(max(A.len, B.len)); for(int i = 0; i <= A.len; i++) A.v[i] = ((A.v[i] - B.v[i]) % mod + mod) % mod; return A; } friend poly operator * (poly A, poly B) { int len; init(A.len, B.len, len); NTT(A.v, len, 1), NTT(B.v, len, 1); for(int i = 0; i < len; i++) A.v[i] = (A.v[i] * B.v[i]) % mod; NTT(A.v, len, -1); A.len += B.len; return A; } }; vec tmp; void inverse(poly &A, poly &B, int n) { if(n == 1){B.v[0] = inv(A.v[0]);return;} inverse(A, B, (n + 1) >> 1); int len; init(n, n, len); tmp.clear(); tmp.resize(len); for(int i = 0; i < n; i++) tmp[i] = A.v[i]; NTT(tmp, len, 1), NTT(B.v, len, 1); for(int i = 0; i < len; i++) B.v[i] = (2 - B.v[i] * tmp[i] % mod + mod) % mod * B.v[i] % mod; NTT(B.v, len, -1); for(int i = n; i < len; i++)B.v[i] = 0; } void diff(poly &A, poly &B, int n) { for(int i = 1; i < n; i++) B.v[i - 1] = i * A.v[i] % mod; B.v[n - 1] = 0; B.length(n); } void integ(poly &A, poly &B, int n) { for(int i = 1; i < n; i++) B.v[i] = A.v[i - 1] * inv(i) % mod; B.v[0] = 0; B.length(n); } poly C, D, E, F, G, H, I; void Ln(poly &A, poly &B, int n) { E.clear(n); F.clear(n); diff(A, E, n); inverse(A, F, n); E = E * F; integ(E, B, n); B.length(n); } void Exp(poly &A, poly &B, int n) { if(n == 1){B.v[0] = 1; return;} Exp(A, B, (n + 1) >> 1); int len; init(n, n, len); C.clear(n); D.clear(n); C.v[0] = 1; Ln(B, D, n); C = B * (C + A - D); for(int i = 0; i < n; i++)B.v[i] = C.v[i]; for(int i = n; i < len; i++)B.v[i] = 0; } const int inv2 = inv(2); void Sqrt(poly &A, poly &B, int n) { if(n == 1){B.v[0] = 1; return;} Sqrt(A, B, (n + 1) >> 1); int len; init(n, n, len); G.clear(n); H.clear(n); inverse(B, H, n); for(int i = 0; i < n; i++)G.v[i] = A.v[i]; NTT(H.v, len, 1), NTT(B.v, len, 1), NTT(G.v, len, 1); for(int i = 0; i < len; i++) B.v[i] = (B.v[i] + G.v[i] * H.v[i] % mod) % mod * inv2 % mod; NTT(B.v, len, -1); for(int i = n; i < len; i++)B.v[i] = 0; } void Pow(poly &A, poly &B, int n, int k) { I.clear(n); Ln(A, I, n); for(int i = 0; i < n; i++)(I.v[i] *= k) %= mod; Exp(I, B, n); } #undef int
}
using namespace Poly;
#define int long long
int n;
int fac[N], ifac[N];
int c(int n, int m) { if(n < m)return 0; return n * (n - 1) / 2 % (mod - 1); }
signed main() { scanf("%lld", &n); n = n + 1; fac[0] = ifac[0] = 1; for(int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % mod; for(int i = 1; i <= n; i++) ifac[i] = inv(fac[i]); poly F, G, H; G.clear(n); for(int i = 0; i < n; i++) G.v[i] = qpow(2, c(i, 2)) * ifac[i] % mod; H.clear(n); for(int i = 1; i < n; i++) H.v[i] = qpow(2, c(i, 2)) * ifac[i - 1] % mod; F.clear(n); inverse(G, F, n); F = F * H; printf("%lld", F.v[n - 1] * fac[n - 2] % mod); return 0; }
|