HDU#1402. A×B


A * B Problem Plus

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25361    Accepted Submission(s): 6524

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

Author

DOOM III


FFT入门题

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=2e5+12;
const double pi=acos(-1);
struct complex
{
    double x,i;
    complex(){}
    complex(double a,double b) {x=a;i=b;}
}A[maxn],B[maxn];
complex operator + (complex a,complex b) {return complex(a.x+b.x,a.i+b.i);}
complex operator - (complex a,complex b) {return complex(a.x-b.x,a.i-b.i);}
complex operator * (complex a,complex b) {return complex(a.x*b.x-a.i*b.i,a.x*b.i+a.i*b.x);}
int n;
int rev[maxn];
void FFT(complex *a,int t)
{
    for(int i=0;i<n;i++) if(rev[i]>i) swap(a[i],a[rev[i]]);//交换
    for(int i=1;i<n;i<<=1)//当前块的长度
    {
        complex wn(cos(2*pi/(i<<1)),t*sin(2*pi/(i<<1)));//wn
        for(int j=0;j<n;j+=(i<<1))//处于哪一块
        {
            complex w(1,0),t0,t1;
            for(int k=0;k<i;k++) //块上的位置
            {
                t0=a[j+k];t1=w*a[j+k+i];//蝴蝶交换
                a[j+k]=t0+t1;
                a[j+k+i]=t0-t1;
                w=w*wn;
            }
        }
    }
}
int sum[maxn];
int main()
{
    freopen("a.in","r",stdin);
    char s1[maxn],s2[maxn];
    while(scanf("%s%s",s1,s2)!=EOF)
    {
        int len1=strlen(s1),len2=strlen(s2);
        int len=1;
        while(len<len1*2||len<len2*2) len<<=1;//两数之积不超过最大数次数的2倍
        for(int i=0;i<len1;i++) A[i]=complex(s1[len1-i-1]-‘0‘,0);
        for(int i=len1;i<len;i++) A[i]=complex(0,0);
        for(int i=0;i<len2;i++) B[i]=complex(s2[len2-i-1]-‘0‘,0);
        for(int i=len2;i<len;i++) B[i]=complex(0,0);
        n=len;
        rev[0]=0;
        int L=0;//
        for(int i=1;i<len;i<<=1) L++;
         for(int i=0;i<n;i++) rev[i]=(rev[i>>1]>>1)|((i&1)<<(L-1));//交换对象 

        FFT(A,1);FFT(B,1);//DFT
        for(int i=0;i<len;i++) A[i]=A[i]*B[i];//点积相乘
        FFT(A,-1);//逆DFT
        for(int i=0;i<len;i++) sum[i]=(int)(A[i].x/len+0.5);//减少误差 逆DFT需要多除以个len
        for(int i=0;i<len;i++)
        {
            sum[i+1]+=sum[i]/10;
            sum[i]%=10;
        }
        len=len1+len2-1;
        while(sum[len]<=0&&len) len--;
        for(int i=len;i>=0;i--) printf("%d",sum[i]);
        printf("\n");
    }
    return 0;
}


HDU#1402. A×B

原文地址:https://www.cnblogs.com/Heey/p/9040903.html

时间: 2024-11-04 19:33:31

HDU#1402. A×B的相关文章

fft模板 HDU 1402

1 // fft模板 HDU 1402 2 3 #include <iostream> 4 #include <cstdio> 5 #include <cstdlib> 6 #include <algorithm> 7 #include <vector> 8 #include <math.h> 9 #include <memory.h> 10 #include <bits/stdc++.h> 11 using

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

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 大数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裸题

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 5431 AB String

通道 题意:给出只有AB组成的字符串S,求第k个不在S中出现的串T. 思路: 代码: #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <set> #include <map> #include <iostream> #include <bitset> #define LL long long

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

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