HDU 1402 A * B Problem Plus(FFT)
Note: the length of each integer will not exceed 50000.
代码(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)的更多相关文章
- hdu 1402 A * B Problem Plus (FFT模板)
A * B Problem Plus Problem Description Calculate A * B. Input Each line will contain two integers A ...
- 【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 ...
- HDU 1402 A * B Problem Plus ——(大数乘法,FFT)
因为刚学fft,想拿这题练练手,结果WA了个爽= =. 总结几点犯的错误: 1.要注意处理前导零的问题. 2.一定要注意数组大小的问题.(前一个fft的题因为没用到b数组,所以b就没管,这里使用了b数 ...
- 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 ...
- 洛谷P1919 【模板】A*B Problem升级版(FFT)
传送门 话说FFT该不会真的只能用来做这种板子吧…… 我们把两个数字的每一位都看作多项式的系数 然后这就是一个多项式乘法 上FFT就好了 然后去掉前导零 (然而连FFT的板子都背不来orz,而且空间又 ...
- Luogu1919 【模板】A*B Problem升级版(FFT)
简单的\(A*B\) \(Problem\),卡精度卡到想女装 #include <iostream> #include <cstdio> #include <cstri ...
- 【Luogu1919】 A*B Problem升级版(FFT)
题面戳我 题解 把每个数都直接看做一个多项式,每一位就是一项 现在求用FFT求出卷积 然后考虑一下进位就可以啦 #include<iostream> #include<cstdio& ...
- hdu 1002 A + B Problem II(大数)
题意:就是求a+b (a,b都不超过1000位) 思路:用数组存储 第一道大数的题目,虽然很水,纪念一下! 代码: #include<cstdio> #include<cstring ...
- 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 ...
随机推荐
- SVM神经网络的术语理解
SVM(Support Vector Machine)翻译成中文是支持向量机, 这里的“机(machine,机器)”实际上是一个算法.而支持向量则是指那些在间隔区边缘的训练样本点[1]. 当初看到这个 ...
- Bluetooth LMP介绍
目录 1. 介绍 2. 数据包格式(Packet Format) 3. Procedure Rules 4. 通用回应消息(General Response Messages) 5. 设备特性(Dev ...
- Qt配置信息设置(QSettings在不同平台下的使用路径)
在Windows操作系统中,大多把配置文件信息写在注册表当中,或写在*.ini文件中,对于这两种操作都有相应的Windows API函数,在以前的文章中都提及过,这里就不多说了~ 在Qt中,提供了一个 ...
- 【Java 基础篇】【第十课】多态
Java的多态使用方法和C++基本是一样的. 看代码吧. public class ten { public static void main(String[] args) { Human guest ...
- 50道JavaScript经典题和解法(JS新手进...持续更新...)
最近在学习<数据结构与算法JavaScript描述>这本书,对JavaScript的特性和数据结构都有了进一步的了解和体会. 学习之余,也进行了相应的练习,题目难度不大,但是对所学知识的巩 ...
- ArcGIS API for Silverlight 调用GP服务绘制等值面
原文:ArcGIS API for Silverlight 调用GP服务绘制等值面 GP服务模型如下图: 示例效果图片如下:
- Linux centos关机与重启命令详解与实战
Linux centos重启命令: 1.reboot 2.shutdown -r now 立刻重启(root用户使用) 3.shutdown -r 10 过10分钟自动重启(root用户使用) 4.s ...
- Tornado-简介
定义 Tornado全称为Tornado Web Server,是一个用Python写的Web服务器和应用框架,最终由FriendFeed公司使用,后来FriendFeed被Facebook收购之后, ...
- Selenium2学习-016-WebUI自动化实战实例-014-Selenium 窗口选择
在日常的 WebUI 自动化测试脚本编写过程中,经常需要打开新的页面,或者在多个打开的页面之间进行切换,以对页面元素进行相应的操作,以模拟用户的行为,实现 UI 的自动化测试.在过往的时间中,经常有初 ...
- c# 并行运算
c# 并行运算 1. Parallel.INVOKE() 看实例: private static Stopwatch watch = new Stopwatch(); private static v ...