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

    可以帮助分析慢查询. 选项: -n 10 列出最近10条慢查询 如: mysqldumpslow

  2. jquery学习笔记---requirejs 和模块化编程

    http://www.cnblogs.com/lisongy/p/4711056.html jquery模块化编程:http://www.cnblogs.com/digdeep/p/4602460.h ...

  3. android:installLocation = "auto" 的用法

    在Froyo(android 2.2,API Level:8)中引入了android:installLocation.通过设置该属性可以使得开发者以及用户决定程序的安装位置. android:inst ...

  4. Pyqt 一个简单的浏览器

    使用QtWebKit 做一个简单的浏览器. mybrowserUI.ui <?xml version="1.0" encoding="UTF-8"?> ...

  5. 【JAVA基本数据类型包装类】

    一.概述 JAVA中一共有8种数据类型,分别是byte short int long boolean float double  char,与此相对应的,有8个类与它们分别对应: byte Byte ...

  6. A-B 练习【大数减法举例】

      A-B Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 题目链接:http://acm.sdut.edu.cn/sdutoj/ ...

  7. blender源代码编译

    blender源码路径(svn):https://svn.blender.org/svnroot/bf-blender/trunk/blender/ 依赖外部Lib(svn):https://svn. ...

  8. 3-1创建Sql Sever数据库登录名

    登录名:连接Sql Sever 服务器 数据库用户名: Sql Sever 的使用者 每个用来登录Sql Sever 的账户都是一个用户. 同一个数据库可以拥有多个用户,每一个用户也同时可以访问多个数 ...

  9. FreeSWITCH 体系配置结构

    转自:http://www.cnblogs.com/logo-fox/archive/2013/12/09/3465440.html FreeSWITCH总体结构: FreeSWITCH 由一个稳定的 ...

  10. 注解:【无连接表的】Hibernate单向1->N关联

    Person与Address关联:单向1->N,[无连接表的] (性能较低,不推荐使用!) Person.java package org.crazyit.app.domain; import ...