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. C++ 实现一个信号量

    C++ 实现一个信号量 信号量有很多应用场景,事实上只要是生产者-消费者模型,一般都需要一个信号量来控制. POSIX接口是有PV信号量API的.但C++标准没有.下面是一个PV信号量的简单实现.有些 ...

  2. Python内置函数(34)——map

    英文文档: map(function, iterable, ...) Return an iterator that applies function to every item of iterabl ...

  3. Spring Security 入门(1-4-1)Spring Security - 认证过程

    理解时可结合一下这位老兄的文章:http://www.importnew.com/20612.html 1.Spring Security的认证过程 1.1.登录过程 - 如果用户直接访问登录页面 用 ...

  4. OAuth2.0学习(1-3)OAuth2.0的参与者和流程

    OAuth(开放授权)是一个开放标准.允许第三方网站在用户授权的前提下访问在用户在服务商那里存储的各种信息.而这种授权无需将用户提供用户名和密码提供给该第三方网站. OAuth允许用户提供一个令牌给第 ...

  5. SQL Server数据库优化的10多种方法

    巧妙优化sql server数据库的几种方法,在实际操作中导致查询速度慢的原因有很多,其中最为常见有以下的几种:没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷). I/O吞吐量小, ...

  6. C++中const对象和非const对象调用成员函数问题

    一.类MyClass 二.主函数调用 三.结果

  7. O(logN)中logN的底数

    转载:http://blog.csdn.net/jdbc/article/details/42173751 问题: 无论是计算机算法概论.还是数据结构书中, 关于算法的时间复杂度很多都用包含O(log ...

  8. fetch简明学习

    前面的话 Fetch API 提供了一个 JavaScript接口,用于访问和操纵HTTP管道的部分,例如请求和响应.它还提供了一个全局 fetch()方法,该方法提供了一种简单,合乎逻辑的方式来跨网 ...

  9. python开发:python基本数据类型

    运算符 1.算数运算: 2.比较运算: 3.赋值运算: 4.逻辑运算: 5.成员运算: 基本数据类型 1.数字 int(整型) 在32位机器上,整数的位数为32位,取值范围为-2**31-2**31- ...

  10. WebApi 基于token的多平台身份认证架构设计

    1   概述 在存在账号体系的信息系统中,对身份的鉴定是非常重要的事情. 随着移动互联网时代到来,客户端的类型越来越多, 逐渐出现了 一个服务器,N个客户端的格局 . 不同的客户端产生了不同的用户使用 ...