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. [Android Pro] APK

    svn updatesvn status ls -alsvn log --limit 8 > RELEASE_NOTE.txt cat RELEASE_NOTE.txt chmod a+x gr ...

  2. August 12th 2016 Week 33rd Friday

    Everything is good in its season. 万物逢时皆美好. Every dog has its day. You are not in your best condition ...

  3. mongodb 3.2 用户权限管理配置

    使用mongodb 有段时间了,由于是在内网使用,便没有设置权限,一直是裸奔. 最近有时间,研究了下mongodb 3.2 的用户权限配置,网上有许多用户权限配置的文章,不过大多是之前版本,有些出入, ...

  4. Callable 和 Future接口 学习

    * Callable是类似于Runnable的接口,实现Callable接口的类和实现Runnable的类都是可被其它线程执行的任务. * Callable和Runnable有几点不同: * (1)C ...

  5. Swing开发之JComboBox篇

    http://blog.csdn.net/sjf0115/article/details/6991579

  6. UVA 624 CD

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

  7. java07课堂作业

    一.动手动脑:多层的异常捕获-1 阅读以下代码(CatchWho.java),写出程序运行结果: public class CatchWho { public static void main(Str ...

  8. 通过btn获取所在cell

    [cell.btn addTarget:self action:@selector(cellBtnClicked:event:) forControlEvents:UIControlEventTouc ...

  9. Pyqt 中__init__(self,parent==None) parent理解

    参考: 在PyQt中,所有class都是从QObject派生而来,QWidget对象就可以有一个parent.这种parent-child关系主要用于两个方面: 没有parent的QWidget类被认 ...

  10. HTTPCLIENT 学习 (1) 入门

    早就如雷贯耳它的大名,却一直不曾相见,昨天下载下来,今天终于测试了一把,用的官网的QUICK START例子,来访问我自己以前开发过的WEB程序,因为这个网站恰好有一个写好的通过POST请求验证用户名 ...