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. centos7的gnome假死

    centos7的gnome假死,干掉gnome相关进程,如nautilus,kworker

  2. webpack学习(四)配置plugins

    1 plugins是什么??? 如果学过vue和react肯定知道生命周期函数,而生命周期函数实际上就是当程序运行在某个时刻一定会发生的函数. plugins其实也是如此,我们在项目中配置相应的plu ...

  3. spring boot + thymeleaf 乱码问题

    spring boot + thymeleaf 乱码问题 hellotrms 发布于 2017/01/17 15:27 阅读 1K+ 收藏 0 答案 1 开发四年只会写业务代码,分布式高并发都不会还做 ...

  4. 【u105】路径计数2

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 一个N×N的网格,你一开始在(1, 1),即左上角.每次只能移动到下方相邻的格子或者右方相邻的格子,问 ...

  5. linux一个例子驱动

    我们介绍的驱动称为 short (Simple Hardware Operations and Raw Tests). 所有它做 的是读和写几个 8-位 端口, 从你在加载时选择的开始. 缺省地, 它 ...

  6. Oracle 和pl/sql以及pl/sql developer

    oracle是厂家的名字,也是数据库产品的名字.比如sybase公司的sybase数据库.而微软公司的数据库产品就叫sqlserver了. pl/sql 是oracle数据库所用的sql语言的名称.微 ...

  7. LuoguP1402 酒店之王

    LuoguP1402 酒店之王 最大流题目.带有一定的思维技(tao)巧(lu) 依旧分析题目.如果只有房间或者菜一种限制.那么就是一道裸的最大流了 可是两种条件都应当满足, 这貌似也可以做. 因为每 ...

  8. easypermissions拒绝权限后闪退。 java.lang.NoSuchMethodError: No virtual method isStateSaved()Z in class Landroid/support/v4/app/FragmentManager

    Process: com.tazan.cd.streetlight, PID: 18825 java.lang.NoSuchMethodError: No virtual method isState ...

  9. 西游记之孙悟空三打白骨精(IMAX)

    短评:看了20分钟就有玩手机的冲动.剧情还差点意思,不能达到吸引人目不转睛的程度

  10. PyTorch深度学习:60分钟入门(Translation)

    这是https://zhuanlan.zhihu.com/p/25572330的学习笔记. Tensors Tensors和numpy中的ndarrays较为相似, 因此Tensor也能够使用GPU来 ...