题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1402

题意: 给出两个长度1e5以内的大数a, b, 输出 a * b.

思路: fft模板

详情参见:  m.blog.csdn.net/f_zyj/article/details/76037583

       http://blog.csdn.net/sdj222555/article/details/9786527

      https://wenku.baidu.com/view/8bfb0bd476a20029bd642d85.html

可以将 a, b 看成两个多项式, 每个数位为一项, 每一位上的数字即为所在项的系数.所以直接fft即可.

代码:

 #include <iostream>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <stdio.h>
using namespace std; const double PI = acos(-1.0); struct Complex{//复数结构体
double x, y;//实部,虚部
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二进制反转后位置)互换
void change(Complex y[], int len){//len必须为2的幂
for(int i = , j = len / ; i < len - ; i++){
if(i < j) swap(y[i], y[j]); //交换互为下标反转的元素,i<j保证只交换一次
int k = len >> ;
while(j >= k){
j -= k;
k /= ;
}
if(j < k) j += k;
}
} //做FFT,len必须为2的幂,on=1是DFT,on=-1是IDTF
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;//IDTF
}
}
} const int MAXN = 2e5 + ;
Complex x1[MAXN], x2[MAXN];
char str1[MAXN >> ], str2[MAXN >> ];
int sum[MAXN]; int main(void){
while(~scanf("%s%s", str1, str2)){
int len1 = strlen(str1);
int len2 = strlen(str2);
int len = ;
while(len < len1 * || len < len2 * ) len <<= ;
//将str1,str2构造成两个多项式
for(int i = ; i < len1; i++){//倒存
x1[i] = Complex(str1[len1 - i - ] - '', );
}
for(int i = len1; i < len; i++){
x1[i] = Complex(, );//不够的补0
}
for(int i = ; i < len2; i++){
x2[i] = Complex(str2[len2 - i - ] - '', );
}
for(int i = len2; i < len; i++){
x2[i] = Complex(, );
}
fft(x1, len, ); //DFT(str1)
fft(x2, len, ); //DFT(str2);
for(int i = ; i < len; i++){
x1[i] = x1[i] * x2[i];//点乘结果存入x1
}
fft(x1, len, -);//IDFT(a*b)
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 - ;//长度为len1和len2的两个数的乘积长度最大不超过len1+len2+1
while(sum[len] <= && len > ) len--;//去前导0
for(int i = len; i >= ; i--){
printf("%d", sum[i]);
}
puts("");
}
return ;
}

hdu1402(大数a*b&fft模板)的更多相关文章

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

  2. 再写FFT模板

    没什么好说的,今天又考了FFT(虽然不用FFT也能过)但是确实有忘了怎么写FFT了,于是乎只有重新写一遍FFT模板练一下手了.第一部分普通FFT,第二部分数论FFT,记一下模数2^23*7*17+1 ...

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

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

  4. FFT模板(多项式乘法)

    FFT模板(多项式乘法) 标签: FFT 扯淡 一晚上都用来捣鼓这个东西了...... 这里贴一位神犇的博客,我认为讲的比较清楚了.(刚好适合我这种复数都没学的) http://blog.csdn.n ...

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

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

  6. fft模板 HDU 1402

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

  7. 51nod 1028 大数乘法 V2 【FFT模板题】

    题目链接 模板题.. #include<bits/stdc++.h> using namespace std; typedef int LL; typedef double db; nam ...

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

  9. [hdu1402]A * B Problem Plus(FFT模板题)

    解题关键:快速傅里叶变换fft练习. 关于结果多项式长度的确定,首先将短多项式扩展为长多项式,然后扩展为两倍. #include<cstdio> #include<cstring&g ...

随机推荐

  1. JAX-WS注解

    JAX-WS注解: javax.jws.WebService @WebService应用于类或者接口上面,该类便是一个对外访问WebService,默认情况里面所有的public方法都是可以对外提供访 ...

  2. L2-023. 图着色问题(暴力)

    L2-023. 图着色问题 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 图着色问题是一个著名的NP完全问题.给定无向图 G ...

  3. 如何创建和配置Solaris10 zones (ZT)

    http://thegeekdiary.com/how-to-create-and-configure-solaris-10-zones/ Solaris zones enables a softwa ...

  4. 在云服务器上体验Docker

    1. 添加Docker repository key sh -c "wget -qO- https://get.docker.io/gpg | apt-key add -" 2. ...

  5. spring的配置文件在web.xml中加载的方式

    web.xml加载spring配置文件的方式主要依据该配置文件的名称和存放的位置不同来区别,目前主要有两种方式. 1.如果spring配置文件的名称为applicationContext.xml,并且 ...

  6. Python的安装以及路径的设置(python的下载地址:www.python.org)

    在有的Python版本中在安装时,我们的可以再安装时选择Python路径的自动配备 在选择python的安装程序的时候,我们尽量选择python的2.版本,因为随着Python的更新,Python的数 ...

  7. CentOS 6.3 下编译Nginx(笔记整理)

    1. 安装关联程序 [root@localhost opt]# yum search gcc [root@localhost opt]# yum install gcc-c++ [root@local ...

  8. sys添加调用模块的路径;遍历可以调用模块的路径

    import sys sys.path.append("D:") for i in sys.path: print(i)

  9. Luogu 3530 [POI2012]FES-Festival

    我是真的不会写差分约束啊呜呜呜…… BZOJ 2788被权限了. 首先对于第一个限制$x + 1 = y$,可以转化成$x + 1 \leq y \leq x + 1$, 所以连一条$(y, x, - ...

  10. jquery 仿文本编辑器(智能提示输入文字)

    1.前台代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="InputAu ...