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. JPA一对多循环引用的解决&&JackSon无限递归问题

    说是解决,其实不是很完美的解决的,写出来只是想记录一下这个问题或者看一下有没有哪位仁兄会的,能否知道一二. 下面说说出现问题: 问题是这样的,当我查询一个一对多的实体的时候,工具直接就爆了,差不多我就 ...

  2. python基础十四之匿名函数

    匿名函数 处理简单问题的简化函数,关键字lambda. # 格式:函数名 = lambda 参数:返回值 anonymity = lambda s: s ** 0.5 print(anonymity( ...

  3. thinter图形开发界面

    tkinter编程步骤 导入Tkinter 创建控件 import thinter 创建主窗口 #win = tkinter.Tk() 设置标题 win.title("xiaoxin&quo ...

  4. Linux 内核端点

    USB 通讯的最基本形式是通过某些称为 端点 的. 一个 USB 端点只能在一个方向承载数 据, 或者从主机到设备(称为输出端点)或者从设备到主机(称为输入端点). 端点可看作一 个单向的管道. 一个 ...

  5. 记一次奇葩事——html5可能不支持window.onscroll函数

    只在html5里遇到,html4没事:拿出来聊聊,路过帮忙解答下!!! 不正常的 <!doctype html><html><head><meta chars ...

  6. 【37.48%】【hdu 2587】How far away ?(3篇文章,3种做法,LCA之ST算法(RMQ))

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...

  7. CodeForces 375D Tree and Queries

    传送门:https://codeforces.com/problemset/problem/375/D 题意: 给你一颗有根树,树上每个节点都有其对应的颜色,有m次询问,每次问你以点v为父节点的子树内 ...

  8. Team Foundation Server 2015使用教程【6】:新增权限为读取器的团队

  9. 第二阶段:流程图:8.axure绘制简单业务流程图

    元件库选择流程图 流程图示例 一个简单的注册练习 线上可以写字 作业

  10. Vue+Element实现网页版个人简历系统

    这篇文章介绍一个使用Vue+Element实现的个人简历系统,主要用到的技术有:vue.element.css3.css定位. 作者在window10进行开发,目前只在chrome上进行过测试,没有大 ...