洛谷P4725 【模板】多项式对数函数(多项式ln)
程序员文章站
2022-05-18 23:17:53
题意 "题目链接" Sol ~~这个不用背XD~~ 前置知识: $f(x) = ln(x), f'(x) = \frac{1}{x}$ $f(g(x)) = f'(g(x)) g'(x)$ 我们要求的是$G(x) = F(A(x)), F(x) = ln(x)$ 可以直接对两边求导$G'(A(x)) ......
题意
sol
这个不用背xd
前置知识:
\(f(x) = ln(x), f'(x) = \frac{1}{x}\)
\(f(g(x)) = f'(g(x)) g'(x)\)
我们要求的是\(g(x) = f(a(x)), f(x) = ln(x)\)
可以直接对两边求导\(g'(a(x)) = f'(a(x))a'(x) = \frac{a(x)}{a'(x)}\)
发现这个可以算,只要求个逆就行了。
那么就直接求导之后积分回去,复杂度\(o(nlogn)\)
#include<bits/stdc++.h> #define pair pair<int, int> #define mp(x, y) make_pair(x, y) #define fi first #define se second #define ll long long #define ull unsigned long long #define fin(x) {freopen(#x".in","r",stdin);} #define fout(x) {freopen(#x".out","w",stdout);} using namespace std; const int maxn = 4e5 + 10, inf = 1e9 + 10; const double eps = 1e-9, pi = acos(-1); inline int read() { char c = getchar(); int x = 0, f = 1; while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();} while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } int a[maxn], b[maxn]; namespace poly { int rev[maxn], gpow[maxn], gipow[maxn], a[maxn], b[maxn], c[maxn], d[maxn], lim; const int g = 3, gi = 332748118, mod = 998244353; template <typename a, typename b> inline ll add(a x, b y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;} template <typename a, typename b> inline void add2(a &x, b y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);} template <typename a, typename b> inline ll mul(a x, b y) {return 1ll * x * y % mod;} template <typename a, typename b> inline void mul2(a &x, b y) {x = (1ll * x * y % mod + mod) % mod;} int fp(int a, int p, int p = mod) { int base = 1; for(; p; p >>= 1, a = 1ll * a * a % p) if(p & 1) base = 1ll * base * a % p; return base; } int getlen(int x) { int lim = 1; while(lim < x) lim <<= 1; return lim; } int getorigin(int x) {//¼æëãô¸ù static int q[maxn]; int tot = 0, tp = x - 1; for(int i = 2; i * i <= tp; i++) if(!(tp % i)) {q[++tot] = i;while(!(tp % i)) tp /= i;} if(tp > 1) q[++tot] = tp; for(int i = 2, j; i <= x - 1; i++) { for(j = 1; j <= tot; j++) if(fp(i, (x - 1) / q[j], x) == 1) break; if(j == tot + 1) return i; } } void init(/*int p,*/ int lim) { //mod = p; g = getorigin(mod); gi = fp(g, mod - 2); for(int i = 1; i <= lim; i++) gpow[i] = fp(g, (mod - 1) / i), gipow[i] = fp(gi, (mod - 1) / i); } void ntt(int *a, int lim, int opt) { int len = 0; for(int n = 1; n < lim; n <<= 1) ++len; for(int i = 1; i <= lim; i++) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (len - 1)); for(int i = 0; i <= lim; i++) if(i < rev[i]) swap(a[i], a[rev[i]]); for(int mid = 1; mid < lim; mid <<= 1) { int wn = (opt == 1 ? gpow[mid << 1] : gipow[mid << 1]); for(int i = 0; i < lim; i += (mid << 1)) { for(int j = 0, w = 1; j < mid; j++, w = mul(w, wn)) { int x = a[i + j], y = mul(w, a[i + j + mid]); a[i + j] = add(x, y), a[i + j + mid] = add(x, -y); } } } if(opt == -1) { int inv = fp(lim, mod - 2); for(int i = 0; i <= lim; i++) mul2(a[i], inv); } } void mul(int *a, int *b, int n, int m) { memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b)); int lim = 1, len = 0; while(lim <= n + m) len++, lim <<= 1; for(int i = 0; i <= n; i++) a[i] = a[i]; for(int i = 0; i <= m; i++) b[i] = b[i]; ntt(a, lim, 1); ntt(b, lim, 1); for(int i = 0; i <= lim; i++) b[i] = mul(b[i], a[i]); ntt(b, lim, -1); for(int i = 0; i <= n + m; i++) b[i] = b[i]; } void inv(int *a, int *b, int len) {//b1 = 2b - a1 * b^2 if(len == 1) {b[0] = fp(a[0], mod - 2); return ;} inv(a, b, len >> 1); for(int i = 0; i < len; i++) a[i] = a[i], b[i] = b[i]; ntt(a, len << 1, 1); ntt(b, len << 1, 1); for(int i = 0; i < (len << 1); i++) mul2(a[i], mul(b[i], b[i])); ntt(a, len << 1, -1); for(int i = 0; i < len; i++) add2(b[i], add(b[i], -a[i])); for(int i = 0; i < (len << 1); i++) a[i] = b[i] = 0; } void dao(int *a, int *b, int len) { for(int i = 1; i < len; i++) b[i - 1] = mul(i, a[i]); } void ji(int *a, int *b, int len) { for(int i = 1; i < len; i++) b[i] = mul(a[i - 1], fp(i, mod - 2)); } void ln(int *a, int *b, int len) { dao(a, c, len); inv(a, d, len); ntt(c, len << 1, 1); ntt(d, len << 1, 1); for(int i = 0; i < (len << 1); i++) d[i] = mul(c[i], d[i]); ntt(d, len << 1, -1); ji(d, b, len << 1); } /* void polysqrt(int *a, int *b, int len) {//b1 = \frac{1}{2} (b + a / b) if(len == 1) {b[0] = }; } */ }; using namespace poly; signed main() { int n = read(); for(int i = 0; i < n; i++) a[i] = read(); init(4 * n); ln(a, b, getlen(n)); for(int i = 0; i < n; i++) cout << b[i] << " "; return 0; }
上一篇: L2-004 这是二叉搜索树吗?