HDU 1402 A * B Problem Plus (FFT模板题)
FFT模板题,求A*B。
用次FFT模板需要注意的是,N应为2的幂次,不然二进制平摊反转置换会出现死循环。
取出结果值时注意精度,要加上eps才能A。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
const double pi = acos(-1.0);
const int maxn = 50000 + 5;
const double eps = 1e-6; struct Complex {
double a, b;
Complex() {
}
Complex(double a, double b) :
a(a), b(b) {
}
Complex operator +(const Complex& t) const {
return Complex(a + t.a, b + t.b);
}
Complex operator -(const Complex& t) const {
return Complex(a - t.a, b - t.b);
}
Complex operator *(const Complex& t) const {
return Complex(a * t.a - b * t.b, a * t.b + b * t.a);
}
}; // 二进制平摊反转置换
void brc(Complex *x, int n) {
int i, j, k;
for (i = 1, j = n >> 1; i < n - 1; i++) {
if (i < j)
swap(x[i], x[j]); k = n >> 1;
while (j >= k) {
j -= k;
k >>= 1;
}
if (j < k)
j += k;
}
} // FFT,其中on==1时为DFT,on==-1时为IDFT
void FFT(Complex *x, int n, int on) {
int h, i, j, k, p;
double r;
Complex u, t;
brc(x, n);
for (h = 2; h <= n; h <<= 1) { // 控制层数
r = on * 2.0 * pi / h;
Complex wn(cos(r), sin(r));
p = h >> 1;
for (j = 0; j < n; j += h) {
Complex w(1, 0);
for (k = j; k < j + p; k++) {
u = x[k];
t = w * x[k + p];
x[k] = u + t;
x[k + p] = u - t;
w = w * wn;
}
}
}
if (on == -1) // IDFT
for (i = 0; i < n; i++)
x[i].a = x[i].a / n + eps;
} int n, ma, N;
Complex x1[maxn<<2], x2[maxn<<2];
char sa[maxn], sb[maxn];
int ans[maxn<<1]; void solve() {
int n1 = strlen(sa), n2 = strlen(sb);
int N = 1, tmpn = max(n1, n2) << 1;
// N应为2的幂次
while(N < tmpn) N <<= 1;
for(int i = 0;i < N; i++)
x1[i].a = x1[i].b = x2[i].a = x2[i].b = 0;
for(int i = 0;i < n1; i++)
x1[i].a = sa[n1-i-1] - '0';
for(int i = 0;i < n2; i++)
x2[i].a = sb[n2-i-1] - '0';
FFT(x1, N, 1); FFT(x2, N, 1);
for(int i = 0;i < N; i++)
x1[i] = x1[i]*x2[i];
FFT(x1, N, -1);
int pre = 0, top = 0;
for(int i = 0;i < n1+n2; i++) {
// 不加epsA不了~
int cur = (int)(x1[i].a + eps);
ans[++top] = (cur + pre)%10;
pre = (pre + cur)/10;
}
while(!ans[top] && top > 1) top--;
for(int i = top;i >= 1; i--)
printf("%d", ans[i]);
puts("");
} int main() {
while(scanf("%s%s", sa, &sb) != -1) {
solve();
}
return 0;
}
HDU 1402 A * B Problem Plus (FFT模板题)的更多相关文章
- HDU - 1402 A * B Problem Plus FFT裸题
http://acm.hdu.edu.cn/showproblem.php?pid=1402 题意: 求$a*b$ 但是$a$和$b$的范围可以达到 $1e50000$ 题解: 显然...用字符串模拟 ...
- hdu 1402 A * B Problem Plus FFT
/* hdu 1402 A * B Problem Plus FFT 这是我的第二道FFT的题 第一题是完全照着别人的代码敲出来的,也不明白是什么意思 这个代码是在前一题的基础上改的 做完这个题,我才 ...
- HDU 1402 A * B Problem Plus (FFT求高精度乘法)
A * B Problem Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU - 1402 A * B Problem Plus (FFT实现高精度乘法)
题意:计算A*B,A,B均为长度小于50000的整数. 这是FFT在大整数相乘中的一个应用,我本来想用NTT做的,但NTT由于取模很可能取炸,所以base必须设得很小,而且效率也比不上FFT. A和B ...
- [hdu1402]A * B Problem Plus(FFT模板题)
解题关键:快速傅里叶变换fft练习. 关于结果多项式长度的确定,首先将短多项式扩展为长多项式,然后扩展为两倍. #include<cstdio> #include<cstring&g ...
- HDU 5832 A water problem(某水题)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- 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 ...
- FFT(快速傅立叶变换):HDU 1402 A * B Problem Plus
Calculate A * B. Input Each line will contain two integers A and B. Process to end of file. Note: th ...
随机推荐
- MYSQL视图的学习笔记
MYSQL视图的学习笔记,学至Tarena金牌讲师,金色晨曦科技公司技术总监沙利穆 课程笔记的综合. 视图及图形化工具 1. 视图的定义 视图就是从一个或多个表中,导出来的表,是一个虚 ...
- mysql数据库优化[千万级查询]
1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...
- java使用ObjectInputStream从文件中读取对象
import java.io.EOFException;import java.io.FileInputStream;import java.io.FileNotFoundException;impo ...
- 一个安全测试的CheckList
转自:http://hi.baidu.com/dontcry/item/90c2bc466558c217886d1075 不登录系统,直接输入登录后的页面的URL是否可以访问: 不登录系统,直接输入下 ...
- Android学习之多点触摸并不神秘
最近研究了一下多点触摸,写了个利用多点触摸来控制图片大小和单点触摸控制图片移动的程序,和大家分享分享. Android中监听触摸事件是onTouchEvent方法,它的参数为MotionEvent,下 ...
- cpio.gz 解压
linux下cpio.gz文件的解压方法:今天下载了 10201_database_linux_x86_64.cpio.gz 文件,解压方法如下:1. gunzip 10201_database_li ...
- Linux系统中C&Cpp程序开发(一)
之前一直在Windows系统下进行程序的设计,近期开始学习使用Linux系统,因而打算将程序开发也转移到Linux系统下.今天先简单介绍一下该系统下的C程序开发步骤. 首先要预先安装vim和gcc工具 ...
- C#之out与ref的共性与区别以及用法
引入: 首先看一个例子: class Program { static void Main(string[] args) { ; int result = Test(number); Console. ...
- 关于链接target的问题
<a href="http://www.baidu.com" target="_blank">点击链接</a> target: _bla ...
- 新手们的GDI+绘制方格
//绘制panel控件触发的事件 //不可在窗体加载时绘制方格 private void panel1_Paint(object sender, PaintEventArgs e) ...