A * B Problem Plus

Problem Description
Calculate A * B.
Input
Each line will contain two integers A and B. Process to end of file.
Note: the length of each integer will not exceed 50000.
Output
For each case, output A * B in one line.
Sample Input
1
2
1000
2
Sample Output
2 2000

FFT的模板题。我们把数字的每一位看成i*(x^j),当我们把x取成10的时候我们就得到了这个大整数

  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. const int maxn = +;
  5. const double pi = acos(-1.0);
  6. const double PI = acos(-1.0);
  7. #define fft FFT
  8. #define r real
  9. struct Complex
  10. {
  11. double r,i;
  12. Complex(double _r,double _i):r(_r),i(_i){}
  13. Complex(){}
  14. Complex operator +(const Complex &b)
  15. {
  16. return Complex(r+b.r,i+b.i);
  17. }
  18. Complex operator -(const Complex &b)
  19. {
  20. return Complex(r-b.r,i-b.i);
  21. }
  22. Complex operator *(const Complex &b)
  23. {
  24. return Complex(r*b.r-i*b.i,r*b.i+i*b.r);
  25. }
  26. };
  27. void change(Complex y[],int len)
  28. {
  29. int i,j,k;
  30. for(i = , j = len/;i < len-;i++)
  31. {
  32. if(i < j)swap(y[i],y[j]);
  33. k = len/;
  34. while( j >= k)
  35. {
  36. j -= k;
  37. k /= ;
  38. }
  39. if(j < k)j += k;
  40. }
  41. }
  42. void fft(Complex y[],int len,int on)
  43. {
  44. change(y,len);
  45. for(int h = ;h <= len;h <<= )
  46. {
  47. Complex wn(cos(-on**pi/h),sin(-on**pi/h));
  48. for(int j = ;j < len;j += h)
  49. {
  50. Complex w(,);
  51. for(int k = j;k < j+h/;k++)
  52. {
  53. Complex u = y[k];
  54. Complex t = w*y[k+h/];
  55. y[k] = u+t;
  56. y[k+h/] = u-t;
  57. w = w*wn;
  58. }
  59. }
  60. }
  61. if(on == -)
  62. for(int i = ;i < len;i++)
  63. y[i].r /= len;
  64. }
  65. char numA[maxn],numB[maxn];
  66. Complex a[maxn*],b[maxn*];
  67. int ans[maxn*];
  68. int main()
  69. {
  70. //freopen("de.txt","r",stdin);
  71. while (~scanf("%s",numA)){
  72. int lenA = strlen(numA);
  73. int sa = ;
  74. while ((<<sa)<lenA) sa++;
  75. scanf("%s",numB);
  76. int lenB = strlen(numB);
  77. int sb = ;
  78. while ((<<sb)<lenB) sb++;
  79. int len = (<<(max(sa,sb)+));
  80. for (int i=;i<len;++i){
  81. if (i<lenA) a[i] = Complex(numA[lenA-i-]-'',);
  82. else a[i] = Complex(,);
  83. if (i<lenB) b[i] = Complex(numB[lenB-i-]-'',);
  84. else b[i] = Complex(,);
  85. }
  86. fft(a,len,);
  87. fft(b,len,);//将a b 换成点值表达
  88. for (int i=;i<len;++i)
  89. a[i] = a[i]*b[i];//点值相乘
  90. fft(a,len,-);//DFT逆变换回去
  91. for (int i=;i<len;++i)
  92. ans[i] = (int)(a[i].r+0.5);//误差处理
  93. for (int i=;i<len-;++i){
  94. ans[i+]+=ans[i]/;//处理进位问题
  95. ans[i]%=;
  96. }
  97. bool flag = ;//调整输出格式处理前导零问题
  98. for (int i=len-;i>=;--i){
  99. if (ans[i]) printf("%d",ans[i]),flag=;
  100. else if (flag||i==) printf("");
  101. }
  102. printf("\n");
  103. }
  104. return ;
  105. }

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

  1. HDU 1402 A * B Problem Plus (FFT模板题)

    FFT模板题,求A*B. 用次FFT模板需要注意的是,N应为2的幂次,不然二进制平摊反转置换会出现死循环. 取出结果值时注意精度,要加上eps才能A. #include <cstdio> ...

  2. hdu 1402 A * B Problem Plus FFT

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

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

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

  4. 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 ...

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

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

  6. P1919 【模板】A*B Problem升级版 /// FFT模板

    题目大意: 给定l,输入两个位数为l的数A B 输出两者的乘积 FFT讲解 这个讲解蛮好的 就是讲解里面贴的模板是错误的 struct cpx { double x,y; cpx(double _x= ...

  7. HDU 1402 A * B Problem Plus 快速傅里叶变换 FFT 多项式

    http://acm.hdu.edu.cn/showproblem.php?pid=1402 快速傅里叶变换优化的高精度乘法. https://blog.csdn.net/ggn_2015/artic ...

  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. git 切换分支开发并合并提交到远程仓库

  2. SpringMVC的 几个注解

    1.@RequestMapping: 是一个用来处理请求地址映射的注解,可用于类或方法上. 1):用在类上:是父路径. 2):用在方法上:是子路径. @Controller //设置想要跳转的父路径 ...

  3. R中unlist函数的使用

    买的书里面实例讲的不清不楚,所以看帮助文档了 用法:unlist(x, recursive = TRUE, use.names = TRUE) 帮助文档讲x可以是向量或者列表,如果是向量,则原样返回, ...

  4. [HDU3117]Fibonacci Numbers

    题目:Fibonacci Numbers 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3117 分析: 1)后四位可以用矩阵快速幂解决.$T= \left ...

  5. php 统计每天价格,货币种类,汇总得算法和数据处理 (后端和前段实现自动统计价格和币种类型)

    整体实现逻辑介绍 1.对查询数据做一个总体查询,需要根据查询自己主要业务逻辑数据. 2.对总体查询数据和时间和币种类型做三部分数据处理. 3.总体查询数据按照币种和日期组装二维数据对应得键值是价格. ...

  6. JSP 获取访问者真正的IP地址

    request.getRemoteAddr(),这种方法在大部分情况下都是有效的,但是在通过了Apache,Squid等反向代理软件就不能获取到客户端的真实IP地址了, 如果使用了反向代理软件,用re ...

  7. nginx中如何设置gzip(总结)

    nginx中如何设置gzip(总结) 一.总结 一句话总结: 真正用的时候,花一小点时间把gzip的各个字段的意思都看一下,会节约大量时间 直接gzip on:在nginx的配置中就可以开启gzip压 ...

  8. 查看IOS-app证书到期时间

    参照: iOS企业版证书到期 https://www.jianshu.com/p/44b0dc46ef37 如果不能十分确定每一个打出来的ipa的有效期(过期时间),而又需要关注它具体什么时候需要强制 ...

  9. 建议66,67 注意Arrays.asList()的使用

    代码 public static void main(String[] args) { int[]data = {1,2,3,4,5}; List list = Arrays.asList(data) ...

  10. 配送单MYSQL ,一点都不机智

    这是配送单制作,后面修改了下表 . 写的太乱. 不过也不想去修改了. 放在这里了.反正还能用. 不然就坑爹了. 以后写好一点. 这都是些神马, 太难受了. /* 配送单制作,缺少商品规格,以及库存查询 ...