题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1028

题目大意就是求两个大数的乘法。

但是用普通的大数乘法,这个长度的大数肯定不行。

大数可以表示点值表示法,然后卷积乘法就能用FFT加速运算了。

这道题是来存模板的。

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <cstring>
  6. #include <algorithm>
  7. #include <set>
  8. #include <map>
  9. #include <queue>
  10. #include <string>
  11. #define LL long long
  12.  
  13. using namespace std;
  14.  
  15. //多项式乘法运算
  16. //快速傅里叶变换
  17. //FFT
  18. //用于求两个多项式的卷积,但有精度损失
  19. //时间复杂度nlogn
  20. const int maxN = ;
  21. const double PI = acos(-1.0);
  22.  
  23. struct Complex
  24. {
  25. double r, i;
  26.  
  27. Complex(double rr = 0.0, double ii = 0.0)
  28. {
  29. r = rr;
  30. i = ii;
  31. }
  32.  
  33. Complex operator+(const Complex &x)
  34. {
  35. return Complex(r+x.r, i+x.i);
  36. }
  37.  
  38. Complex operator-(const Complex &x)
  39. {
  40. return Complex(r-x.r, i-x.i);
  41. }
  42.  
  43. Complex operator*(const Complex &x)
  44. {
  45. return Complex(r*x.r-i*x.i, i*x.r+r*x.i);
  46. }
  47. };
  48.  
  49. //雷德算法--倒位序
  50. //Rader算法
  51. //进行FFT和IFFT前的反转变换。
  52. //位置i和 (i二进制反转后位置)互换
  53. void Rader(Complex y[], int len)
  54. {
  55. int j = len>>;
  56. for(int i = ; i < len-; i++)
  57. {
  58. if (i < j) swap(y[i], y[j]);
  59. int k = len >> ;
  60. while (j >= k)
  61. {
  62. j -= k;
  63. k >>= ;
  64. }
  65. if (j < k) j += k;
  66. }
  67. }
  68.  
  69. //FFT实现
  70. //len必须为2^k形式,
  71. //on==1时是DFT,on==-1时是IDFT
  72. void FFT(Complex y[], int len, int on)
  73. {
  74. Rader(y, len);
  75. for (int h = ; h <= len; h<<=)//分治后计算长度为h的DFT
  76. {
  77. Complex wn(cos(-on**PI/h), sin(-on**PI/h)); //单位复根e^(2*PI/m)用欧拉公式展开
  78. for (int j = ; j < len; j += h)
  79. {
  80. Complex w(, );//旋转因子
  81. for (int k = j; k < j+h/; k++)
  82. {
  83. Complex u = y[k];
  84. Complex t = w*y[k+h/];
  85. y[k] = u+t;//蝴蝶合并操作
  86. y[k+h/] = u-t;
  87. w = w*wn;//更新旋转因子
  88. }
  89. }
  90. }
  91. if (on == -)
  92. for (int i = ; i < len; i++)
  93. y[i].r /= len;
  94. }
  95.  
  96. //求卷积
  97. void Conv(Complex a[], Complex b[], int ans[], int len)
  98. {
  99. FFT(a, len, );
  100. FFT(b, len, );
  101. for (int i = ; i < len; i++)
  102. a[i] = a[i]*b[i];
  103. FFT(a, len, -);
  104. //精度复原
  105. for(int i = ; i < len; i++)
  106. ans[i] = a[i].r+0.5;
  107. }
  108.  
  109. //进制恢复
  110. //用于大数乘法
  111. void turn(int ans[], int len, int unit)
  112. {
  113. for(int i = ; i < len; i++)
  114. {
  115. ans[i+] += ans[i]/unit;
  116. ans[i] %= unit;
  117. }
  118. }
  119.  
  120. char str1[maxN], str2[maxN];
  121. Complex za[maxN],zb[maxN];
  122. int ans[maxN];
  123. int len;
  124.  
  125. void init(char str1[], char str2[])
  126. {
  127. int len1 = strlen(str1);
  128. int len2 = strlen(str2);
  129. len = ;
  130. while (len < *len1 || len < *len2) len <<= ;
  131.  
  132. int i;
  133. for (i = ; i < len1; i++)
  134. {
  135. za[i].r = str1[len1-i-]-'';
  136. za[i].i = 0.0;
  137. }
  138. while (i < len)
  139. {
  140. za[i].r = za[i].i = 0.0;
  141. i++;
  142. }
  143. for (i = ; i < len2; i++)
  144. {
  145. zb[i].r = str2[len2-i-]-'';
  146. zb[i].i = 0.0;
  147. }
  148. while (i < len)
  149. {
  150. zb[i].r = zb[i].i = 0.0;
  151. i++;
  152. }
  153. }
  154.  
  155. void work()
  156. {
  157. Conv(za, zb, ans, len);
  158. turn(ans, len, );
  159. while (ans[len-] == ) len--;
  160. for (int i = len-; i >= ; i--)
  161. printf("%d", ans[i]);
  162. printf("\n");
  163. }
  164.  
  165. int main()
  166. {
  167. //freopen("test.in", "r", stdin);
  168. while (scanf("%s%s", str1, str2) != EOF)
  169. {
  170. init(str1, str2);
  171. work();
  172. }
  173. return ;
  174. }

ACM学习历程—51NOD1028 大数乘法V2(FFT)的更多相关文章

  1. 1028 大数乘法 V2(FFT or py)

    1028 大数乘法 V2 基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 给出2个大整数A,B,计算A*B的结果.   Input 第1行:大数A 第2行:大数B ...

  2. 51 Nod 1028 大数乘法 V2【Java大数乱搞】

    1028 大数乘法 V2 基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 给出2个大整数A,B,计算A*B的结果. Input 第1行:大数A 第2行:大数B (A ...

  3. ACM学习历程—HDU5667 Sequence(数论 && 矩阵乘法 && 快速幂)

    http://acm.hdu.edu.cn/showproblem.php?pid=5667 这题的关键是处理指数,因为最后结果是a^t这种的,主要是如何计算t. 发现t是一个递推式,t(n) = c ...

  4. ACM学习历程—HDU5585 Numbers(数论 || 大数)(BestCoder Round #64 (div.2) 1001)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5585 题目大意就是求大数是否能被2,3,5整除. 我直接上了Java大数,不过可以对末尾来判断2和5, ...

  5. ACM学习历程—HDU1041 Computer Transformation(递推 && 大数)

    Description A sequence consisting of one digit, the number 1 is initially written into a computer. A ...

  6. ACM学习历程—HDU1023 Train Problem II(递推 && 大数)

    Description As we all know the Train Problem I, the boss of the Ignatius Train Station want to know  ...

  7. ACM学习历程—HDU 3092 Least common multiple(数论 && 动态规划 && 大数)

    Description Partychen like to do mathematical problems. One day, when he was doing on a least common ...

  8. ACM学习历程—NPU1045 2015年陕西省程序设计竞赛网络预赛(热身赛)C题 Graph Theory(递推 && 组合数学 && 大数)

    Description In graph theory, a matching or independent edge set in a graph G = (V , E) is a set of e ...

  9. FFT/NTT [51Nod 1028] 大数乘法 V2

    题目链接:51Nod 传送门 没压位,效率会低一点 1.FFT #include <cstdio> #include <cstring> #include <algori ...

随机推荐

  1. SpringMVC基于代码的配置方式(零配置,无web.xml)

    基于配置文件的web项目维护起来可能会更方便,可是有时候我们会有一些特殊的需求,比方防止客户胡乱更改配置,这时候我们须要给配置隐藏到代码中. 1.创建一个动态web项目(无需web.xml) 2.右键 ...

  2. phpcms的基础知识和配置

    一.设置界面 1.站点设置:相当于服务器上的站点 (1)站点修改:“关键词”和“描述”的修改,便于网络优化和搜索引擎对本网站的搜索. (2)点击站点后边的修改,模板的修改,引用自己模板 2.基本设置: ...

  3. poj1135

    Domino Effect Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10454   Accepted: 2590 De ...

  4. Gone Fishing(贪心)

    Gone Fishing John is going on a fising trip. He has h hours available (1 ≤ h ≤ 16), and there are n ...

  5. NSURLSession各文件关系

    NSURLSession   通过session创建任务 @property (class, readonly, strong) NSURLSession *sharedSession; + (NSU ...

  6. SVM vs. Softmax

    http://cs231n.github.io/linear-classify/

  7. memcached 不同客户端的问题

    摘要: memcached-java客户端调用get方法获取数据失败 主要演示一下在memcached服务器端set数据之后,在客户端调用java api获取数据.不过此过程如果不慎会读取数据失败. ...

  8. pinpoint改造支持查询

    原架构 改造后架构

  9. jquery 如何获取单选框的值

    jquery 如何获取单选框的值   获取单选框的值有三种方式: 1.$('input:radio:checked').val():2.$("input[type='radio']:chec ...

  10. Python 3 mysql 数据类型

    Python 3 mysql 数据类型 存储引擎决定了表的类型,而表内存放的数据也要有不同的类型,每种数据类型都有自己的宽度,但宽度是可选的 详细参考: http://www.runoob.com/m ...