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.
 
题目大意:求A * B。
思路:快速傅里叶变换的模板题,偷的模板……也不知道使用姿势对不对QAQ。
 

代码(250MS):(Update:2014年11月16日)

 #include <cmath>
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <cstring>
#include <complex>
using namespace std;
typedef complex<double> Complex;
const double PI = acos(-); void fft_prepare(int maxn, Complex *&e) {
e = new Complex[ * maxn - ];
e += maxn - ;
e[] = ;
for (int i = ; i < maxn; i <<= )
e[i] = Complex(cos( * PI * i / maxn), sin( * PI * i / maxn));
for (int i = ; i < maxn; i++)
if ((i & -i) != i) e[i] = e[i - (i & -i)] * e[i & -i];
for (int i = ; i < maxn; i++) e[-i] = e[maxn - i];
}
/* f = 1: dft; f = -1: idft */
void dft(Complex *a, int N, int f, Complex *e, int maxn) {
int d = maxn / N * f;
Complex x;
for (int n = N, m; m = n / , m >= ; n = m, d *= )
for (int i = ; i < m; i++)
for (int j = i; j < N; j += n)
x = a[j] - a[j + m], a[j] += a[j + m], a[j + m] = x * e[d * i];
for (int i = , j = ; j < N - ; j++) {
for (int k = N / ; k > (i ^= k); k /= );
if (j < i) swap(a[i], a[j]);
}
} const int MAXN = ;
Complex x1[MAXN], x2[MAXN];
char s1[MAXN / ], s2[MAXN / ];
int sum[MAXN]; int main() {
Complex* e = ;
fft_prepare(MAXN, e);
while(scanf("%s%s",s1,s2) != EOF) {
int n1 = strlen(s1);
int n2 = strlen(s2);
int n = ;
while(n < n1 * || n < n2 * ) n <<= ;
for(int i = ; i < n; ++i) {
x1[i] = i < n1 ? s1[n1 - - i] - '' : ;
x2[i] = i < n2 ? s2[n2 - - i] - '' : ;
} dft(x1, n, , e, MAXN);
dft(x2, n, , e, MAXN);
for(int i = ; i < n; ++i) x1[i] = x1[i] * x2[i];
dft(x1, n, -, e, MAXN);
for(int i = ; i < n; ++i) x1[i] /= n; for(int i = ; i < n; ++i) sum[i] = round(x1[i].real());
for(int i = ; i < n; ++i) {
sum[i + ] += sum[i] / ;
sum[i] %= ;
} n = n1 + n2 - ;
while(sum[n] <= && n > ) --n;
for(int i = n; i >= ;i--) printf("%d", sum[i]);
puts("");
}
}

HDU 1402 A * B Problem Plus(FFT)的更多相关文章

  1. hdu 1402 A * B Problem Plus (FFT模板)

    A * B Problem Plus Problem Description Calculate A * B. Input Each line will contain two integers A ...

  2. 【HDU 1402】A * B Problem Plus(FFT)

    Problem Description Calculate A * B. Input Each line will contain two integers A and B. Process to e ...

  3. HDU 1402 A * B Problem Plus ——(大数乘法,FFT)

    因为刚学fft,想拿这题练练手,结果WA了个爽= =. 总结几点犯的错误: 1.要注意处理前导零的问题. 2.一定要注意数组大小的问题.(前一个fft的题因为没用到b数组,所以b就没管,这里使用了b数 ...

  4. HDU1402 A * B Problem Plus(FFT)

    http://acm.hdu.edu.cn/showproblem.php?pid=1402 初学FFT. http://www.cnblogs.com/WABoss/p/FFT_Note.html ...

  5. 洛谷P1919 【模板】A*B Problem升级版(FFT)

    传送门 话说FFT该不会真的只能用来做这种板子吧…… 我们把两个数字的每一位都看作多项式的系数 然后这就是一个多项式乘法 上FFT就好了 然后去掉前导零 (然而连FFT的板子都背不来orz,而且空间又 ...

  6. Luogu1919 【模板】A*B Problem升级版(FFT)

    简单的\(A*B\) \(Problem\),卡精度卡到想女装 #include <iostream> #include <cstdio> #include <cstri ...

  7. 【Luogu1919】 A*B Problem升级版(FFT)

    题面戳我 题解 把每个数都直接看做一个多项式,每一位就是一项 现在求用FFT求出卷积 然后考虑一下进位就可以啦 #include<iostream> #include<cstdio& ...

  8. hdu 1002 A + B Problem II(大数)

    题意:就是求a+b (a,b都不超过1000位) 思路:用数组存储 第一道大数的题目,虽然很水,纪念一下! 代码: #include<cstdio> #include<cstring ...

  9. 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 ...

随机推荐

  1. charles 使用 技巧

    测试的是Android ,App , 在 手机wifi 网络代理设置为 电脑代理. 然后,手机访问的网络 都通过 电脑端的 charles监控!

  2. Solr4.3之拼写检查Spellcheck功能

    原文地址:http://www.656463.com/article/iaquii.htm 拼写检查功能,能在搜索时提供一个较好用户体验,所以,主流的搜索引擎都有这个功能,在这之前,笔者先简单的说一下 ...

  3. SqlServer中的一些非常用功能

    1.启用双引号作为分隔符 Set Quoted_Identifier on 此时:create table dbo.testcolumn("column" char(2))是合法的 ...

  4. C++控制程序只运行一个实例

    int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { ...

  5. HD 1003 Max Sum 的递归解法

    #include <STDIO.H> typedef struct SU_tag{ SU_tag(){} SU_tag(int a,int b,int c):max_sum(a),left ...

  6. SQLite.net发布后找不到"SQLite.Interop.dll"的问题

    http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki sqlite-netFx40-static-binary-b ...

  7. onmouseenter与onmouseover

    简单的说: mouseenter第一次进入这个元素的某个子元素时触发.一旦触发后,在mouseleave之前,鼠标在这个元素的子元素上触发mouseenter事件,不会触发这个元素的mouseente ...

  8. Linux进程间通信与线程间同步详解(全面详细)

    引用:http://community.csdn.net/Expert/TopicView3.asp?id=4374496linux下进程间通信的几种主要手段简介: 1. 管道(Pipe)及有名管道( ...

  9. android常用命令

    首先配置好环境变量会比较方便... 大部分sdk提供的工具都在sdk\platform-tools和sdk\tools下,建议配置这两个路径到path 另外aapt工具在sdk\build-tools ...

  10. css spprite应用

    (一)实现简单的淘宝带图标侧边栏效果 <!DOCTYPE html> <html lang="en"> <head> <meta char ...