码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 高精度乘法模板(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. }

  • 相关阅读:
    计算机概述
    海岸雷达问题(java实现)——贪心算法例题
    LeetCode每日一练 —— 876. 链表的中间结点
    开发ABAP程序中的错误
    FM5889协议系列-识别加限流IC 多口USB超级充电器
    MyBatis篇---第一篇
    pinyin-match选择器拼音快速检索目标
    Prometheus集成springboot(七)
    Spring学习笔记(三十六)——SpringBoot 实现大文件分片上传、断点续传及秒传
    9_帖子详情
  • 原文地址:https://blog.csdn.net/m0_74315028/article/details/132657006
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号