A * B Problem Plus(fft)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1402
hdu_1402:A * B Problem Plus
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15419 Accepted Submission(s):
3047
end of file.
Note: the length of each integer will not exceed
50000.
2
1000
2
2000
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int MAX=;
const double PI=acos(-1.0),eps=1e-;;
double cof1[MAX], cof2[MAX];
int n, k, permutation[MAX];
char s1[MAX],s2[MAX];
int ans[MAX];
struct complex {//复数
double r, v;
complex operator + (complex& obj) {
complex temp;
temp.r = r + obj.r;
temp.v = v + obj.v;
return temp;
}
complex operator - (complex& obj) {
complex temp;
temp.r = r - obj.r;
temp.v= v - obj.v;
return temp;
}
complex operator * ( complex& obj) {
complex temp;
temp.r = r*obj.r - v*obj.v;
temp.v = r*obj.v + v*obj.r;
return temp;
}
} p1[MAX], p2[MAX], omiga[MAX], result1[MAX], result2[MAX];
void caculate_permutation(int s, int interval, int w, int next) {
if(interval==n) {
permutation[w] = s;
return ;
}
caculate_permutation(s,interval*, w, next/);
caculate_permutation(s+interval, interval*, w+next, next/);
}
void fft(complex transform[], complex p[]) {
int i, j, l, num, m;
complex temp1, temp2;
for(i=; i<n; i++)transform[i] = p[ permutation[i] ] ;
num = , m = n;
for(i=; i<=k; i++) {
for(j=; j<n; j+=num*)
for(l=; l<num; l++)
temp2 = omiga[m*l]*transform[j+l+num],
temp1 = transform[j+l],
transform[j+l] = temp1 + temp2,
transform[j+l+num] = temp1 - temp2;
num*=,m/=;
}
}
void polynomial_by(int n1,int n2) {//多项式乘法,cof1、cof2保存的是a[0],a[1]..a[n-1]的值(a[i]*x^i)
int i;
double angle;
k = , n = ;
while(n<n1+n2-)k++,n*=;
for(i=; i<n1; i++)p1[i].r = cof1[i], p1[i].v = ;
while(i<n)p1[i].r = p1[i].v = , i++;
for(i=; i<n2; i++)p2[i].r = cof2[i], p2[i].v = ;
while(i<n)p2[i].r = p2[i].v = , i++;
caculate_permutation(,,,n/);
angle = PI/n;
for(i=; i<n; i++)omiga[i].r = cos(angle*i), omiga[i].v = sin(angle*i);
fft(result1,p1);
fft(result2,p2);
for(i=; i<n; i++)result1[i]= result1[i]*result2[i];
for(i=; i<n; i++)omiga[i].v = -omiga[i].v;
fft(result2, result1);
for(i=; i<n; i++)result2[i].r/=n;
i = n -;
while(i&&fabs(result2[i].r)<eps)i--;
n = i+;
while(i>=) ans[i]=(int)(result2[i].r+0.5), i--;
}
int main() {
while(scanf("%s",s1)!=EOF){
scanf("%s",s2);
int n1=strlen(s1),n2=strlen(s2);
for(int i=;i<n1;i++){
cof1[i]=s1[n1--i]-'';
}
for(int i=;i<n2;i++){
cof2[i]=s2[n2--i]-'';
}
memset(ans,,sizeof(ans));
polynomial_by(n1,n2);
for(int i=;i<n;i++){
if(ans[i]>=){
ans[i+]+=ans[i]/;
ans[i]%=;
}
}
while(ans[n]>){
if(ans[n]>=){
ans[n+]+=ans[n]/;
ans[n]%=;
}
n++;
}
for(int i=n-;i>=;i--){
putchar(''+ans[i]);
}
puts("");
}
return ;
}
A * B Problem Plus(fft)的更多相关文章
- hdu 1402 A * B Problem Plus FFT
/* hdu 1402 A * B Problem Plus FFT 这是我的第二道FFT的题 第一题是完全照着别人的代码敲出来的,也不明白是什么意思 这个代码是在前一题的基础上改的 做完这个题,我才 ...
- 【CF954I】Yet Another String Matching Problem(FFT)
[CF954I]Yet Another String Matching Problem(FFT) 题面 给定两个字符串\(S,T\) 求\(S\)所有长度为\(|T|\)的子串与\(T\)的距离 两个 ...
- 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 ...
- CODEVS3123 a*b problem plus (FFT)
type xh=record x,y:double; end; arr=..] of xh; var n,m:longint; s1,s2:ansistring; a,b,g,w:arr; ch:ch ...
- HDU1402 A * B Problem Plus FFT
分析:网上别家的代码都分析的很好,我只是给我自己贴个代码,我是kuangbin的搬运工 一点想法:其实FFT就是快速求卷积罢了,当小数据的时候我们完全可以用母函数来做,比如那种硬币问题 FFT只是用来 ...
- HDU-1402 A * B Problem Plus FFT(快速傅立叶变化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1402 一般的的大数乘法都是直接模拟乘法演算过程,复杂度O(n^2),对于这题来说会超时.乘法的过程基本 ...
- HDU 1402 A * B Problem Plus (FFT模板题)
FFT模板题,求A*B. 用次FFT模板需要注意的是,N应为2的幂次,不然二进制平摊反转置换会出现死循环. 取出结果值时注意精度,要加上eps才能A. #include <cstdio> ...
- HDU - 1402 A * B Problem Plus FFT裸题
http://acm.hdu.edu.cn/showproblem.php?pid=1402 题意: 求$a*b$ 但是$a$和$b$的范围可以达到 $1e50000$ 题解: 显然...用字符串模拟 ...
- 洛谷.1919.[模板]A*B Problem升级版(FFT)
题目链接:洛谷.BZOJ2179 //将乘数拆成 a0*10^n + a1*10^(n-1) + ... + a_n-1的形式 //可以发现多项式乘法就模拟了竖式乘法 所以用FFT即可 注意处理进位 ...
随机推荐
- boost::algorithm(字符串算法库)
没什么说的,需要 #include<boost/algorithm/string.hpp> 1.大小写转换 std::string s("test string"); ...
- 第四节:dingo/API 最新版 V2.0 之 Responses (连载)
因为某些某些原因,不能按时更新,唉.我会尽力,加快速度.(这句话不是翻译的哈) 原文地址--> https://github.com/dingo/api/wiki/Responses A fun ...
- Docker(十):Docker安全
1.Docker安全主要体现在如下方面 a)Docker容器的安全性 b)镜像安全性 c)Docker daemon安全性 2.安装策略 2.1 Cgroup Cgroup用于限制容器对CPU.内存的 ...
- Android 7.1 WindowManagerService 屏幕旋转流程分析 (三)
三.屏幕的绘制 performSurfacePlacement()函数来触发window的绘制,这里最大的循环次数是6,当然一般不会到最大次数就会被Scheduled. final void perf ...
- Python并发实践_02_通过yield实现协程
python中实现并发的方式有很多种,通过多进程并发可以真正利用多核资源,而多线程并发则实现了进程内资源的共享,然而Python中由于GIL的存在,多线程是没有办法真正实现多核资源的. 对于计算密集型 ...
- Python学习_08_函数式编程
在python中,函数名也是一个变量,代表对一个函数内容的引用,意味着可以作为参数传入到其他函数中,根据这个特性,发散出装饰器.闭包等概念,并涉及到变量作用域等问题. 函数 python中函数操作符为 ...
- Vue 爬坑之路(九)—— 用正确的姿势封装组件
迄今为止做的最大的 Vue 项目终于提交测试,天天加班的日子终于告一段落... 在开发过程中,结合 Vue 组件化的特性,开发通用组件是很基础且重要的工作 通用组件必须具备高性能.低耦合的特性 为了满 ...
- python中输出内容颜色得控制
参考:http://www.jb51.net/article/51237.htm 颜色代码 1)代码列表 格式:\[显示方式;前景色;背景色m 说明: 前景色 背景色 颜色 ------------- ...
- zxing .net 多种条码格式的生成
下载地址:http://zxingnet.codeplex.com/ zxing.net是.net平台下编解条形码和二维码的工具,使用非常方便. 本文主要说明一下多种类型条码的生成. 适用的场景,标签 ...
- OS.path部分函数的介绍
OS.path模块中的部分函数的介绍 os.path.abspath(path) #返回绝对路径 os.path.basename(path) #返回文件名 os.path.commonprefix( ...