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的模板题。我们把数字的每一位看成i*(x^j),当我们把x取成10的时候我们就得到了这个大整数

#include <bits/stdc++.h>

using namespace std;
const int maxn = +;
const double pi = acos(-1.0);
const double PI = acos(-1.0);
#define fft FFT
#define r real
struct Complex
{
double r,i;
Complex(double _r,double _i):r(_r),i(_i){}
Complex(){}
Complex operator +(const Complex &b)
{
return Complex(r+b.r,i+b.i);
}
Complex operator -(const Complex &b)
{
return Complex(r-b.r,i-b.i);
}
Complex operator *(const Complex &b)
{
return Complex(r*b.r-i*b.i,r*b.i+i*b.r);
}
};
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]);
k = len/;
while( j >= k)
{
j -= k;
k /= ;
}
if(j < k)j += k;
}
}
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].r /= len;
}
char numA[maxn],numB[maxn];
Complex a[maxn*],b[maxn*];
int ans[maxn*];
int main()
{
//freopen("de.txt","r",stdin);
while (~scanf("%s",numA)){
int lenA = strlen(numA);
int sa = ;
while ((<<sa)<lenA) sa++;
scanf("%s",numB);
int lenB = strlen(numB);
int sb = ;
while ((<<sb)<lenB) sb++;
int len = (<<(max(sa,sb)+));
for (int i=;i<len;++i){
if (i<lenA) a[i] = Complex(numA[lenA-i-]-'',);
else a[i] = Complex(,);
if (i<lenB) b[i] = Complex(numB[lenB-i-]-'',);
else b[i] = Complex(,);
}
fft(a,len,);
fft(b,len,);//将a b 换成点值表达
for (int i=;i<len;++i)
a[i] = a[i]*b[i];//点值相乘
fft(a,len,-);//DFT逆变换回去
for (int i=;i<len;++i)
ans[i] = (int)(a[i].r+0.5);//误差处理
for (int i=;i<len-;++i){
ans[i+]+=ans[i]/;//处理进位问题
ans[i]%=;
}
bool flag = ;//调整输出格式处理前导零问题
for (int i=len-;i>=;--i){
if (ans[i]) printf("%d",ans[i]),flag=;
else if (flag||i==) printf("");
}
printf("\n");
}
return ;
}

hdu 1402 A * B Problem Plus (FFT模板)的更多相关文章

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

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

  2. hdu 1402 A * B Problem Plus FFT

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

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

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

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

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

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

  6. P1919 【模板】A*B Problem升级版 /// FFT模板

    题目大意: 给定l,输入两个位数为l的数A B 输出两者的乘积 FFT讲解 这个讲解蛮好的 就是讲解里面贴的模板是错误的 struct cpx { double x,y; cpx(double _x= ...

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

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

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

随机推荐

  1. spring-cloud:熔断监控Hystrix Dashboard和Turbine的示例

    1.运行环境 开发工具:intellij idea JDK版本:1.8 项目管理工具:Maven 4.0.0 2.GITHUB地址 https://github.com/nbfujx/springCl ...

  2. kNN(从文本文件中解析数据)

    # 准备数据:从文本文件中解析数据# 在kNN.py中创建名为file2matrix的函数,处理输入格式问题# 该函数的输入为文件名字符串,输出为训练样本矩阵和类标签向量# 将文本记录到转换Numpy ...

  3. C++ string.replace的使用

    //下面是一个检查一个字符串中是否有'.'的函数,该函数将找到的'.'转化为'_'. inline void checkName(string& name) { std::; while (s ...

  4. SSH弱小算法支持问题

    SSH弱小算法支持问题:SSH的配置文件中加密算法没有指定(没有配置加密算法),则会默认支持所有加密算法,包括arcfour,arcfour128,arcfour256等弱加密算法.解决方法:1.修改 ...

  5. Agri-Net —poj1258

    Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44670   Accepted: 18268 Descri ...

  6. 后端技术杂谈3:Lucene基础原理与实践

    本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https://github.com/h2pl/Java-Tutorial 喜欢的话麻烦点下 ...

  7. delphi 在代码中 添加 TO-DO 并且 管理

    TO-DO List是一项非常好用的功能.采用她可以让我们很清楚的了解以前完成了那些任务,还有哪些任务需要做,由谁负责完成,是不是比较紧急的任务等.今天来不及完成的,明天上班就可以很快的找到任务所在的 ...

  8. Bootstrap 学习笔记2 栅格系统 辅助类下拉框

    辅助类和响应式工具: 颜色和字体相同 响应式工具: 图标菜单按钮组件: btn-group 按钮式下拉菜单

  9. Html5 学习笔记 【PC固定布局】 实战2 导航栏搜索区域

    <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8& ...

  10. C语言|博客作业5

    ---恢复内容开始--- 一.本周教学内容&目标 第2章 用C语言编写程序-函数 2.5 生成乘方表与阶乘表.使学生对函数的定义和调用有初步的认识,能模仿编程. 二.本周作业头 问题 答案 这 ...