• 【模板】FFT


    FFT 的本质是在 (O(nlogn)) 的时间内进行点值表达和系数表达之间的转换,并在 (O(n)) 的时间内计算结果,故总时间复杂度为 (O(logn))
    FFT注意事项

    • 由于 tot 采用的是倍增的方式,因此实际内存开销可能是 (2(n+m))
    • 多项式的项数和次数不同。

    update at 2019.10.11
    代码如下

    #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> 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;
    }
    
    int main() {
    	ios::sync_with_stdio(false);
    	cin.tie(0), cout.tie(0);
    	int n, m;
    	cin >> n >> m;
    	vector<LL> a(n + 1), b(m + 1);
    	for (int i = 0; i <= n; i++) {
    		cin >> a[i];
    	}
    	for (int i = 0; i <= m; i++) {
    		cin >> b[i];
    	}
    	vector<LL> c = convolution(a, n + 1, b, m + 1, [&](LL a, LL b) {
    		return a * b % mod;
    	});
    	for (int i = 0; i <= n + m; i++) {
    		cout << c[i] << " ";
    	}
    	return 0;
    } 
    
  • 相关阅读:
    java实现快速排序
    java实现二叉树
    hudson——持续集成
    Oracle存储过程的数组参数
    在linux系统下建立artifactory管理maven库
    关于排错:专注思考,细心观察,步步为营
    在关键字'('附近有语法错误 Incorrect syntax near '(' in sql server table values function
    快速将PSD文件生成WordPress主题模板Divine
    SQLserver 复制分发( 发布与订阅) 疑难杂症 Replication (Publications,Subscriptions)
    Windows8将撼动笔记本电脑市场?
  • 原文地址:https://www.cnblogs.com/wzj-xhjbk/p/10808598.html
Copyright © 2020-2023  润新知