http://acm.hdu.edu.cn/showproblem.php?pid=1402

fft做O(nlog(n))大数乘法,kuangbin的模板

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
using namespace std;
const double PI = acos(-1.0);
//复数结构体
struct Complex
{
double x,y;//实部和虚部 x+yi
Complex(double _x = 0.0,double _y = 0.0)
{
x = _x;
y = _y;
}
Complex operator -(const Complex &b)const
{
return Complex(x-b.x,y-b.y);
}
Complex operator +(const Complex &b)const
{
return Complex(x+b.x,y+b.y);
}
Complex operator *(const Complex &b)const
{
return Complex(x*b.x-y*b.y,x*b.y+y*b.x);
}
};
/*
* 进行FFT和IFFT前的反转变换。
* 位置i和 (i二进制反转后位置)互换
* len必须去2的幂
*/
void change(Complex y[],int len)
{
int i,j,k;
for(i = , j = len/;i <len-;i++)
{
if(i < j)swap(y[i],y[j]);
//交换互为小标反转的元素,i<j保证交换一次
//i做正常的+1,j左反转类型的+1,始终保持i和j是反转的
k = len/;
while(j >= k)
{
j -= k;
k /= ;
}
if(j < k)j += k;
}
}
/*
* 做FFT
* len必须为2^k形式,
* on==1时是DFT,on==-1时是IDFT
*/
void fft(Complex y[],int len,int on)
{
change(y,len);
for(int h = ; h <= len; h <<= )
{
Complex wn(cos(-on**PI/h),sin(-on**PI/h));
for(int j = ;j < len;j+=h)
{
Complex w(,);
for(int k = j;k < j+h/;k++)
{
Complex u = y[k];
Complex t = w*y[k+h/];
y[k] = u+t;
y[k+h/] = u-t;
w = w*wn;
}
}
}
if(on == -)
for(int i = ;i < len;i++)
y[i].x /= len;
}
const int MAXN = ;
Complex x1[MAXN],x2[MAXN];
char str1[MAXN/],str2[MAXN/];
int sum[MAXN];
int main()
{
while(~scanf("%s%s",str1,str2))
{
int len1 = strlen(str1);
int len2 = strlen(str2);
int len = ;
while(len < len1* || len < len2*)len<<=;
for(int i = ;i < len1;i++)
x1[i] = Complex(str1[len1--i]-'',);
for(int i = len1;i < len;i++)
x1[i] = Complex(,);
for(int i = ;i < len2;i++)
x2[i] = Complex(str2[len2--i]-'',);
for(int i = len2;i < len;i++)
x2[i] = Complex(,);
//求DFT
fft(x1,len,);
fft(x2,len,);
for(int i = ;i < len;i++)
x1[i] = x1[i]*x2[i];
fft(x1,len,-);
for(int i = ;i < len;i++)
sum[i] = (int)(x1[i].x+0.5);
for(int i = ;i < len;i++)
{
sum[i+]+=sum[i]/;
sum[i]%=;
}
len = len1+len2-;
while(sum[len] <= && len > )len--;
for(int i = len;i >= ;i--)
printf("%d",sum[i]);
printf("\n");
}
return ;
}

HDU 1402的更多相关文章

  1. hdu 1402 A * B Problem Plus FFT

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

  2. HDU 1402 FFT 大数乘法

    $A * B$ FFT模板题,找到了一个看起来很清爽的模板 /** @Date : 2017-09-19 22:12:08 * @FileName: HDU 1402 FFT 大整数乘法.cpp * ...

  3. A * B Problem Plus HDU - 1402 (FFT)

    A * B Problem Plus HDU - 1402 (FFT) Calculate A * B.  InputEach line will contain two integers A and ...

  4. fft模板 HDU 1402

    // fft模板 HDU 1402 #include <iostream> #include <cstdio> #include <cstdlib> #includ ...

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

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

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

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

  7. HDU 1402 fft 模板题

    题目就是求一个大数的乘法 这里数字的位数有50000的长度,按平时的乘法方式计算,每一位相乘是要n^2的复杂度的,这肯定不行 我们可以将每一位分解后作为系数,如153 = 1*x^2 + 5*x^1 ...

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

    Problem Description Calculate A * B.   Input Each line will contain two integers A and B. Process to ...

  9. HDU 1402:A * B Problem Plus

    A * B Problem Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

随机推荐

  1. 内置函数——format

    说明: 1. 函数功能将一个数值进行格式化显示. 2. 如果参数format_spec未提供,则和调用str(value)效果相同,转换成字符串格式化. >>> format(3.1 ...

  2. rest-framework框架的基本组件

    快速实例 Quickstart 大致步骤 (1)创建表,数据迁移 (2)创建表序列化类BookSerializer class BookSerializer(serializers.Hyperlink ...

  3. Spring笔记一

    什么是Spring spring (由rod johnson创建的一个开源框架) spring是一个开源框架,spring是于2003 年兴起的一个轻量级的java 开发框架,由rod johnson ...

  4. (转) bicabo Visual Studio 2012自动添加注释(如版权信息等)

    如何使用Visual Studio 2012给程序文件的头部自动添加如下的注释? /********************************************************** ...

  5. FTRL与Online Optimization

    1. 背景介绍 最优化求解问题可能是我们在工作中遇到的最多的一类问题了:从已有的数据中提炼出最适合的模型参数,从而对未知的数据进行预测.当我们面对高维高数据量的场景时,常见的批量处理的方式已经显得力不 ...

  6. Linux内核分析04

    扒开系统调用的三层皮(上) 一,用户态.内核态和中断 用户态.内核态和中断的处理过程 用户态和内核态的区分 内核态:代码可以执行特权指令,访问任意的物理地址,CPU的这种执行级别就对应着~ 相对的用户 ...

  7. 全局变量的使用【C++/Qt】

    转:https://blog.csdn.net/caoshangpa/article/details/51104022 一.使用extern关键字 cglobal.h #ifndef CGLOBAL_ ...

  8. 使用JavaScript下载csv文件

    前端可以使用JavaScript在客户端下载包含页面数据的文件,这里以下载CSV格式文件为例,代码如下: function downloadData(data, filename, type) { v ...

  9. ubuntu16.04解决tensorflow提示未编译使用SSE3、SSE4.1、SSE4.2、AVX、AVX2、FMA的问题【转】

    本文转载自:https://blog.csdn.net/Nicholas_Wong/article/details/70215127 rticle/details/70215127 在我的机器上出现的 ...

  10. ubuntu16.04安装tensorflow官方教程与机器学习资料【学习笔记】

    tensorflow官网有官方的安装教程:https://www.tensorflow.org/install/install_linux google的机器学习官方快速入门教程:https://de ...