• 高精度乘法模板(fft)


     正常高精度复杂度是o(n^2),fft复杂度o(nlogn)

    1. #define int long long//__int128 2^127-1(GCC)
    2. #define PII pair
    3. #define f first
    4. #define s second
    5. using namespace std;
    6. const int inf = 0x3f3f3f3f3f3f3f3f, N = 3e5 + 5, mod = 1e9 + 7;
    7. const double PI = acos(-1);
    8. int n, m;
    9. struct Complex
    10. {
    11. double x, y;
    12. Complex operator+ (const Complex& t) const
    13. {
    14. return { x + t.x, y + t.y };
    15. }
    16. Complex operator- (const Complex& t) const
    17. {
    18. return { x - t.x, y - t.y };
    19. }
    20. Complex operator* (const Complex& t) const
    21. {
    22. return { x * t.x - y * t.y, x * t.y + y * t.x };
    23. }
    24. }a[N], b[N];
    25. int rev[N], bit, tot;
    26. void fft(Complex a[], int inv)
    27. {
    28. for (int i = 0; i < tot; i++)
    29. if (i < rev[i])
    30. swap(a[i], a[rev[i]]);
    31. for (int mid = 1; mid < tot; mid <<= 1)
    32. {
    33. auto w1 = Complex({ cos(PI / mid), inv * sin(PI / mid) });
    34. for (int i = 0; i < tot; i += mid * 2)
    35. {
    36. auto wk = Complex({ 1, 0 });
    37. for (int j = 0; j < mid; j++, wk = wk * w1)
    38. {
    39. auto x = a[i + j], y = wk * a[i + j + mid];
    40. a[i + j] = x + y, a[i + j + mid] = x - y;
    41. }
    42. }
    43. }
    44. }
    45. signed main() {
    46. ios_base::sync_with_stdio(0);
    47. cin.tie(0), cout.tie(0);
    48. string aa, bb;
    49. cin >> aa >> bb;
    50. n = aa.size()-1, m = bb.size()-1;
    51. for (int i = 0; i <= n; i++) { a[i].x = aa[i] - '0'; }
    52. for (int i = 0; i <= m; i++) { b[i].x = bb[i] - '0'; }
    53. while ((1 << bit) < n + m + 1) bit++;
    54. tot = 1 << bit;
    55. for (int i = 0; i < tot; i++) {
    56. rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (bit - 1));
    57. }
    58. fft(a, 1), fft(b, 1);
    59. for (int i = 0; i < tot; i++) a[i] = a[i] * b[i];
    60. fft(a, -1);
    61. string s;
    62. int t=0;
    63. for (int i = n+m; i >= 0; i--) {
    64. t+=(int)(a[i].x / tot + 0.5);
    65. s+=t%10+'0';
    66. t/=10;
    67. }
    68. if(t) s+=t+'0';
    69. reverse(s.begin(),s.end());
    70. cout<
    71. }

  • 相关阅读:
    TcpServer::start都做了些什么
    矩阵全相等三角形——枚举
    React复习笔记
    HTML CSS JS 画的效果图
    vue3+electron+typescript 项目安装、打包、多平台踩坑记录-mac+linux(包括国产化系统)
    持久内存BTT实现及优化(一)
    计算机网络与互联网
    常用的Dos命令
    OceanBase 4.2 主备库 FailOver 与 SwitchOver 概念
    Java基础:Java基本概念
  • 原文地址:https://blog.csdn.net/m0_74315028/article/details/132657006