题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1028

题目大意就是求两个大数的乘法。

但是用普通的大数乘法,这个长度的大数肯定不行。

大数可以表示点值表示法,然后卷积乘法就能用FFT加速运算了。

这道题是来存模板的。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <string>
#define LL long long using namespace std; //多项式乘法运算
//快速傅里叶变换
//FFT
//用于求两个多项式的卷积,但有精度损失
//时间复杂度nlogn
const int maxN = ;
const double PI = acos(-1.0); struct Complex
{
double r, i; Complex(double rr = 0.0, double ii = 0.0)
{
r = rr;
i = ii;
} Complex operator+(const Complex &x)
{
return Complex(r+x.r, i+x.i);
} Complex operator-(const Complex &x)
{
return Complex(r-x.r, i-x.i);
} Complex operator*(const Complex &x)
{
return Complex(r*x.r-i*x.i, i*x.r+r*x.i);
}
}; //雷德算法--倒位序
//Rader算法
//进行FFT和IFFT前的反转变换。
//位置i和 (i二进制反转后位置)互换
void Rader(Complex y[], int len)
{
int j = len>>;
for(int i = ; i < len-; i++)
{
if (i < j) swap(y[i], y[j]);
int 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)
{
Rader(y, len);
for (int h = ; h <= len; h<<=)//分治后计算长度为h的DFT
{
Complex wn(cos(-on**PI/h), sin(-on**PI/h)); //单位复根e^(2*PI/m)用欧拉公式展开
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;
} //求卷积
void Conv(Complex a[], Complex b[], int ans[], int len)
{
FFT(a, len, );
FFT(b, len, );
for (int i = ; i < len; i++)
a[i] = a[i]*b[i];
FFT(a, len, -);
//精度复原
for(int i = ; i < len; i++)
ans[i] = a[i].r+0.5;
} //进制恢复
//用于大数乘法
void turn(int ans[], int len, int unit)
{
for(int i = ; i < len; i++)
{
ans[i+] += ans[i]/unit;
ans[i] %= unit;
}
} char str1[maxN], str2[maxN];
Complex za[maxN],zb[maxN];
int ans[maxN];
int len; void init(char str1[], char str2[])
{
int len1 = strlen(str1);
int len2 = strlen(str2);
len = ;
while (len < *len1 || len < *len2) len <<= ; int i;
for (i = ; i < len1; i++)
{
za[i].r = str1[len1-i-]-'';
za[i].i = 0.0;
}
while (i < len)
{
za[i].r = za[i].i = 0.0;
i++;
}
for (i = ; i < len2; i++)
{
zb[i].r = str2[len2-i-]-'';
zb[i].i = 0.0;
}
while (i < len)
{
zb[i].r = zb[i].i = 0.0;
i++;
}
} void work()
{
Conv(za, zb, ans, len);
turn(ans, len, );
while (ans[len-] == ) len--;
for (int i = len-; i >= ; i--)
printf("%d", ans[i]);
printf("\n");
} int main()
{
//freopen("test.in", "r", stdin);
while (scanf("%s%s", str1, str2) != EOF)
{
init(str1, str2);
work();
}
return ;
}

ACM学习历程—51NOD1028 大数乘法V2(FFT)的更多相关文章

  1. 1028 大数乘法 V2(FFT or py)

    1028 大数乘法 V2 基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 给出2个大整数A,B,计算A*B的结果.   Input 第1行:大数A 第2行:大数B ...

  2. 51 Nod 1028 大数乘法 V2【Java大数乱搞】

    1028 大数乘法 V2 基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 给出2个大整数A,B,计算A*B的结果. Input 第1行:大数A 第2行:大数B (A ...

  3. ACM学习历程—HDU5667 Sequence(数论 && 矩阵乘法 && 快速幂)

    http://acm.hdu.edu.cn/showproblem.php?pid=5667 这题的关键是处理指数,因为最后结果是a^t这种的,主要是如何计算t. 发现t是一个递推式,t(n) = c ...

  4. ACM学习历程—HDU5585 Numbers(数论 || 大数)(BestCoder Round #64 (div.2) 1001)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5585 题目大意就是求大数是否能被2,3,5整除. 我直接上了Java大数,不过可以对末尾来判断2和5, ...

  5. ACM学习历程—HDU1041 Computer Transformation(递推 && 大数)

    Description A sequence consisting of one digit, the number 1 is initially written into a computer. A ...

  6. ACM学习历程—HDU1023 Train Problem II(递推 && 大数)

    Description As we all know the Train Problem I, the boss of the Ignatius Train Station want to know  ...

  7. ACM学习历程—HDU 3092 Least common multiple(数论 && 动态规划 && 大数)

    Description Partychen like to do mathematical problems. One day, when he was doing on a least common ...

  8. ACM学习历程—NPU1045 2015年陕西省程序设计竞赛网络预赛(热身赛)C题 Graph Theory(递推 && 组合数学 && 大数)

    Description In graph theory, a matching or independent edge set in a graph G = (V , E) is a set of e ...

  9. FFT/NTT [51Nod 1028] 大数乘法 V2

    题目链接:51Nod 传送门 没压位,效率会低一点 1.FFT #include <cstdio> #include <cstring> #include <algori ...

随机推荐

  1. Paint的setPathEffect(PathEffect effect)、以及Path的具体使用,收益多多!

    Paint的setPathEffect(PathEffect effect).以及Path的具体使用,收益多多! 在这首先申明一下介绍只是为了学习使用 内容都来自:http://www.cnblogs ...

  2. centos7.0 安装pdo_mysql扩展

    1:进入到源码包 /usr/local/php-7.1.6/ext/pdo_mysql 执行/usr/local/php-7.1/bin/phpize 如果报如下错误: Cannot find aut ...

  3. share(发包方面)

    share(发包方面) 接收所有map发过来的包,这个是GS线程驱动的 { for (;;) { //... if(!itMap.second->RecvData(Pkt)) break; if ...

  4. 【BZOJ3105】[cqoi2013]新Nim游戏 贪心+线性基

    [BZOJ3105][cqoi2013]新Nim游戏 Description 传统的Nim游戏是这样的:有一些火柴堆,每堆都有若干根火柴(不同堆的火柴数量可以不同).两个游戏者轮流操作,每次可以选一个 ...

  5. rpc接口和http接口的区别和联系

    1 什么是http接口 http接口是基于http协议的post和get接口. 2 什么是rpc接口 rpc接口就相当于调用本地接口一样调用远程服务的接口. 3 常用的rpc框架 thrift 自动代 ...

  6. 【网络与系统安全】关于SSL/TSL协议的分析

    前言 TSL协议的前身是由网景(Netscape)公司于1994年研发的安全套接字(Secure Socket Layer)协议.它建立在TCP协议栈的传输层,用于保护面向连接的TCP通信.实际TSL ...

  7. Python中PIL及Opencv转化

    转载:http://blog.sina.com.cn/s/blog_80ce3a550102w26x.html Convert between Python tuple and list a = (1 ...

  8. python基础4 ----字符编码

    python基础---字符编码 一.了解字符编码 1. 文本编辑器存取文件的原理(nodepad++,pycharm,word) 打开编辑器就打开了启动了一个进程,是在内存中的,所以在编辑器编写的内容 ...

  9. mysql查询当天,本周,本月,上一个月的数据(转)

    今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 昨天 SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ...

  10. 基于WebServices简易网络聊天工具的设计与实现

    基于WebServices简易网络聊天工具的设计与实现 Copyright 朱向洋 Sunsea ALL Right Reserved 一.项目内容 本次课程实现一个类似QQ的网络聊天软件的功能:服务 ...