FFT模板题,求A*B。

用次FFT模板需要注意的是,N应为2的幂次,不然二进制平摊反转置换会出现死循环。

取出结果值时注意精度,要加上eps才能A。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cmath>
  4. #include <algorithm>
  5. using namespace std;
  6. typedef long long ll;
  7. const double pi = acos(-1.0);
  8. const int maxn = 50000 + 5;
  9. const double eps = 1e-6;
  10.  
  11. struct Complex {
  12. double a, b;
  13. Complex() {
  14. }
  15. Complex(double a, double b) :
  16. a(a), b(b) {
  17. }
  18. Complex operator +(const Complex& t) const {
  19. return Complex(a + t.a, b + t.b);
  20. }
  21. Complex operator -(const Complex& t) const {
  22. return Complex(a - t.a, b - t.b);
  23. }
  24. Complex operator *(const Complex& t) const {
  25. return Complex(a * t.a - b * t.b, a * t.b + b * t.a);
  26. }
  27. };
  28.  
  29. // 二进制平摊反转置换
  30. void brc(Complex *x, int n) {
  31. int i, j, k;
  32. for (i = 1, j = n >> 1; i < n - 1; i++) {
  33. if (i < j)
  34. swap(x[i], x[j]);
  35.  
  36. k = n >> 1;
  37. while (j >= k) {
  38. j -= k;
  39. k >>= 1;
  40. }
  41. if (j < k)
  42. j += k;
  43. }
  44. }
  45.  
  46. // FFT,其中on==1时为DFT,on==-1时为IDFT
  47. void FFT(Complex *x, int n, int on) {
  48. int h, i, j, k, p;
  49. double r;
  50. Complex u, t;
  51. brc(x, n);
  52. for (h = 2; h <= n; h <<= 1) { // 控制层数
  53. r = on * 2.0 * pi / h;
  54. Complex wn(cos(r), sin(r));
  55. p = h >> 1;
  56. for (j = 0; j < n; j += h) {
  57. Complex w(1, 0);
  58. for (k = j; k < j + p; k++) {
  59. u = x[k];
  60. t = w * x[k + p];
  61. x[k] = u + t;
  62. x[k + p] = u - t;
  63. w = w * wn;
  64. }
  65. }
  66. }
  67. if (on == -1) // IDFT
  68. for (i = 0; i < n; i++)
  69. x[i].a = x[i].a / n + eps;
  70. }
  71.  
  72. int n, ma, N;
  73. Complex x1[maxn<<2], x2[maxn<<2];
  74. char sa[maxn], sb[maxn];
  75. int ans[maxn<<1];
  76.  
  77. void solve() {
  78. int n1 = strlen(sa), n2 = strlen(sb);
  79. int N = 1, tmpn = max(n1, n2) << 1;
  80. // N应为2的幂次
  81. while(N < tmpn) N <<= 1;
  82. for(int i = 0;i < N; i++)
  83. x1[i].a = x1[i].b = x2[i].a = x2[i].b = 0;
  84. for(int i = 0;i < n1; i++)
  85. x1[i].a = sa[n1-i-1] - '0';
  86. for(int i = 0;i < n2; i++)
  87. x2[i].a = sb[n2-i-1] - '0';
  88. FFT(x1, N, 1); FFT(x2, N, 1);
  89. for(int i = 0;i < N; i++)
  90. x1[i] = x1[i]*x2[i];
  91. FFT(x1, N, -1);
  92. int pre = 0, top = 0;
  93. for(int i = 0;i < n1+n2; i++) {
  94. // 不加epsA不了~
  95. int cur = (int)(x1[i].a + eps);
  96. ans[++top] = (cur + pre)%10;
  97. pre = (pre + cur)/10;
  98. }
  99. while(!ans[top] && top > 1) top--;
  100. for(int i = top;i >= 1; i--)
  101. printf("%d", ans[i]);
  102. puts("");
  103. }
  104.  
  105. int main() {
  106. while(scanf("%s%s", sa, &sb) != -1) {
  107. solve();
  108. }
  109. return 0;
  110. }

HDU 1402 A * B Problem Plus (FFT模板题)的更多相关文章

  1. HDU - 1402 A * B Problem Plus FFT裸题

    http://acm.hdu.edu.cn/showproblem.php?pid=1402 题意: 求$a*b$ 但是$a$和$b$的范围可以达到 $1e50000$ 题解: 显然...用字符串模拟 ...

  2. hdu 1402 A * B Problem Plus FFT

    /* hdu 1402 A * B Problem Plus FFT 这是我的第二道FFT的题 第一题是完全照着别人的代码敲出来的,也不明白是什么意思 这个代码是在前一题的基础上改的 做完这个题,我才 ...

  3. HDU 1402 A * B Problem Plus (FFT求高精度乘法)

    A * B Problem Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  4. HDU - 1402 A * B Problem Plus (FFT实现高精度乘法)

    题意:计算A*B,A,B均为长度小于50000的整数. 这是FFT在大整数相乘中的一个应用,我本来想用NTT做的,但NTT由于取模很可能取炸,所以base必须设得很小,而且效率也比不上FFT. A和B ...

  5. [hdu1402]A * B Problem Plus(FFT模板题)

    解题关键:快速傅里叶变换fft练习. 关于结果多项式长度的确定,首先将短多项式扩展为长多项式,然后扩展为两倍. #include<cstdio> #include<cstring&g ...

  6. HDU 5832 A water problem(某水题)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  7. hdu 1402 A * B Problem Plus (FFT模板)

    A * B Problem Plus Problem Description Calculate A * B. Input Each line will contain two integers A ...

  8. HDU 1402 A * B Problem Plus(FFT)

    Problem Description Calculate A * B.   Input Each line will contain two integers A and B. Process to ...

  9. FFT(快速傅立叶变换):HDU 1402 A * B Problem Plus

    Calculate A * B. Input Each line will contain two integers A and B. Process to end of file. Note: th ...

随机推荐

  1. wsdl文件结构分析

    WSDL (Web Services Description Language,Web服务描述语言)是一种XML Application,他将Web服务描述定义为一组服务访问点,客户端可以通过这些服务 ...

  2. SURF特征

    了解了SIFT特征后,来学习SURF特征. 虽说是SIFT的一个变种,可是跟SIFT还是有差别的 差别有例如以下: 1.尺度空间的构建(近似)不同. 2.同意尺度空间多层图像同一时候被处理 3.特征点 ...

  3. 自定义ImageView实现图片手势滑动,多点触摸放大缩小效果

    首先呢,还是一贯作风,我们先来看看众多应用中的示例:(这种效果是很常见的,可以说应用的必须品.)                搜狐客户端                               ...

  4. javascript封装id|class|元素选择器

    由于各个浏览器都支持的选择方法只有如下三种: 1 document.getElementById() 2 document.getElementsByName() 3 document.getElem ...

  5. Entrez检索实例 - NCBI

    题目:已知来豆荚斑驳病毒(bean pod mottle virus,BPMV)的名字,查询BPMV基因组信息.核酸序列信息.蛋白序列信息和结构信息 解答: 1.直接搜索,点genome,即可看到病毒 ...

  6. 内容提供者 ContentResolver 数据库 示例 -2

    MainActivity public class MainActivity extends ListActivity {     // 访问内容提供者时需要的主机名称     public stat ...

  7. JavaScript原型,原型链 !

    js原型 问题:什么是js原型? js每声明一个function,都有prototype原型,prototype原型是函数的一个默认属性,在函数的创建过程中由js编译器自动添加. 也就是说:当生产一个 ...

  8. 10个最实用的Linux命令

    收集了一些对于Linux新手最基本但最有用的Linux命令.你完全可以键入这些命令来管理你的服务器.这些命令对于学习vps或服务器管理的新手最为简便.1.List命令 ls -a //列出所有文件 l ...

  9. C#进程与线程

    public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { / ...

  10. php 之 PDO数据访问抽象层(0513)

    PDO(PHP Data Objects)是一种在PHP里连接数据库的使用接口. PDO与mysqli曾经被建议用来取代原本PHP在用的mysql相关函数, 基于数据库使用的安全性,因为后者欠缺对于S ...