A * B Problem Plus

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

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
 
Author
DOOM III
 
Recommend

题意:求高精度a*b                                  --代码参考kuangbin大神

思路:

通过FFT我们可以快速求出多项式的卷积,从而解决数相乘。

求卷积大致如下图,至于FFT具体原理看不太懂- -

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
typedef long double ld;
const ld eps=1e-10;
const int inf = 0x3f3f3f;
const int MOD = 1e9+7; const double PI = acos(-1.0); struct Complex
{
double x,y;
Complex(double _x = 0.0,double _y = 0.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);
}
}; void change(Complex y[],int len)
{
int i,j,k;
for(i = 1,j = len/2; i < len-1; i++)
{
if(i < j) swap(y[i],y[j]);
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;
}
} const int maxn = 200100;
Complex x1[maxn],x2[maxn];
char str1[maxn],str2[maxn];
int sum[maxn]; int main()
{
while(scanf("%s%s",str1,str2) != EOF)
{
int len1 = strlen(str1);
int len2 = strlen(str2);
int len = 1;
while(len < len1*2 || len < len2*2) len <<= 1; for(int i = 0; i < len1; i++)
x1[i] = Complex(str1[len1-i-1]-'0',0);
for(int i = len1; i < len; i++)
x1[i] = Complex(0,0); for(int i = 0; i < len2; i++)
x2[i] = Complex(str2[len2-1-i]-'0',0);
for(int i = len2; 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];
//cout << x1[i].x << " "<< x1[i].y <<endl;
}
fft(x1,len,-1);
for(int i = 0;i < len;i++){
sum[i] = (int)(x1[i].x+0.5);
//cout << sum[i] << endl;
} for(int i = 0; i < len; i++)
{
sum[i+1] += sum[i]/10;
sum[i] %= 10;
}
len= len1+len2-1;
while(sum[len] <= 0 && len > 0)
len--;
for(int i = len; i >= 0; i--)
printf("%c",sum[i]+'0');
printf("\n");
}
return 0;
}

  

hdu 1402 FFT(模板)的更多相关文章

  1. HDU 1402 fft 模板题

    题目就是求一个大数的乘法 这里数字的位数有50000的长度,按平时的乘法方式计算,每一位相乘是要n^2的复杂度的,这肯定不行 我们可以将每一位分解后作为系数,如153 = 1*x^2 + 5*x^1 ...

  2. HDU 1402 FFT 大数乘法

    $A * B$ FFT模板题,找到了一个看起来很清爽的模板 /** @Date : 2017-09-19 22:12:08 * @FileName: HDU 1402 FFT 大整数乘法.cpp * ...

  3. HDU 4609 FFT模板

    http://acm.hdu.edu.cn/showproblem.php?pid=4609 题意:给你n个数,问任意取三边能够,构成三角形的概率为多少. 思路:使用FFT对所有长度的个数进行卷积(\ ...

  4. fft模板 HDU 1402

    // fft模板 HDU 1402 #include <iostream> #include <cstdio> #include <cstdlib> #includ ...

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

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

  6. hdu 1402 A * B Problem Plus FFT

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

  7. A * B Problem Plus HDU - 1402 (FFT)

    A * B Problem Plus HDU - 1402 (FFT) Calculate A * B.  InputEach line will contain two integers A and ...

  8. HDU 1402

    http://acm.hdu.edu.cn/showproblem.php?pid=1402 fft做O(nlog(n))大数乘法,kuangbin的模板 #include <stdio.h&g ...

  9. hdu1402(大数a*b&fft模板)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1402 题意: 给出两个长度1e5以内的大数a, b, 输出 a * b. 思路: fft模板 详情参 ...

随机推荐

  1. 关于mule中Spring使用中的一个问题

    在mule中连接数据库时,大家通常喜欢使用spring的数据库连接以及bean的配置,但是在使用时会出现一些问题,即bean无法找到,这些,就是需要把bean的id属性改成name属性:可能是因为mu ...

  2. vue 的模板编译—ast(抽象语法树) 详解与实现

    首先AST是什么? 在计算机科学中,抽象语法树(abstract syntax tree或者缩写为AST),或者语法树(syntax tree),是源代码的抽象语法结构的树状表现形式,这里特指编程语言 ...

  3. 0基础菜鸟学前端之Vue.js

    简介:0基础前端菜鸟,啃了将近半月前端VUE框架,对前端知识有了初步的了解.下面总结一下这段时间的学习心得. 文章结构 前端基础 Vue.js简介 Vue.js常用指令 Vue.js组件 Vue.js ...

  4. 算法题丨3Sum Closest

    描述 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

  5. 自动化服务部署(一):Linux下安装JDK

    自动化测试的主要目的是为了执行回归测试.当然,为了模拟真实的用户操作,一般都是在UAT或者生产环境进行回归测试. 为了尽量避免内网和外网解析对测试结果的影响,将自动化测试服务部署在外网的服务器是比较好 ...

  6. CentOS 6.5 Tomcat安装及配置

    1.安装jdk,配置jdk环境(此步骤略过) 2.下载安装tomcat 百度网盘链接: https://pan.baidu.com/s/1Ieejo7TQyzRAVPhQft8Phw 密码: dg2v ...

  7. centos7配置Apache支持HTTPS

    Apache版本2.4 安装mod_ssl yum install mod_ssl 建立文件夹,存放sslkey mkdir /etc/httpd/ssl/ 建立凭证档 openssl req -x5 ...

  8. c# IPC实现本机进程之间的通信

    IPC可以实现本地进程之间通信.这种用法不是太常见,常见的替代方案是使用wcf,remoting,web service,socket(tcp/pipe/...)等其他分布式部署方案来替代进程之间的通 ...

  9. drupal 8 建立我的相册

    一.先建一个存图片的内容类型 1.创建内容类型 点击[结构]>[内容类型]>[+add content type] 点击[保存和管理字段] 2.添加字段 点击[+添加字段] 点击[保存并继 ...

  10. [LeetCode] Longest Word in Dictionary 字典中的最长单词

    Given a list of strings words representing an English Dictionary, find the longest word in words tha ...