FFT之大数乘法
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
#define N 50500*2
const double PI = acos(-1.0);
struct Vir
{
double re, im;
Vir(double _re = ., double _im = .) :re(_re), im(_im){}
Vir operator*(Vir r) { return Vir(re*r.re - im*r.im, re*r.im + im*r.re); }
Vir operator+(Vir r) { return Vir(re + r.re, im + r.im); }
Vir operator-(Vir r) { return Vir(re - r.re, im - r.im); }
};
void bit_rev(Vir *a, int loglen, int len)
{
for (int i = ; i < len; ++i)
{
int t = i, p = ;
for (int j = ; j < loglen; ++j)
{
p <<= ;
p = p | (t & );
t >>= ;
}
if (p < i)
{
Vir temp = a[p];
a[p] = a[i];
a[i] = temp;
}
}
}
void FFT(Vir *a, int loglen, int len, int on)
{
bit_rev(a, loglen, len); for (int s = , m = ; s <= loglen; ++s, m <<= )
{
Vir wn = Vir(cos( * PI*on / m), sin( * PI*on / m));
for (int i = ; i < len; i += m)
{
Vir w = Vir(1.0, );
for (int j = ; j < m / ; ++j)
{
Vir u = a[i + j];
Vir v = w*a[i + j + m / ];
a[i + j] = u + v;
a[i + j + m / ] = u - v;
w = w*wn;
}
}
}
if (on == -)
{
for (int i = ; i < len; ++i) a[i].re /= len, a[i].im /= len;
}
}
char a[N * ], b[N * ];
Vir pa[N * ], pb[N * ];
int ans[N * ];
int main()
{
while (scanf("%s%s", a, b) != EOF)
{
int lena = strlen(a);
int lenb = strlen(b);
int n = , loglen = ;
while (n < lena + lenb) n <<= , loglen++;
for (int i = , j = lena - ; i < n; ++i, --j)
pa[i] = Vir(j >= ? a[j] - '' : ., .);
for (int i = , j = lenb - ; i < n; ++i, --j)
pb[i] = Vir(j >= ? b[j] - '' : ., .);
for (int i = ; i <= n; ++i) ans[i] = ; FFT(pa, loglen, n, );
FFT(pb, loglen, n, );
for (int i = ; i < n; ++i)
pa[i] = pa[i] * pb[i];
FFT(pa, loglen, n, -); for (int i = ; i < n; ++i) ans[i] = pa[i].re + 0.5;
for (int i = ; i<n; ++i) ans[i + ] += ans[i] / , ans[i] %= ; int pos = lena + lenb - ;
for (; pos> && ans[pos] <= ; --pos);
for (; pos >= ; --pos) printf("%d", ans[pos]);
puts("");
}
return ;
}
FFT之大数乘法的更多相关文章
- HDU1402:A * B Problem Plus(FFT与大数乘法)
Calculate A * B. Input Each line will contain two integers A and B. Process to end of file. Note: th ...
- 1028 大数乘法 V2(FFT or py)
1028 大数乘法 V2 基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 给出2个大整数A,B,计算A*B的结果. Input 第1行:大数A 第2行:大数B ...
- ACM学习历程—51NOD1028 大数乘法V2(FFT)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1028 题目大意就是求两个大数的乘法. 但是用普通的大数乘法,这 ...
- [hdu1402]大数乘法(FFT模板)
题意:大数乘法 思路:FFT模板 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ...
- HDU 1402 FFT 大数乘法
$A * B$ FFT模板题,找到了一个看起来很清爽的模板 /** @Date : 2017-09-19 22:12:08 * @FileName: HDU 1402 FFT 大整数乘法.cpp * ...
- 51nod 1027大数乘法
题目链接:51nod 1027大数乘法 直接模板了. #include<cstdio> #include<cstring> using namespace std; ; ; ; ...
- [POJ] #1001# Exponentiation : 大数乘法
一. 题目 Exponentiation Time Limit: 500MS Memory Limit: 10000K Total Submissions: 156373 Accepted: ...
- 用分治法实现大数乘法,加法,减法(java实现)
大数乘法即多项式乘法问题,求A(x)与B(x)的乘积C(x),朴素解法的复杂度O(n^2),基本思想是把多项式A(x)与B(x)写成 A(x)=a*x^m+b B(x)=c*x^m+d 其中a,b,c ...
- HDOJ-1042 N!(大数乘法)
http://acm.hdu.edu.cn/showproblem.php?pid=1042 题意清晰..简单明了开门见山的大数乘法.. 10000的阶乘有35000多位 数组有36000够了 # i ...
随机推荐
- [转]Zen Cart官网屏蔽中国用户访问的真正原因
近需要到 zen cart 的官方网站查询一些资料,却发现无法访问!在网上搜索一番以后,原来如此. Zen Cart官网屏蔽中国用户访问的真正原因 作者:[鹏程万里] 日期:2011-03-26 准备 ...
- Input Team
The Chromium Input team (aka input-dev) is a web platform team focused on making touch (P1) and othe ...
- CentOS 7.4 ifconfig, ip/ss, nmcli, nmtui, 配置文件 修改ip信息用法
CentOS 7.4 ifconfig, ip/ss, nmcli, nmtui, 配置文件 修改ip信息用法 CentOS 7.4 中, 网卡命名方式发生改变, 可预测功能命名: 网卡简要名称组成格 ...
- DedeCMS文章编辑不更新时间1970年1月1日
在修改文章或者后期优化的时候,织梦dedecms5.7版本存在一个问题,修改文章的同时也修改了文章的发布时间,这个功能可能有些人比较需要,但同时也有些站长朋友又不需要,因为我们编辑某个文章的时候,发现 ...
- 紫书 例题 10-23 UVa 10213(欧拉公式+高精度)
用欧拉公式V-E+F=2 V是顶点数,E是边数,F是面数 具体推导见https://blog.csdn.net/QWsin/article/details/53635397 要用高精度 #includ ...
- 【习题 8-20 UVA-1620】Lazy Susan
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 会发现,如果把连续4个数字进行一次翻转的话. 假设这连续的4个数字的逆序数为x; 那么翻转过后,逆序数就会变成6-x; (最多6个逆 ...
- 最全面的AndroidStudio配置指南总结-包括护眼模式
使用AndroidStudio开发APP已有半年多的时间了,从刚开始的不习惯到慢慢适应再到逐渐喜欢上AndroidStudio,中间的过程颇有一番曲折,现在把自己对AndroidStudio的配置心得 ...
- iPad popView封装
仿照UITableView的UITableViewDataSource 协义 1.代理.让代理帮我们类完毕一些方法 2.完毕当前类不能完毕的事情还有传值等功能 实现方法 // 1. 声明一个协议 // ...
- awk技巧
1通过awk脚本执行awk程序:awk-f program_file_name input_files #!/bin/awk -f BEGIN { print "What is your n ...
- Lesson 2 Building your first web page: Part 1
In this ‘hands-on’ module we will be building our first web page in no time. We just need to quickly ...