HDU1402:A * B Problem Plus(FFT与大数乘法)
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 .
Output
For each case, output A * B in one line.
Sample Input Sample Output
把每一位看成ai*10^i,然后就是两个多项式相乘。利用FFT,把复杂度降到O(nlogn)。
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
const double pi=acos(-1.0);
struct complex
{
double r,i;
complex(){};
complex(double rr,double ii):r(rr),i(ii){}
complex friend operator +(complex a,complex b){ return complex(a.r+b.r,a.i+b.i); }
complex friend operator -(complex a,complex b) { return complex(a.r-b.r,a.i-b.i);}
complex friend operator *(complex a,complex b) { return complex(a.r*b.r-a.i*b.i,a.r*b.i+a.i*b.r);}
}tmp[maxn];
struct DFT{
complex a[maxn];
void fft(int sz,int bg,int step,int opt){
if(sz==) return; int m=sz>>;
fft(m,bg,step<<,opt); fft(m,bg+step,step<<,opt);
complex w=complex(,),t=complex(cos(2.0*pi/sz),sin(2.0*pi*opt/sz));
for(int k=;k<m;k++)
{
int pos=*step*k;
tmp[k]=a[pos+bg]+w*a[pos+bg+step];
tmp[k+m]=a[pos+bg]-w*a[pos+bg+step];
w=w*t;
}
for(int i=;i!=sz;i++) a[i*step+bg]=tmp[i];
}
}A,B;
char c[maxn]; int ans[maxn+];
int main()
{
while(~scanf("%s",c)){
int L1=strlen(c),L2,len=;
for(int i=;i<L1;i++) A.a[i].r=c[L1-i-]-'',A.a[i].i=;
scanf("%s",c); L2=strlen(c);
for(int i=;i<L2;i++) B.a[i].r=c[L2-i-]-'',B.a[i].i=; while(len<L1+L2+) len<<=; for(int i=L1;i<=len;i++) A.a[i].r=A.a[i].i=;
for(int i=L2;i<=len;i++) B.a[i].r=B.a[i].i=; A.fft(len,,,); B.fft(len,,,);
for(int i=;i<len;i++) A.a[i]=A.a[i]*B.a[i];
A.fft(len,,,-); int head=;
for(int i=;i<len;i++) ans[i]=(int)(A.a[i].r/len+0.5);
for(int i=;i<len;i++){
ans[i+]+=ans[i]/; ans[i]%=;
if(ans[i]>) head=i;
} for(int i=head;i>=;i--) printf("%d",ans[i]);
printf("\n");
}
return ;
}
HDU1402:A * B Problem Plus(FFT与大数乘法)的更多相关文章
- HDU-1402 A * B Problem Plus FFT(快速傅立叶变化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1402 一般的的大数乘法都是直接模拟乘法演算过程,复杂度O(n^2),对于这题来说会超时.乘法的过程基本 ...
- HDU1402 A * B Problem Plus FFT
分析:网上别家的代码都分析的很好,我只是给我自己贴个代码,我是kuangbin的搬运工 一点想法:其实FFT就是快速求卷积罢了,当小数据的时候我们完全可以用母函数来做,比如那种硬币问题 FFT只是用来 ...
- [hdu1402]A * B Problem Plus(FFT模板题)
解题关键:快速傅里叶变换fft练习. 关于结果多项式长度的确定,首先将短多项式扩展为长多项式,然后扩展为两倍. #include<cstdio> #include<cstring&g ...
- 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 ...
- HDU - 1402 A * B Problem Plus (FFT实现高精度乘法)
题意:计算A*B,A,B均为长度小于50000的整数. 这是FFT在大整数相乘中的一个应用,我本来想用NTT做的,但NTT由于取模很可能取炸,所以base必须设得很小,而且效率也比不上FFT. A和B ...
- FFT之大数乘法
#include <iostream> #include <stdio.h> #include <cmath> #include <algorithm> ...
- [hdu1402]大数乘法(FFT模板)
题意:大数乘法 思路:FFT模板 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ...
- A * B Problem Plus(fft)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1402 hdu_1402:A * B Problem Plus Time Limit: 2000/100 ...
- hdu 1402 A * B Problem Plus FFT
/* hdu 1402 A * B Problem Plus FFT 这是我的第二道FFT的题 第一题是完全照着别人的代码敲出来的,也不明白是什么意思 这个代码是在前一题的基础上改的 做完这个题,我才 ...
随机推荐
- 【ITOO 4】WCF中,分布式事务处理
导读:事务可以确保除非事务性单元内的所有操作都成功完成,否则不会永久更新面向数据的资源.通过将一组相关操作组合为一个要么全部成功要么全部失败的单元,可以简化错误恢复并使应用程序更加可靠.在项目中,就有 ...
- Codeforces603E - Pastoral Oddities
Portal Description 初始时有\(n(n\leq10^5)\)个孤立的点,依次向图中加入\(m(m\leq3\times10^5)\)条带权无向边.使得图中每个点的度数均为奇数的边集是 ...
- 【2018 Multi-University Training Contest 3】
01:https://www.cnblogs.com/myx12345/p/9420198.html 02: 03: 04:https://www.cnblogs.com/myx12345/p/940 ...
- input文本框的value属性在页面中不随输入的数据而变化
今天,在做试验遇到这么一个需求: 一个input文本框,输入值后将标签传到后台,在后台解析标签,发现value仍然是初值,不是我们改变后的值. 例如: <input name="&qu ...
- 由八数码问题引入。对BFS有更深考虑
12号到今天共研究八数码问题poj1077,首先用的是普通BFS,遇到很多问题,开始用一个二级指针作为结构成员,知道了二级指针与二维数值名的不同!http://write.blog.csdn.net/ ...
- Spring Cloud(9):Config配置中心
Config配置中心作用简单来讲:统一配置,方便管理 开源配置中心: 1.百度Disconf 2.阿里Diamand 3.Spring Cloud Config 搭建Config-Server 快速上 ...
- 登录页面练习servlet
登录练习: 1创建登录页面 创建servlet进行登录页面请求 2点击登录完成操作 浏览器发送请求到服务器(用户信息+其他数据 )3服务器调用对应的servlet进行处理 设置响应编码格式 获取请求信 ...
- Java加载配置文件类
/** * 对应配置文件类, */ package com.up72.parkSys.ThirdParty; import java.io.IOException;import java.io.In ...
- Java日志框架-Logback手册中文版以及官方配置文档教程
Logback手册中文版:(链接: https://pan.baidu.com/s/1bpMyasR 密码: 6u5c),虽然版本有点旧,但是大体意思差不多,先用中文版了解个大概,然后一切最新的配置以 ...
- 【APUE】文件I/O
Linux的内核将所有外部设备都可以看做一个文件来操作.那么我们对与外部设备的操作都可以看做对文件进行操作.我们对一个文件的读写,都通过调用内核提供的系统调用:内核给我们返回一个file descri ...