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 ...
随机推荐
- map 玩家上线
map 玩家上线 else if(gs2ms_add_player == pkt.cmd) { PlayerChannel* pPC = new PlayerChannel(this); //加到地图 ...
- SimpleDateFormat注意点
今天测试了一下SimpleDateFormat把一个字符串转为Date类型 SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-M ...
- 九度OJ 1200:最大的两个数 (最值)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2904 解决:761 题目描述: 输入一个四行五列的矩阵,找出每列最大的两个数. 输入: 输入第一行包括一个整数n(1<=n<= ...
- The Princess and the Pea,摘自iOS应用Snow White and more stories
Once upon a time there was a prince who wanted to marry a real princess.从前,有个王子想和真正的公主结婚. He looked ...
- ggplot2绘图系统
ggplot2包实现了一个在R中基于全面一致的语法创建图形时的系统 .在ggplot2中,图是采用串联起来(+)号函数创建的.详细内容参见<ggplot2:数据分析与图形艺术>,这里只简要 ...
- C# Lambda表达式与Linq
, , , , , , }; //linq写法 var res = from i in arry select i; //lambda写法 var res = arry.Select(i => ...
- DHTMLTree、Dtree和Ztree的学习使用
一.DHTMLTree是树菜单,允许我们快速开发界面优美,基于Ajax的javascript库.她允许在线编辑,拖拽,三种状态(全选.不选.半选),复选框等模式.同时在加载大数据量的时候,仍然可以保持 ...
- Ubuntu安装SSH + Windows上配置Putty
1. Ubuntu安装SSH 命令: # sudo apt-get install openssh-server 2. 启动SSH Server 命令: # sudo /etc/init.d/ssh ...
- Kattis - triangle 【数学】
题意 求第N个迭代三角形 中 所有黑色三角形的周长的整数部分的位数 思路 该三角形的周长是 3^(n + 1)/ 2 ^ (n) 然后 可以用 long double 存下来 再求位数 就可以 AC ...
- iOS 基本数据类型 和 指针 特点
基本数据类型 : 整型int, 字符型char , 浮点型 (float 和 double), 枚举型; -- 构造类型 : 数组类型, 结构体类型, 共用体类型; -- 指针类型 : 最终要的数据类 ...