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
直接上代码:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define MAXN 133333
const double PI=acos(-1.0); struct Complex{
double real,imag;
Complex(){}
Complex(double _real,double _imag):real(_real),imag(_imag){}
Complex operator-(const Complex &cp) const{
return Complex(this->real-cp.real,this->imag-cp.imag);
}
Complex operator+(const Complex &cp) const{
return Complex(this->real+cp.real,this->imag+cp.imag);
}
Complex operator*(const Complex &cp) const{
return Complex(this->real*cp.real-this->imag*cp.imag,this->real*cp.imag+this->imag*cp.real);
}
void setValue(double _real=0,double _imag=0){
real=_real; imag=_imag;
}
}; int len;
Complex wn[MAXN],wn_anti[MAXN]; void FFT(Complex y[],int op){
// Rader, 位逆序置换
for(int i=1,j=len>>1,k; i<len-1; ++i){
if(i<j) swap(y[i],y[j]);
k=len>>1;
while(j>=k){
j-=k;
k>>=1;
}
if(j<k) j+=k;
}
// h=1,Wn^0=1
for(int h=2; h<=len; h<<=1){
// Wn = e^(2*PI*i/n),如果是插值Wn = e^(-2*PI*i/n),i虚数单位
Complex Wn = (op==1 ? wn[h] : wn_anti[h]);
for(int i=0; i<len; i+=h){
Complex W(1,0);
for(int j=i; j<i+(h>>1); ++j){
Complex u=y[j],t=W*y[j+(h>>1)];
y[j]=u+t;
y[j+(h>>1)]=u-t;
W=W*Wn;
}
}
}
if(op==-1){
for(int i=0; i<len; ++i) y[i].real/=len;
}
}
void Convolution(Complex A[],Complex B[],int n){
// 初始化
for(len=1; len<(n<<1); len<<=1);
for(int i=n; i<len; ++i){
A[i].setValue();
B[i].setValue();
}
// e^(θi) = cosθ+isinθ -> Wn = cos(2*PI/n)+isin(2*PI/n)
for(int i=0; i<=len; ++i){ // 预处理
wn[i].setValue(cos(2.0*PI/i),sin(2.0*PI/i));
wn_anti[i].setValue(wn[i].real,-wn[i].imag);
}
FFT(A,1); FFT(B,1);
for(int i=0; i<len; ++i){
A[i]=A[i]*B[i];
}
FFT(A,-1);
} char s1[55555],s2[55555];
Complex A[MAXN],B[MAXN];
int ans[MAXN]; int main(){
while(scanf("%s%s",s1,s2)==2){
int l1=strlen(s1),l2=strlen(s2);
for(int i=0; i<l1; ++i){
A[i].setValue(s1[l1-i-1]-'0');
}
for(int i=0; i<l2; ++i){
B[i].setValue(s2[l2-i-1]-'0');
}
if(l1<l2){
for(int i=l1; i<l2; ++i) A[i].setValue();
l1=l2;
}else{
for(int i=l2; i<l1; ++i) B[i].setValue();
}
Convolution(A,B,l1);
for(int i=0; i<len; ++i){
ans[i]=(int)(A[i].real+0.5);
}
for(int i=0; i<len; ++i){
ans[i+1]+=ans[i]/10;
ans[i]%=10;
}
while(--len && ans[len]==0);
for(int i=len; i>=0; --i) printf("%d",ans[i]);
putchar('\n');
}
return 0;
}
HDU1402 A * B Problem Plus(FFT)的更多相关文章
- HDU 1402 A * B Problem Plus(FFT)
Problem Description Calculate A * B. Input Each line will contain two integers A and B. Process to ...
- 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 ...
- 洛谷P1919 【模板】A*B Problem升级版(FFT)
传送门 话说FFT该不会真的只能用来做这种板子吧…… 我们把两个数字的每一位都看作多项式的系数 然后这就是一个多项式乘法 上FFT就好了 然后去掉前导零 (然而连FFT的板子都背不来orz,而且空间又 ...
- 【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 ...
- Luogu1919 【模板】A*B Problem升级版(FFT)
简单的\(A*B\) \(Problem\),卡精度卡到想女装 #include <iostream> #include <cstdio> #include <cstri ...
- 【Luogu1919】 A*B Problem升级版(FFT)
题面戳我 题解 把每个数都直接看做一个多项式,每一位就是一项 现在求用FFT求出卷积 然后考虑一下进位就可以啦 #include<iostream> #include<cstdio& ...
- 【CF954I】Yet Another String Matching Problem(FFT)
[CF954I]Yet Another String Matching Problem(FFT) 题面 给定两个字符串\(S,T\) 求\(S\)所有长度为\(|T|\)的子串与\(T\)的距离 两个 ...
- 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 ...
- 【数学】快速傅里叶变换(FFT)
快速傅里叶变换(FFT) FFT 是之前学的,现在过了比较久的时间,终于打算在回顾的时候系统地整理一篇笔记,有写错的部分请指出来啊 qwq. 卷积 卷积.旋积或褶积(英语:Convolution)是通 ...
随机推荐
- .net 获取https页面的信息 在iis7.5服务器上不管用
让我纠结了一天多的问题,给大家看下,有相同情况的可以不用浪费时间了,本人当时找了好半天都没找到什么有用的信息,项目在本地没有问题,但部署在服务器后,获取不到https页面的信息,加入下面的代码就可以了 ...
- Mysql相关集锦
1.MyBatis中设置或获取插入的自增主键 http://my.oschina.net/kolbe/blog/512904 2.MySql性能调优与架构设计系列 http://www.cnblogs ...
- tomcat和apache区别联系
tomcat和apache区别联系 Apache是普通服务器,本身只支持html即普通网页.不过可以通过插件支持php,还可以与Tomcat连通(单向Apache连接Tomcat, 就是说通过Apac ...
- ios Push证书 转换步骤
1.将aps_developer_identity.cer转换成aps_developer_identity.pem格式openssl x509 -in aps_developer_identity. ...
- C++面向对象基础知识
多态是为了接口重用,封装和继承是为了代码重用 子类重新定义父类虚函数的方法叫做继承,不是重载! 一.基本概念 对于C++中经常出现的函数名称相同但是参数列表或者返回值不同的函数,主要存在三种情况: 1 ...
- MVC - 16.MVC过滤器
filter n. 滤波器:[化工] 过滤器:筛选:滤光器 vt. 过滤:渗透:用过滤法除去 1.过滤器表 过滤器类型 接口 默认实现 描述 Action IActionFilte ...
- Java学习笔记(十六)——Java RMI
[前面的话] 最近过的好舒服,每天过的感觉很充实,一些生活和工作的技巧注意了就会发现,其实生活也是可以过的如此的有滋有味,满足现在的状况,并且感觉很幸福. 学习java RMI的原因是最近在使用dub ...
- 微信支付开发(1) JS API支付V3版(转)
http://www.cnblogs.com/txw1958/p/wxpayv3-jsapi.html 本文介绍微信支付下的jsapi实现流程 前言 微信支付现在分为v2版和v3版,2014年9月10 ...
- Memcached缓存在.Net 中的使用(memcacheddotnet)
缓存对于提高大数据量的网站性能无疑不是一个很好的解决方案,针对缓存的使用网上同仁介绍很多,再次我仅仅分享一下自己对Memcached使用的简单介绍.Memchached的使用通过第三方DLL来完成,常 ...
- Web框架本质
Web框架本质 众所周知,对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. #!/usr/bin/env python #coding:utf- ...