HDU 1402

A * B Problem Plus

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 26545    Accepted Submission(s): 6964

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
 
思路分析 : 两个 5e4 的多项式相乘,显然最朴素的 n^2 的高精度乘法是过不去的 , 这里可以用 fft , 将两个数看成是多项式即可
    有几个坑点 , 0 * 654651 = 0 , 要把首位前面的 0 全去掉
代码示例 :

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 1e5+5;
const int maxm = 5e5+5;
char s1[maxn], s2[maxn];
const double pi = acos(-1.0); struct Complex{
double x, y;
Complex (double _x=0, double _y=0):x(_x), y(_y){}
Complex operator -(const Complex &b)const{
return Complex(x-b.x, y-b.y);
}
Complex operator +(const Complex &b)const{
return Complex(x+b.x, y+b.y);
}
Complex operator *(const Complex &b)const{
return Complex(x*b.x-y*b.y, x*b.y+y*b.x);
}
};
Complex x1[maxm], x2[maxm];
void change(Complex y[], int len){
for(int i = 1, j = len/2; i < len-1; i++){
if (i < j) swap(y[i], y[j]);
int k = len/2;
while(j >= k){
j -= k;
k /= 2;
}
if (j < k) j += k;
}
} void fft(Complex y[], int len, int on){
change(y, len);
for(int h = 2; h <= len; h <<= 1){
Complex wn(cos(-on*2*pi/h), sin(-on*2*pi/h));
for(int j = 0; j < len; j += h){
Complex w(1, 0);
for(int k = j; k < j+h/2; k++){
Complex u = y[k];
Complex t = w*y[k+h/2];
y[k] = u+t;
y[k+h/2] = u-t;
w = w*wn;
}
}
}
if (on == -1){
for(int i = 0; i < len; i++)
y[i].x /= len;
}
}
vector<int>ans;
int sum[maxm]; int main () { while(~scanf("%s%s", s1, s2)){
int l1 = strlen(s1);
int l2 = strlen(s2);
memset(x1, 0, sizeof(x1));
memset(x2, 0, sizeof(x2));
int len = 1;
while(len < (l1+l2-1)) len <<= 1; for(int i = 0; i < l1; i++) x1[i] = Complex((double)(s1[i]-'0'), 0);
for(int i = l1; i < len; i++) x1[i] = Complex(0, 0);
for(int i = 0; i < l2; i++) x2[i] = Complex((double)(s2[i]-'0'), 0);
for(int i = l2; i < len; i++) x2[i] = Complex(0, 0);
fft(x1, len, 1); fft(x2, len, 1);
for(int i = 0; i < len; i++) x1[i] = x1[i]*x2[i];
fft(x1, len, -1); ans.clear();
ans.resize(l1+l2-1);
for(int i = l1+l2-2; i >= 0; i--){
ans[i] += (int)(x1[i].x+0.5);
if (i != 0) {
ans[i-1] += ans[i]/10;
ans[i] %= 10;
}
else {
int temp = ans[0]/10;
if(temp != 0){
ans[0] %= 10;
ans.insert(ans.begin(), temp);
}
}
}
int pos = 0;
for(int i = 0; i < l1+l2-1; i++) {
pos = i;
if (ans[i] != 0) break;
}
for(int i = pos; i < ans.size(); i++){
printf("%d", ans[i]);
}
printf("\n");
memset(s1, '\0', sizeof(s1));
memset(s2, '\0', sizeof(s2));
} return 0;
}
/*
4121 46
1321 46
*/

两个大数相乘 - 高精度FFT的更多相关文章

  1. 两个大数相乘-Java

    两个字符串表示两个非常大的数,请设计算法计算这两个大数的乘积,结果用字符串表示.例如S1="7832974972840919321747983209327",S2="19 ...

  2. Go--实现两个大数相乘

    ----- import ( "bufio" "fmt" "os" "strings" ) func multi(str ...

  3. Karatsuba乘法--实现大数相乘

    Karatsuba乘法 Karatsuba乘法是一种快速乘法.此算法在1960年由Anatolii Alexeevitch Karatsuba 提出,并于1962年得以发表.此算法主要用于两个大数相乘 ...

  4. 1051:A × B problem 大数相乘

    给你两个整数,请你计算A × B. 输入 数据的第一行是整数T(1 ≤ T ≤ 20),代表测试数据的组数.接着有T组数据,每组数据只有一行,包括两个非负整数A和B.但A和B非常大,Redraimen ...

  5. 高精度&&FFT

    ACM-高精度模板(综合篇) 时间:-- :: 阅读: 评论: 收藏: [点我收藏+] 标签:高精度 在这里,我们约定,能用int表示的数据视为单精度,否则为高精度.所有函数的设计均采用带返回值的形式 ...

  6. 大数相乘算法C++版

    #include <iostream> #include <cstring> using namespace std; #define null 0 #define MAXN ...

  7. java版大数相乘

    在搞ACM的时候遇到大数相乘的问题,在网上找了一下,看到了一个c++版本的 http://blog.csdn.net/jianzhibeihang/article/details/4948267 用j ...

  8. linux c 实现大数相乘

      #include <stdio.h> #include <string.h> #include <math.h> #include <stdbool.h& ...

  9. Linux C/C++ 编程练手 --- 大数相加和大数相乘

    最近写了一个大数相乘和相加的程序,结果看起来是对的.不过期间的效率可能不是最好的,有些地方也是临时为了解决问题而直接写出来的. 可以大概说一下相乘和相加的解决思路(当然,大数操作基本就是两个字符串的操 ...

随机推荐

  1. dotnet core 2.1 使用阶梯编译

    在 dotnet core 2.1 可以使用阶梯编译的方法,从 dotnet framework 开始,在代码的所有方法在第一次进入的时候就需要使用 JIT 进行编译为本机的代码.可以看到代码是在第一 ...

  2. python进阶之异常处理

    异常处理 在代码运行时,会因为各种原因出现bug,而程序遇到bug就会中断运行,而在日常生产中程序是要长时间运行不能随意中断的.因此就需要我们提前做好异常处理. 异常 print(x) # 一般报错就 ...

  3. turtle 20秒画完小猪佩奇“社会人”

    转载:https://blog.csdn.net/csdnsevenn/article/details/80650456 图片源自网络 作者 丁彦军 如需转载,请联系原作者授权. 今年社交平台上最火的 ...

  4. HDU 1698 Just a Hook (线段树模板题-区间求和)

    Just a Hook In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of t ...

  5. 改善Azure App Service托管应用程序性能的几个技巧

    本文介绍了几个技巧,这些技巧可以改善Azure App Service托管应用程序的性能.其中一些技巧是你现在就可以进行的配置变更, 而其他技巧则可能需要对应用程序进行一些重新设计和重构. 开发者都希 ...

  6. H3C 端口绑定基本配置

  7. cmd 如何跨驱动器移动文件夹

    如果在命令行或 cmd 批处理文件通过 move 移动文件夹的时候,移动的文件夹是跨驱动器的,那么将会显示拒绝访问 解决通过 move 移动文件夹到不同的驱动器需要通过先复制文件夹到另一个驱动器,然后 ...

  8. Eclipse GlassFish Server 配置

    一.下载GlassFish Server    通过如下地址下载合适版本:    http://glassfish.java.net/public/downloadsindex.html    htt ...

  9. Java 学习笔记(10)——容器

    之前学习了java中从语法到常用类的部分.在编程中有这样一类需求,就是要保存批量的相同数据类型.针对这种需求一般都是使用容器来存储.之前说过Java中的数组,但是数组不能改变长度.Java中提供了另一 ...

  10. python 练习题3

    # 计算阶乘def factorial(n): if n>2: return n*factorial(n-1) else: return nprint(factorial(2))print(fa ...