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

Sample Output

2
2000

题解:

  FFT入门

  注意几组数据

  0 0

  0 5

0005 000006

#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18+1LL;
const double pi = acos(-1.0);
const int N = 1e5+10, M = 1e3+20,inf = 2e9,mod = 1e9+7;

struct Complex {
    double r , i ;
    Complex () {}
    Complex ( double r , double i ) : r ( r ) , i ( 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 ) ;
    }
    Complex operator * ( const Complex& t ) const {
        return Complex ( r * t.r - i * t.i , r * t.i + i * t.r ) ;
    }
} ;

void FFT ( Complex y[] , int n , int rev ) {
    for ( int i = 1 , j , t , k ; i < n ; ++ i ) {
        for ( j = 0 , t = i , k = n >> 1 ; 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 ( 1 , 0 ) , t ;
        for ( int k = 0 ; k < ds ; ++ k , w = w * wn ) {
            for ( int i = k ; i < n ; i += s ) {
                y[i + ds] = y[i] - ( t = w * y[i + ds] ) ;
                y[i] = y[i] + t ;
            }
        }
    }
    if ( rev == -1 ) for ( int i = 0 ; i < n ; ++ i ) y[i].r /= n ;
}

Complex s[N*4],t[N*4];
char a[N],b[N];
int ans[N];
int main() {
    while(scanf("%s%s",a,b)!=EOF) {
        int n = strlen(a);
        int m = strlen(b);
        int flag2 = 0, flag1 = 0;
        for(int i = 0; i < n; ++i) {
            if(a[i] == ‘0‘ && !flag1) {
                continue;
            }
            else a[flag1++] = a[i];
        }
        for(int i = 0; i < m; ++i) {
            if(b[i] == ‘0‘ && !flag2) {
                continue;
            }
            else b[flag2++] = b[i];
        }
        n = flag1;
        m = flag2;
       // cout<<n<<" "<<m<<endl;
       if(n == 0 || m == 0) {
        puts("0");
        continue;
       }
        int n1 = 1;
        while(n1 <= m+n-2) n1<<=1;
        for(int i = 0; i < n; ++i) {
            s[i] = Complex(a[i] - ‘0‘,0);
        }
        for(int i = n; i < n1; ++i) {
            s[i] = Complex(0,0);
        }
        for(int i = 0; i < m; ++i) {
            t[i] = Complex(b[i] - ‘0‘,0);
        }
        for(int i = m; i < n1; ++i) {
            t[i] = Complex(0,0);
        }
        FFT(s,n1,1);
        FFT(t,n1,1);
        for(int i = 0; i < n1; ++i) s[i] = s[i]*t[i];
        FFT(s,n1,-1);
        int f = 1;
        int last = 0,cnt = 0;
        for(int i = n+m-2; i >= 0; --i) {
            int x = (int) (s[i].r+0.1) + last;
           //printf("%d\n",x);
            last = 0;
            if(x > 9) {
                last+=x/10;
                ans[++cnt] = x % 10;
            }
            else ans[++cnt] = x;
        }
        if(last) {
            ans[++cnt] = last;
        }
        for(int i = cnt; i >= 1; --i) printf("%d",ans[i]);
        printf("\n");
    }
    return 0;
}

  

时间: 2024-11-05 12:29:04

HDU 1402 A * B Problem Plus FFT的相关文章

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)$如何优化,考虑

hdu 1402 大数A*B模板(FFT)

hdu 1402 大数A*B模板(FFT) 题目链接 参考博客 #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> #include <complex> #include <iostream> using namespace std; typedef long long ll; template <class T> void

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.

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

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(快速傅里叶变换)

题意:两个数相乘,每个数的长度不超过10^5: 思路:FFT第一题.通过将系数表达式转换为点值表达式,降低复杂度:算导是个好东西!!! 用DFT实现单位复根计算点值表达式,逆DFT则将点值表达式转为系数表达式,即计算插值:复杂度均为O(n^2); FFT采用分治的思想,将奇偶分开处理,优化DFT:复杂度为O(nlogn); FFT处理时,(将点值扩展到2^n)->(计算点值)->(点乘运算)->(计算插值); 由于奇偶分治时结果的位置发生变化,需要在傅里叶变换时进行二进制位置互换: 每经

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

Hdu 1402 (FFT)

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