HDU 1402 A * B Problem Plus(快速傅里叶变换)

题意:两个数相乘,每个数的长度不超过10^5;

思路:FFT第一题。通过将系数表达式转换为点值表达式,降低复杂度;算导是个好东西!!!

用DFT实现单位复根计算点值表达式,逆DFT则将点值表达式转为系数表达式,即计算插值;复杂度均为O(n^2);

FFT采用分治的思想,将奇偶分开处理,优化DFT;复杂度为O(nlogn);

FFT处理时,(将点值扩展到2^n)->(计算点值)->(点乘运算)->(计算插值);

由于奇偶分治时结果的位置发生变化,需要在傅里叶变换时进行二进制位置互换;

每经过一次蝴蝶操作DFT次数乘二,则控制层数;

每次迭代使w的值不断变化可以节约每次通过for循环从0开始计算wn的时间;蝴蝶操作计算点值;

DFT的逆计算插值;

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define pi acos(-1.0)
char str1[5000100],str2[5000100];
int num[5000100];
int len1,len2,n,m;
struct Complex{
    double r,i;
    Complex(){};
    Complex(double x,double y){
          r=x;i=y;
    }
    Complex operator +(const Complex &t)const{
        return Complex(r+t.r,i+t.i);
    }
    Complex operator -(const Complex &t)const{
        return Complex(r-t.r,i-t.i);
    }
    Complex operator *(const Complex &t)const{
        return Complex(r*t.r-i*t.i,r*t.i+i*t.r);
    }
}x1[5000100],x2[5000100];
void fft(Complex y[],int n,int rev)
{
    for(int i=1,j,k,t;i<n;i++){
       for(j=0,k=n>>1,t=i;k;k>>=1,t>>=1) j=j<<1|t&1;  //二进制位置互换
       if(i<j) swap(y[i],y[j]);
    }
    for(int s=2,ds=1;s<=n;ds=s,s<<=1){  //控制层数
      Complex wn=Complex(cos(rev*2*pi/s),sin(rev*2*pi/s)),w=Complex(1,0),t; //初始化单位复根和螺旋因子
      for(int k=0;k<ds;k++,w=w*wn){  //更新螺旋因子
         for(int i=k;i<n;i+=s){
             t=w*y[i+ds];        //蝴蝶操作
             y[i+ds]=y[i]-t;
             y[i]=y[i]+t;
         }
      }
    }
    if(rev==-1) for(int i=0;i<n;i++) y[i].r/=n;  //求逆
}
int main()
{
    int i,j,k,t;
    while(scanf("%s%s",str1,str2)!=EOF)
    {
        len1=strlen(str1);
        len2=strlen(str2);
        n=1;
        while(n<len1+len2) n<<=1;
        for(i=0;i<len1;i++) x1[i]=Complex(str1[len1-i-1]-‘0‘,0);
        for(;i<n;i++) x1[i]=Complex(0,0);
        for(i=0;i<len2;i++) x2[i]=Complex(str2[len2-i-1]-‘0‘,0);
        for(;i<n;i++) x2[i]=Complex(0,0); //长度扩展
        fft(x1,n,1);
        fft(x2,n,1);   //系数表达式转点值表达式
        for(i=0;i<n;i++) x1[i]=x1[i]*x2[i]; //点值运算
        fft(x1,n,-1);  //求插值
        for(i=0,t=0;i<n;i++,t/=10){ //结果处理
            t+=(int)(x1[i].r+0.1);
            num[i]=t%10;
        }
        for(;t;t/=10) num[i]=t%10;
        while(n>1&&!num[n-1]) n--;
        for(i=n-1;i>=0;i--) printf("%d",num[i]);
        printf("\n");
    }
    return 0;
}
时间: 2024-08-28 08:19:08

HDU 1402 A * B Problem Plus(快速傅里叶变换)的相关文章

HDU - 1402 A * B Problem Plus FFT裸题

http://acm.hdu.edu.cn/showproblem.php?pid=1402 题意: 求$a*b$ 但是$a$和$b$的范围可以达到 $1e50000$ 题解: 显然...用字符串模拟的大数或者压位的大数是无法胜任这种计算的.... 然后,2个大整数相乘,可以理解为卷积,所以就用快速傅里叶变换(FFT)来加速他 模板题 简单总结一下对FFT的认知: FFT用于算卷积,卷积可以理解为两个多项式相乘显然复杂度是$O(n^2)$的 但是fft可以优化为$O(nlogn)$如何优化,考虑

FFT(快速傅立叶变换):HDU 1402 A * B Problem Plus

Calculate A * B. Input Each line will contain two integers A and B. Process to end of file. Note: the length of each integer will not exceed 50000. Output For each case, output A * B in one line. Sample Input 1 2 1000 2 Sample Output 2 2000 唉,模板题,膜的邝

HDU 4291 A Short problem(矩阵快速幂+循环节)

题目链接": http://acm.hdu.edu.cn/showproblem.php?pid=4291 题意: g(0)=0,g(1)=1; g(n) = 3g(n - 1) + g(n - 2): 求g(g(g(n))) mod 109 + 7 分析: 首先我们得认识到,如果一层一层算是必定会超时的. 其次,取模运算是有循环节的. step1我们找出g(x)%1000000007的循环节 mod1 step2 设g(g(n)) = g(x) x=g(n) 对mod1 取模得到mod2. 剩

HDU 1402 A * B Problem Plus FFT

A * B Problem Plus Problem Description Calculate A * B. Input Each line will contain two integers A and B. Process to end of file. Note: the length of each integer will not exceed 50000. Output For each case, output A * B in one line. Sample Input 1

HDU 1402:A * B Problem Plus

A * B Problem Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16932    Accepted Submission(s): 3558 Problem Description Calculate A * B. Input Each line will contain two integers A and B.

hdu 1402 A * B Problem Plus (FFT + 大数相乘)

A * B Problem Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13456    Accepted Submission(s): 2401 Problem Description Calculate A * B. Input Each line will contain two integers A and B.

HDU 1402 A * B Problem Plus ——(大数乘法,FFT)

因为刚学fft,想拿这题练练手,结果WA了个爽= =. 总结几点犯的错误: 1.要注意处理前导零的问题. 2.一定要注意数组大小的问题.(前一个fft的题因为没用到b数组,所以b就没管,这里使用了b数组,结果忘记给其大小乘以4倍了) 代码如下: 1 #include<bits/stdc++.h> 2 using namespace std; 3 const double pi = atan(1.0)*4; 4 const int N = 5e4 + 5; 5 typedef long long

浅谈FFT(快速傅里叶变换)

本文主要简单写写自己学习FFT的经历以及一些自己的理解和想法. FFT的介绍以及入门就不赘述了,网上有许多相关的资料,入门的话推荐这篇博客:FFT(最详细最通俗的入门手册),里面介绍得很详细. 为什么要学习FFT呢?因为FFT能将多项式乘法的时间复杂度由朴素的$O(n^2)$降到$O(nlogn)$,这相当于能将任意形如$f[k]=\sum\limits _{i+j=k}f[i]*f[j]$的转移方程的计算在$O(nlogn)$的时间内完成.因此对于想要进阶dp的同学来说,FFT是必须掌握的技能

A * B Problem Plus HDU - 1402 (FFT)

A * B Problem Plus HDU - 1402 第一道FFT... 1 #include <iostream> 2 #include <complex> 3 #include <cstdio> 4 #include <cstring> 5 #include <cmath> 6 #include <algorithm> 7 using namespace std; 8 9 const int maxn = 200010; 1