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 ...
随机推荐
- group_concat函数导致的主从同步异常
group_concat函数导致的主从同步异常的问题总结 今天在处理一个group_concat函数导致的主从异常的问题,排查过程比较简单,不过第一次遇到这个问题记录一下排查的思路,后面如果再遇到其他 ...
- 记录-JQuery日历插件My97DatePicker日期范围限制
对于日期控件,有时会有不能选择今天以前的日期这种需求..... My97DatePicker是一个非常优秀的日历插件,不仅支持多种调用模式,还支持日期范围限制. 常规的调用比较简单,如下所示: 1 & ...
- Java程序发送邮件
之前上网有看到过别人总结的使用java程序发送邮件,于是自己下来练习,把自己学习的一些心得总结出来. 首先我们这里需要采用两个jar包: 需要的朋友可以自行上网去CSDN类似的网站上面找 顺便把自己测 ...
- ASP跳出FOR循环
由于ASP不能使用GOTO语句,我在FOR循环中加入一个FOR循环,若需要跳出,即退出最里面那个FOR循环. DEMO: <%dim aa = 0for i = 1 to 10 for j ...
- 我的Android进阶之旅------>WindowManager.LayoutParams介绍
本文转载于: http://hubingforever.blog.163.com/blog/static/171040579201175111031938/ 本文参照自: http://develop ...
- 我的Android进阶之旅------>HTTP Header 详解
HTTP(HyperTextTransferProtocol)即超文本传输协议,目前网页传输的的通用协议.HTTP协议采用了请求/响应模型,浏览器或其他客户端发出请求,服务器给与响应.就整个网络资源传 ...
- linux c编程:线程互斥一
当多个线程共享相同的内存的时候,需要确保每个线程都看到一致的数据视图.如果每个线程使用的变量都是其他线程不会读取和修改的.那么就不存在一致性问题.同样,如果变量是只读的,多个线程也不会有一致性的问题. ...
- C#DataSet/DataAdapter
DataReader必须持续连接,所以在调用方法SqlDataReader作为返回类型时候,必须在方法外关闭流,很不方便. DataAdapter用于对数据源检索数据并填充到DataSet中的表.Da ...
- 财经世界(5)国际货币基金组织,世界银行,国际清算银行(BIS)与美联储
(1)国际货币基金组织 *国际货币基金组织(International Monetary Fund,简称:IMF)是根据1944年7月在布雷顿森林会议签订的<国际货币基金协定>,于1945 ...
- 《程序员代码面试指南》第一章 栈和队列 最大值减去最小值小于或等于num的数量
题目 给定整数数组arr和整数num,共返回多少的数组满足如下情况 max(arr[i...j]) - min(arr[i...j]) <= num max(arr[i...j])表示数组arr ...