ACM学习历程—51NOD1028 大数乘法V2(FFT)
题目链接: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)的更多相关文章
- 1028 大数乘法 V2(FFT or py)
1028 大数乘法 V2 基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 给出2个大整数A,B,计算A*B的结果. Input 第1行:大数A 第2行:大数B ...
- 51 Nod 1028 大数乘法 V2【Java大数乱搞】
1028 大数乘法 V2 基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 给出2个大整数A,B,计算A*B的结果. Input 第1行:大数A 第2行:大数B (A ...
- ACM学习历程—HDU5667 Sequence(数论 && 矩阵乘法 && 快速幂)
http://acm.hdu.edu.cn/showproblem.php?pid=5667 这题的关键是处理指数,因为最后结果是a^t这种的,主要是如何计算t. 发现t是一个递推式,t(n) = c ...
- ACM学习历程—HDU5585 Numbers(数论 || 大数)(BestCoder Round #64 (div.2) 1001)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5585 题目大意就是求大数是否能被2,3,5整除. 我直接上了Java大数,不过可以对末尾来判断2和5, ...
- ACM学习历程—HDU1041 Computer Transformation(递推 && 大数)
Description A sequence consisting of one digit, the number 1 is initially written into a computer. A ...
- ACM学习历程—HDU1023 Train Problem II(递推 && 大数)
Description As we all know the Train Problem I, the boss of the Ignatius Train Station want to know ...
- ACM学习历程—HDU 3092 Least common multiple(数论 && 动态规划 && 大数)
Description Partychen like to do mathematical problems. One day, when he was doing on a least common ...
- 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 ...
- FFT/NTT [51Nod 1028] 大数乘法 V2
题目链接:51Nod 传送门 没压位,效率会低一点 1.FFT #include <cstdio> #include <cstring> #include <algori ...
随机推荐
- 圆环自带动画进度条ColorfulRingProgressView
这是项目中遇到了,我也是借鉴大神的, 下载地址:https://github.com/oooohuhu/ColorfulRingProgressView 我把它导入了github中了,里面有详细的使用 ...
- Unity框架入门
介绍Unity框架之前,先要说几个概念DIP依赖倒置原则.IOC控制反转.DI依赖注入 DIP是设计原则之一,定义:上层不应该依赖于底层,两者都依赖于抽象: 抽象不依赖于细节,细节应该依赖于抽象. 像 ...
- Hadoop伪分布式环境快速搭建
Hadoop分支 Apache Cloudera Hortonworks 本文是采用Cloudera分支的hadoop. 下载cdh-5.3.6 版本 下载地址:http://archive.clou ...
- 详解Maven项目利用java service wrapper将Java程序生成Windows服务
在项目的开发中,有时候需要将Java应用程序打包成Windows服务,我们就直接可以通过windows的服务来启动和关闭java程序了. 本博文将通过有两种方法实现该功能,手动创建法和Maven自动打 ...
- Feign-独立使用-实战
目录 写在前面 1.1.1. 短连接API的接口准备 1.1.2. 申明远程接口的本地代理 1.1.3. 远程API的本地调用 写在最后 疯狂创客圈 亿级流量 高并发IM 学习实战 疯狂创客圈 Jav ...
- 【python】-- 内置函数、软件目录开发规范(代码编码风格)
内置函数 一.内置函数表格 二.内置函数演示 1.abs(x) 功能:取数的绝对值 >>> abs(-1) #取-1的绝对值 1 ########################## ...
- ShowModal 代码分析
下面为Delphi中,方法TCustomForm.ShowModal的代码,通过分析以下代码,可以了解ShowModal到底是怎么一回事! 1 2 3 4 5 6 7 8 9 10 11 12 13 ...
- PAT 天梯赛 【】 L3-015. 球队“食物链” 【BFS+剪枝】
题目链接 https://www.patest.cn/contests/gplt/L3-015 思路 用一个 数组标记 胜负 每次输入一行字符串 然后遍历 如果 碰到 W 那么 vis[i][j] = ...
- Java多线程系列 JUC线程池01 线程池框架
转载 http://www.cnblogs.com/skywang12345/p/3509903.html 为什么引入Executor线程池框架 new Thread()的缺点 1. 每次new T ...
- <Linux内核源码>文件系统VFS内核4.0.4版本基本概念源码
题外话:Linux内核从2.x和3.x到现在最新的4.x变化非常大,最直观的表现就是很多书上的内核代码已经无法直接继续使用,所以看看新的源码是非常有意义的! (下文中的内核源码都来自于 kernel ...