zoukankan      html  css  js  c++  java
  • 【模板】多项式对数函数

    代码如下

    #include <bits/stdc++.h>
    
    using namespace std;
    
    typedef long long LL;
    
    const LL mod = 998244353, g = 3, ig = 332748118;
    
    inline LL fpow(LL a, LL b) {
    	LL ret = 1 % mod;
    	for (; b; b >>= 1, a = a * a % mod) {
    		if (b & 1) {
    			ret = ret * a % mod;
    		}
    	}
    	return ret;
    }
    
    void ntt(vector<LL> &v, vector<int> &rev, int opt) {
    	int tot = v.size();
    	for (int i = 0; i < tot; i++) if (i < rev[i]) swap(v[i], v[rev[i]]);
    	for (int mid = 1; mid < tot; mid <<= 1) {
    		LL wn = fpow(opt == 1 ? g : ig, (mod - 1) / (mid << 1));
    		for (int j = 0; j < tot; j += mid << 1) {
    			LL w = 1;
    			for (int k = 0; k < mid; k++) {
    				LL x = v[j + k], y = v[j + mid + k] * w % mod;
    				v[j + k] = (x + y) % mod, v[j + mid + k] = (x - y + mod) % mod;
    				w = w * wn % mod;
    			}
    		}
    	}
    	if (opt == -1) {
    		LL itot = fpow(tot, mod - 2);
    		for (int i = 0; i < tot; i++) v[i] = v[i] * itot % mod;
    	}
    }
    vector<LL> derivative(vector<LL> &a) {
    	vector<LL> b(a.size());
    	for (int i = 1; i < a.size(); i++) b[i - 1] = i * a[i] % mod;
    	return b;
    }
    vector<LL> integration(vector<LL> &a) {
    	vector<LL> b(a.size());
    	for (int i = 1; i < a.size(); i++) b[i] = a[i - 1] * fpow(i, mod - 2) % mod;
    	return b;
    }
    vector<LL> convolution(vector<LL> &a, int cnta, vector<LL> &b, int cntb, const function<LL(LL, LL)> &calc) {
    	int bit = 0, tot = 1;
    	while (tot <= 2 * max(cnta, cntb)) bit++, tot <<= 1;
    	vector<int> rev(tot);
    	for (int i = 0; i < tot; i++) rev[i] = rev[i >> 1] >> 1 | (i & 1) << (bit - 1);
    	vector<LL> foo(tot), bar(tot);
    	for (int i = 0; i < cnta; i++) foo[i] = a[i];
    	for (int i = 0; i < cntb; i++) bar[i] = b[i];
    	ntt(foo, rev, 1), ntt(bar, rev, 1);
    	for (int i = 0; i < tot; i++) foo[i] = calc(foo[i], bar[i]);
    	ntt(foo, rev, -1);
    	return foo;
    }
    void Inv(int n, vector<LL> &a, vector<LL> &b) {
    	if (n == 1) {b[0] = fpow(a[0], mod - 2);return;}
    	int mid = (n + 1) >> 1;
    	Inv(mid, a, b);
    	vector<LL> bar = convolution(a, n, b, mid, [&](LL a, LL b) {return b * (2 - a * b % mod + mod) % mod;});
    	for (int i = 0; i < n; i++) b[i] = bar[i];
    }
    vector<LL> iexp(vector<LL> &a, int n) {
    	vector<LL> da = derivative(a);
    	vector<LL> ia(n);
    	Inv(n, a, ia);
    	vector<LL> b = convolution(ia, n, da, n, [&](LL a, LL b) {return a * b % mod;});
    	b.resize(n);
    	return integration(b);
    }
    
    int main() {
    	ios::sync_with_stdio(false);
    	cin.tie(0), cout.tie(0);
    	int n;
    	cin >> n;
    	vector<LL> a(n);
    	for (int i = 0; i < n; i++) {
    		cin >> a[i];
    	}
    	vector<LL> b = iexp(a, n);
    	for (int i = 0; i < n; i++) {
    		cout << b[i] << " ";
    	}
    	return 0;
    } 
    
  • 相关阅读:
    一些开源工具软件
    被伤过的心还可以爱谁
    C函数检测CPU架构为LittleEndian或BigEndian
    C#打印(接录)
    C#Lambda表达式参考
    C#反射 创建对象,调用方法
    datagridview cell 验证事件
    我多想抱着你哭
    C#获取属性
    STM32的位操作的方法
  • 原文地址:https://www.cnblogs.com/wzj-xhjbk/p/11656443.html
Copyright © 2011-2022 走看看