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)的更多相关文章

  1. HDU 1402 A * B Problem Plus(FFT)

    Problem Description Calculate A * B.   Input Each line will contain two integers A and B. Process to ...

  2. 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 ...

  3. 洛谷P1919 【模板】A*B Problem升级版(FFT)

    传送门 话说FFT该不会真的只能用来做这种板子吧…… 我们把两个数字的每一位都看作多项式的系数 然后这就是一个多项式乘法 上FFT就好了 然后去掉前导零 (然而连FFT的板子都背不来orz,而且空间又 ...

  4. 【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 ...

  5. Luogu1919 【模板】A*B Problem升级版(FFT)

    简单的\(A*B\) \(Problem\),卡精度卡到想女装 #include <iostream> #include <cstdio> #include <cstri ...

  6. 【Luogu1919】 A*B Problem升级版(FFT)

    题面戳我 题解 把每个数都直接看做一个多项式,每一位就是一项 现在求用FFT求出卷积 然后考虑一下进位就可以啦 #include<iostream> #include<cstdio& ...

  7. 【CF954I】Yet Another String Matching Problem(FFT)

    [CF954I]Yet Another String Matching Problem(FFT) 题面 给定两个字符串\(S,T\) 求\(S\)所有长度为\(|T|\)的子串与\(T\)的距离 两个 ...

  8. 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 ...

  9. 【数学】快速傅里叶变换(FFT)

    快速傅里叶变换(FFT) FFT 是之前学的,现在过了比较久的时间,终于打算在回顾的时候系统地整理一篇笔记,有写错的部分请指出来啊 qwq. 卷积 卷积.旋积或褶积(英语:Convolution)是通 ...

随机推荐

  1. iOS真机调试

    备注:本阶段之前的修改配置文件.准备脚本等,只需要做一次.但本阶段的操作,对每个需要真机调试的工程都要做一遍. ① 禁用Xcode自动的签名操作 将工程配置“Build Settings”中所有的Co ...

  2. UVA 624 CD

    主要是打印路径有点麻烦,然后就是用于标记的数组要开大点,不然会狂wa不止,而且还不告诉你re #include <cstdio> #include <iostream> #in ...

  3. 数据结构和算法 – 9.二叉树和二叉查找树

      9.1.树的定义   9.2.二叉树 人们把每个节点最多拥有不超过两个子节点的树定义为二叉树.由于限制子节点的数量为 2,人们可以为插入数据.删除数据.以及在二叉树中查找数据编写有效的程序了. 在 ...

  4. Dubbo集成Spring与Zookeeper实例

    >>Dubbo最佳实践 使用Dubbo结合Zookeeper和Spring,是使用比较广泛的一种组合,下面参考官方文档,做个简单的示例,一步步搭建一个使用dubbo结合Zookeeper和 ...

  5. ***PHP中error_reporting()用法详解(含codeigniter框架中屏蔽错误提示的解决方案)

    php中我们对错误的处理会常用到error_reporting函数了,大家可以看到最多的是error_reporting(E_ALL ^ E_NOTICE)了,这个到底什么意思呢,下面我来来看看. e ...

  6. 手机站点动态效果插件TouchSlide

    今天看到TouchSlide插件,觉得非常不错,关于使用情况请看demo,下载地址:http://www.superslide2.com/TouchSlide/downLoad.html

  7. 几年前做家教写的C教程(之三专讲了递归和斐波那契)

    C语言学习宝典(3) 数组: 一维数组的定义: 类型说明符  数组名[常量表达式] 例如: int  a[10]; 说明:(1)数组名的命名规则和变量名相同,遵循标示符命名规则 (2)在定义数组时需要 ...

  8. Code Review for SSIS package

    以下是我对SSIS包进行code review的一些建议,如果有其他更好的方案欢迎拍砖. A. 查看是否使用了最优的解决方案 1. 最优的结构视图 2. 解决方案,包,任务,组建,参数的命名使用了易读 ...

  9. 攻城狮在路上(壹) Hibernate(十三)--- Hibernate的检索方式(上)

    Hibernate提供了以下几种检索对象的方式: A.导航对象图检索方式. B.OID检索方式.Session.get() load(); C.HQL检索方式.Query. D.QBC检索方式.Que ...

  10. maven File encoding has not been set

    原pom.xml配置文件: <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&qu ...