

FFT vs Karatsuba: need help
source link: https://codeforces.com/blog/entry/111369
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Hi guys. I've been trying to learn fast polynomial multiplication. I found that the most famous(probably?) algorithms are FFT and Karatsuba algorithm.
Karatsuba algorithm is pretty easy, but it is slow. I couldn't submit few problems because of TLE. Even if a submission passed, it was on the edge between AC and TLE.
On the other side, FFT is much faster but also much more complex. By complex I mean that it's hard to truly understand and implement it.
I need an advice: what should I do? Should I stick with Karatsuba algorithm and learn how to implement it better? Or should I make one more try to understand FFT? If yes, where is the best place to learn it without much pain?
6 hours ago, # | FFT will be faster than Karatsuba for large instance, regardless on how efficient you implement Karatsuba. Polynomial multiplication problems are kinda rare in competitive programming, but most of them will require you to use FFT. So if you want to solve those you need to learn FFT. E.g. on https://cp-algorithms.com/algebra/fft.html (shameless self-promotion). |
4 hours ago, # | Apart from plain polynomial multiplication, FFT and Karatsuba just have slightly different purposes. Karatsuba pros:
Karatsuba cons:
FFT pros:
FFT cons:
So, in general, there are problems that are solvable by one algorithm, but not another and vice versa, FFT is a bit more common, but knowing Karatsuba may help you in cases when you need to multiply modulo 264264, for example. It is doable with FFT, but much harder. There was a problem about that on some TooSimple's contest in PTZ, presumably, in 2018. |
Historically, we (at least for Chinese) cannot effectively compute the multiplication of two polynomials modulo 109+7109+7 date back to 2014. In these old days, FFT (with complex) isn't doable because it loss precision for number of magnitude n×1018n×1018. We should have tried the so-called "three-mods NTT" which is to pick three NTT primes p1p1, p2p2, p3p3 fitting in int32_t and reconstruct the result using Chinese Remainder Theorem. The "three-mods" approach requires 99 calls to NTT, which is slow and hardly useful. In such case, we switched to Karatsuba multiplication, However, after a clever guy figuring out (who exactly? I may have learned the trick from some CodeChef submission) that we can split a 109109 into something like 105×65536+105105×65536+105, and utilize the real parts of complex to save constants, the Karatsuba thing become less useful. Last note: Also there is cultural shift in competitive programming community. Dating back our favorite prime is 109+7109+7 (and something 109+9109+9), you can check old TC tasks for the fact. Today, seemingly everything should be computed under modulo 998′244′353998′244′353 (Thanks to uoj.ac). |
Recommend
-
7
[Submitted on 15 Apr 2019] Asymptotically Efficient Quantum Karatsuba Multiplication Craig Gidney...
-
10
Friday Q&A 2012-11-02Loading [MathJax]/jax/output/HTML-CSS/jax.js mikeash.com: just this guy, you know? Friday Q&A 2012-11-02: Building the FFT by
-
4
0:00 / 0:57 ...
-
7
F# Algorithms Illuminated: Karatsuba multiplication 2/2 The second and final part of the posts explaining our functional impl...
-
6
在上一篇博文中,我们谈到了一种利用分治法优化整数乘法的算法,它的时间复杂度约为O(n1.59),而本文将介绍另一种基于快速傅立叶变换(Fast Fourier Transform, FFT)的整数乘法,它的时间复杂度更低,为O(nlogn)。这个方法将整数乘法和多项式乘法联系起来,而...
-
6
C++实现裸音频数据的FFT变换_shixin_0125的技术博客_51CTO博客#include "waveconvertor.h" // // class CWaveConvertor /* Pjotr '87. */ // As best as I can determine this is in the public domain. N...
-
3
C#实现FFT(递归法) 1. C#实现复数类 我们在进行信号分析的时候,难免会使用到复数。但是遗憾的是,C#没有自带的复数类,以下提供了一种复数类的构建方法。 复数相比于实数,可...
-
7
-is-this-fft-'s blog [Tutori...
-
6
点值表示法 我们正常表示一个多项式的方式,形如 A(x)=a0+a1x+a2x2+...+anxnA(x)=a0+a1x
-
8
Tutorial on FFT/NTT — The tough made simple. ( Part 1 ) Tutorial on FFT/NTT — The tough made simple. ( Part 1 ) ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK