hdu 1402 A * B Problem Plus 快速傅里叶变换

本文介绍了一种使用快速傅立叶变换(FFT)解决大数乘法问题的方法,避免了传统乘法的高时间复杂度。通过将大整数转换为多项式系数,并利用FFT进行高效计算,最终实现大数相乘。

A * B Problem Plus

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


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
 

题目:

求A*B的的结果,长度为5000,如果直接模拟乘法,需要5000*5000的复杂度,会超时。

解析:


一个数anan-1................a0是由n+1个数字构成的

可以看出an*10^n + an-1*10^n-1,......,+a0*1把令x = 10

则  变为f(x) = an*x^n+........+a0*x^0

两个数相乘就可以看成系数不同的f(x)相乘。

变成 ----------  A(x)*B(x)       ------------- 然后ax^n * bx^m = a*b*x^(n+m)可以看成这两个位置上的数字相乘。得到a*b,是没有进位的。所以最后的结果要进行进位处理


代码如下:fft是模板题,想要了解详情看算法导论第七部分,30章。但是需要一点线性代数,复数,数值分析的知识。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
struct Complex{
    double real;
    double imag;
    Complex (){}
    Complex(double the){
        real = cos(the);
        imag = sin(the);
    }
    Complex(double a,double b){
        real = a;
        imag = b;
    }
};
Complex operator + (Complex a,Complex b){
    return Complex(a.real+b.real,a.imag+b.imag);
}
Complex operator - (Complex a,Complex b){
    return Complex(a.real-b.real,a.imag-b.imag);
}
Complex operator * (Complex a,Complex b){
    return Complex(a.real*b.real-a.imag*b.imag,a.imag*b.real+a.real*b.imag);
}

#define maxn 140000
double pi2 = 2*acos(-1.0);
void fft(Complex* A,int len, int ref){
    //A[rev[k]] = ak 系数向量转置
    int bitn = log2(len);
    int i,j,k;
    for(i = 0;i < len; i++){
        k = 0;
        for( j = 0;j < bitn; j++){
            k = (k<<1);
            if(i&(1<<j))
                k |= 1;
        }
        if(k > i)
            swap(A[i],A[k]);
    }
    //fft计算得到点值
    Complex wm,w,t,u;
    for(int m = 2,f=1; m <= len; m<<=1,f<<=1){
        wm = Complex(ref*pi2/m);
        for( k = 0;k < len; k += m){
            w = Complex(1.0,0);
            for( j = k; j < k+f; j++){
                t = w*A[j+f];
                u = A[j];
                A[j] = u+t;
                A[j+f] = u-t;
                w = w*wm;
            }
        }
    }
    if(ref == -1){
        for( i = 0;i < len; i++){
            A[i].real = A[i].real/len;
        }
    }
}
char word1[maxn];
char word2[maxn];
Complex a1[maxn];
Complex a2[maxn];
int result[maxn];
int main(){
    while(gets(word1)){
        gets(word2);
        int len1 = strlen(word1);
        int len2 = strlen(word2);
        int len3 = 1;
//计算的长度必须为2的n次幂
        while(len3 < len1+len2)
            len3 <<= 1;
        //计算第一个多项式在个点的值
        for(int i = len1; i < len3; i++)
            a1[i] = Complex(0,0);
        for(int i = 0;i < len1; i++)
            a1[i] = Complex(word1[len1-i-1]-'0',0);
        fft(a1,len3,1);
        //计算第二个多项式在个点的值
        for(int i = len2; i < len3; i++)
            a2[i] = Complex(0,0);
        for(int i = 0;i < len2; i++)
            a2[i] = Complex(word2[len2-i-1]-'0',0);
        fft(a2,len3,1);
        //计算两个多项式在各个点的乘积
        for(int i = 0;i < len3; i++)
            a1[i] = a1[i]*a2[i];
        //逆向fft求多项式的系数
        fft(a1,len3,-1);
        //每个数的实部就是多项式的系数,由于精度问题需要+0.5
        int flag = 0;
        for(int i = 0;i < len3; i++){
            result[i] = int(a1[i].real+0.5);
        }
        //十进制数每位都是0-9的数字,需要进位
        for(int i = 0;i < len3; i++){
            result[i+1] += result[i]/10;
            result[i] %= 10;
        }
        for(flag = len3-1; flag > 0 && result[flag]==0;flag--);
        for(;flag>=0;flag--){
            putchar(result[flag]+'0');
        }
        printf("\n");
    }
    return 0;
}










HDU 2034 是一道经典的 A-B Problem 题目,通常涉及简单的数学运算或者字符串处理逻辑。以下是对此类问题的分析以及可能的解决方法。 ### HDU 2034 的题目概述 该题目要求计算两个数之间的差值 \(A - B\) 并输出结果。需要注意的是,输入数据可能存在多种情况,因此程序需要能够适应不同的边界条件和特殊情况[^1]。 #### 输入描述 - 多组测试数据。 - 每组测试数据包含两行,分别表示整数 \(A\) 和 \(B\)。 #### 输出描述 对于每组测试数据,输出一行表示 \(A - B\) 的结果。 --- ### 解决方案 此类问题的核心在于正确读取多组输入并执行减法操作。以下是实现此功能的一种常见方式: ```python while True: try: a = int(input()) b = int(input()) print(a - b) except EOFError: break ``` 上述代码片段通过循环不断接收输入直到遇到文件结束符 (EOF),适用于批量处理多组测试数据的情况。 --- ### 特殊考虑事项 尽管基本思路简单明了,在实际编码过程中仍需注意以下几点: 1. **大数值支持**:如果题目中的 \(A\) 或 \(B\) 可能非常大,则应选用可以容纳高精度的数据类型来存储这些变量。 2. **负数处理**:当 \(B>A\) 导致结果为负时,确保程序不会因符号错误而失效。 3. **异常捕获**:为了防止运行期间由于非法字符或其他意外状况引发崩溃,建议加入必要的错误检测机制。 --- ### 示例解释 假设给定如下样例输入: ``` 5 3 7 2 ``` 按照以上算法流程依次完成各步操作后得到的结果应当分别为 `2` 和 `5`。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GDRetop

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值