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 ...
随机推荐
- gcc和g++使用澄清
一:gcc与g++比较 编译c/c++代码的时候,有人用gcc,有人用g++,于是各种说法都来了,譬如c代码用gcc,而 c++代码用g++,或者说编译用gcc,链接用g++,一时也不知哪个说法正确, ...
- CSS3新特性详解
本文讲解CSS3相关实用知识点 CSS3相关实用知识点目录 边框设置 颜色设置 背景设置 渐变使用 超出文本设置 阴影设置 CSS3变换设置 过渡设置 动画设置 多列布局 BoxSizing设置 弹性 ...
- mybatis中in查询
xml配置 : <select id="selectPostIn" resultType="dasyskjcdtblVo"> SELECT SYS_ ...
- c++primer-p100.用迭代器进行二分法搜索
#include <vector> #include <iostream> using namespace std; int main() { vector<int> ...
- 昨天的笔试题, StringBuffer
代码: public static void switchStr(StringBuffer x, StringBuffer y) { x.append(y); System.out.println(& ...
- 巧用cssText属性
给一个HTML元素设置css属性,如 1 2 3 4 var head= document.getElementById("head"); head.style.width = & ...
- ava的打包jar、war、ear包的作用、区别、打包方式
编为大家介绍,基于Java的打包jar.war.ear包的作用与区别详解.需要的朋友参考下以最终客户的角度来看,JAR文件就是一种封装,他们不需要知道jar文件中有多少个.class文件,每个文件中的 ...
- java输入输出--I/O操作基础知识学习
一.java的I/O流 1. 输入流(字节流和字符流,字节流操作的数据单元是8位的字节,字符流操作的是16位的字符)(InputStream 和Reader作为基类) 2.输出流(字节流和字符流,字节 ...
- JQuery利用css()修改样式后 hover失效的解决办法
执行完代码后发现写在样式表中的hover效果失效,改了好几遍差点重新写函数,后来发现很简单,是优先级的问题,css()中的内容覆盖了之前的样式 只需要在样式后写!important即可解决! .fil ...
- oracle行转列练习
----------------------第一题--------------------------- create table STUDENT_SCORE ( name ), subject ), ...