hdu1402(大数a*b&fft模板)
题目链接: 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模板)的更多相关文章
- [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 ...
- 再写FFT模板
没什么好说的,今天又考了FFT(虽然不用FFT也能过)但是确实有忘了怎么写FFT了,于是乎只有重新写一遍FFT模板练一下手了.第一部分普通FFT,第二部分数论FFT,记一下模数2^23*7*17+1 ...
- HDU 1402 A * B Problem Plus (FFT模板题)
FFT模板题,求A*B. 用次FFT模板需要注意的是,N应为2的幂次,不然二进制平摊反转置换会出现死循环. 取出结果值时注意精度,要加上eps才能A. #include <cstdio> ...
- FFT模板(多项式乘法)
FFT模板(多项式乘法) 标签: FFT 扯淡 一晚上都用来捣鼓这个东西了...... 这里贴一位神犇的博客,我认为讲的比较清楚了.(刚好适合我这种复数都没学的) http://blog.csdn.n ...
- P1919 【模板】A*B Problem升级版 /// FFT模板
题目大意: 给定l,输入两个位数为l的数A B 输出两者的乘积 FFT讲解 这个讲解蛮好的 就是讲解里面贴的模板是错误的 struct cpx { double x,y; cpx(double _x= ...
- fft模板 HDU 1402
// fft模板 HDU 1402 #include <iostream> #include <cstdio> #include <cstdlib> #includ ...
- 51nod 1028 大数乘法 V2 【FFT模板题】
题目链接 模板题.. #include<bits/stdc++.h> using namespace std; typedef int LL; typedef double db; nam ...
- 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 ...
- [hdu1402]A * B Problem Plus(FFT模板题)
解题关键:快速傅里叶变换fft练习. 关于结果多项式长度的确定,首先将短多项式扩展为长多项式,然后扩展为两倍. #include<cstdio> #include<cstring&g ...
随机推荐
- POJ 3728 The merchant(LCA+DP)
The merchant Time Limit : 6000/3000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total ...
- win10/server2019 系统安装 详解
https://www.microsoft.com/zh-cn/software-download/windows10 https://go.microsoft.com/fwlink/?LinkId= ...
- appium_python_android测试环境搭建
第一步 安装appium •Appium是由.NET 开发的,所以,它会依赖 .NET framework相关组件,所以先安装.net framework 4.5,备注: Appium最低支持.ne ...
- Tiny4412 Linux 内核配置流程
1.配置交叉编译器 默认情况下,内核构建的是与宿主机相同的体系架构镜像.如果要交叉编译,需要设置两个变量ARCH和CORSS_COMPILE. ①ARCH:指明目标体系架构,如x86.arm.mips ...
- C语言学习笔记--指针和数组的关系
1.数组的本质 (1)数组是一段连续的内存空间 (2)数组的空间大小:sizeof(array_type)*array_size; (3)数组名可看做指向数组第一个元素的常量指针 (4)数组声明时编译 ...
- vs中ffmpeg release版本崩溃问题(转)
vs2010 win7 下开发视频服务器,用到ffmpeg,debug版本运行正常,切换到release时,出现"0x00905a4d 处未处理的异常: 0xC0000005: 读取位置 0 ...
- Android WebView 捕捉点击的URL中的信息
项目要求,在WebView中点击搜索关键字,加载其他Web页面时,需要在一个文本输入框中,实时显示关键字 事实上,这种点击,是WebView内的,并没有跳出这个WebView,Activity也没有经 ...
- 关于play!的attachments.path配置、以及关于Form表单上传请求的认识
相关链接 form表单提交multipart/form-data的请求分析:http://blog.csdn.net/five3/article/details/7181521.http://blog ...
- JAVA基础知识总结13(同步)
好处:解决了线程安全问题. 弊端:相对降低性能,因为判断锁需要消耗资源,还容易产生了死锁. 定义同步是有前提的: 1,必须要有两个或者两个以上的线程,才需要同步. 2,多个线程必须保证使用的是同一个锁 ...
- springBoot数据库jpa+对接mybatis
1 spring Data jpa hibernate引领数据访问技术,使用orm对象关系映射来进行数据库访问,通过模型和数据库进行映射,通过操作对象实现对数据库操作,把数据库相关操作从代码中独立出 ...