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

还是没有看懂这道题的代码,(;′⌒`)


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<vector>
#define ll __int64
#define pi acos(-1.0)
using namespace std;
const int MAX = 200005;
//复数结构体
struct complex
{
    double r,i;
    complex(double R=0,double I=0)
    {
        r=R;
        i=I;
    }
    complex operator+(const complex &a)
    {
        return complex(r+a.r,i+a.i);
    }
    complex operator-(const complex &a)
    {
        return complex(r-a.r,i-a.i);
    }
    complex operator*(const complex &a)
    {
        return complex(r*a.r-i*a.i,r*a.i+i*a.r);
    }
};
/*
 *进行FFT和IFFT前的反转变换
 *位置i和i的二进制反转后位置互换,(如001反转后就是100)
 *len必须去2的幂
 */
void change(complex x[],int len)
{
    int i,j,k;
    for(i = 1, j = len/2; i <len-1; i++)
    {
        if (i < j) swap(x[i],x[j]);
        //交换互为小标反转的元素,i<j保证交换一次
        //i做正常的+1,j做反转类型的+1,始终i和j是反转的
        k = len/2;
        while (j >= k)
        {
            j -= k;
            k /= 2;
        }
        if (j < k) j += k;
    }
}
/*
 *做FFT
 *len必须为2^n形式,不足则补0
 *on=1时是DFT,on=-1时是IDFT
 */
void fft (complex x[],int len,int on)
{
    change(x,len);
    for (int i=2; i<=len; i<<=1)
    {
        complex wn(cos(-on*2*pi/i),sin(-on*2*pi/i));
        for (int j=0; j<len; j+=i)
        {
            complex w(1,0);
            for (int k=j; k<j+i/2; k++)
            {
                complex u = x[k];
                complex t = w*x[k+i/2];
                x[k] = u+t;
                x[k+i/2] = u-t;
                w = w*wn;
            }
        }
    }
    if (on == -1)
        for (int i=0; i<len; i++)
            x[i].r /= len;
}
complex x1[MAX],x2[MAX];
char str1[MAX/2],str2[MAX/2];
ll num[MAX],sum[MAX];
int main()
{
    int i,len1,len2,len;
    while(scanf("%s%s",str1,str2)!=EOF)
    {
        len1 = strlen(str1);
        len2 = strlen(str2);
        len = 1;
        while (len < 2*len1 || len < 2*len2) len<<=1;
        for (i=0; i<len1; i++)
            x1[i] = complex(str1[len1-1-i]-'0',0);
        for (i=len1; i<len; i++)
            x1[i] = complex(0,0);
        for (i=0; i<len2; i++)
            x2[i] = complex(str2[len2-1-i]-'0',0);
        for (i=len2; i<len; i++)
            x2[i] = complex(0,0);
        fft(x1,len,1);
        fft(x2,len,1);
        for (i=0; i<len; i++)
            x1[i] = x1[i]*x2[i];
        fft(x1,len,-1);
        for (i=0; i<len; i++)
            sum[i] = (int)(x1[i].r+0.5);
        for (i=0; i<len; i++)
        {
            sum[i+1]+=sum[i]/10;
            sum[i]%=10;
        }
        len = len1+len2-1;
        while (sum[len]<=0 && len>0) len--;
        for (i=len; i>=0; i--)
            printf("%c",(char)sum[i]+'0');
        printf("\n");
    }
    return 0;
}

HDU 1402:A * B Problem Plus的更多相关文章

  1. hdu 1402 A * B Problem Plus FFT

    /* hdu 1402 A * B Problem Plus FFT 这是我的第二道FFT的题 第一题是完全照着别人的代码敲出来的,也不明白是什么意思 这个代码是在前一题的基础上改的 做完这个题,我才 ...

  2. HDU - 1402 A * B Problem Plus FFT裸题

    http://acm.hdu.edu.cn/showproblem.php?pid=1402 题意: 求$a*b$ 但是$a$和$b$的范围可以达到 $1e50000$ 题解: 显然...用字符串模拟 ...

  3. HDU 1402 A * B Problem Plus 快速傅里叶变换 FFT 多项式

    http://acm.hdu.edu.cn/showproblem.php?pid=1402 快速傅里叶变换优化的高精度乘法. https://blog.csdn.net/ggn_2015/artic ...

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

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

  6. hdu 1402 A * B Problem Plus (FFT模板)

    A * B Problem Plus Problem Description Calculate A * B. Input Each line will contain two integers A ...

  7. 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: th ...

  8. HDU 1402 A * B Problem Plus (FFT模板题)

    FFT模板题,求A*B. 用次FFT模板需要注意的是,N应为2的幂次,不然二进制平摊反转置换会出现死循环. 取出结果值时注意精度,要加上eps才能A. #include <cstdio> ...

  9. HDU - 1402 A * B Problem Plus (FFT实现高精度乘法)

    题意:计算A*B,A,B均为长度小于50000的整数. 这是FFT在大整数相乘中的一个应用,我本来想用NTT做的,但NTT由于取模很可能取炸,所以base必须设得很小,而且效率也比不上FFT. A和B ...

随机推荐

  1. django关闭DEBUG后 static静态文件都访问不了

    Django框架仅在开发模式下提供静态文件服务.当我开启DEBUG模式时,Django内置的服务器是提供静态文件的服务的,所以css等文件访问都没有问题,但是关闭DEBUG模式后,Django便不提供 ...

  2. Java基础之创建窗口——使用边界布局管理器(TryBorderLayout)

    控制台程序. 边界布局管理器最多能在容器中放置5个组件.在这种布局管理器中,可以把组件放在容器的任意一个边界上,也可以把组件放在容器的中心.每个位置只能放置一个组件.如果把组件放置在已被占用的边界上, ...

  3. Java基础之一组有用的类——Observable和Observer对象(Horrific)

    控制台程序. Obserable类提供了一个有趣的机制,可以把类对象中发生的改变通知给许多其他类对象. 对于可以观察的对象来说,类定义中需要使用java.util.Observable类.只需要简单地 ...

  4. Browserify

    NodeJS 把 JavaScript 的使用从浏览器端扩展到了服务器端,使得前端开发人员可以用熟悉的语言编写服务器端代码.这一变化使得 NodeJS 很快就流行起来.在 NodeJS 社区中有非常多 ...

  5. js正则表达式进行格式校验

    今天做了个js正则表达式的练习,利用正则表达式进行注册信息格式验证,注册信息界面如下: 格式要求: 1.学号项不能为空,必须为纯数字,不能与数据库中的重复,正则表达式/^\d+$/g: 2.姓名项不能 ...

  6. ajax和servlet交互,表单日历插件,表单验证,form.js

    我的index.jsp <body> <a>点我获取数据</a> <table border=1px> <tr> <td>ID& ...

  7. demo15 AlertDialog

    Dialog dialog = new AlertDialog.Builder(this).setTitle("对话框").setMessage("this is msg ...

  8. em和rem及rem在移动的应用

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <style t ...

  9. c# 获取路径的几种方法

    1.取得控制台应用程序的根目录方法 方法1.Environment.CurrentDirectory 取得或设置当前工作目录的完整限定路径方法2.AppDomain.CurrentDomain.Bas ...

  10. ssh-copy-id帮你建立信任

    一.ssh-keygen -t rsa [nameA@machineA]$ ssh-keygen -t rsa Generating public/private rsa key pair. Ente ...