题目链接: 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模板)的更多相关文章

  1. [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 ...

  2. 再写FFT模板

    没什么好说的,今天又考了FFT(虽然不用FFT也能过)但是确实有忘了怎么写FFT了,于是乎只有重新写一遍FFT模板练一下手了.第一部分普通FFT,第二部分数论FFT,记一下模数2^23*7*17+1 ...

  3. HDU 1402 A * B Problem Plus (FFT模板题)

    FFT模板题,求A*B. 用次FFT模板需要注意的是,N应为2的幂次,不然二进制平摊反转置换会出现死循环. 取出结果值时注意精度,要加上eps才能A. #include <cstdio> ...

  4. FFT模板(多项式乘法)

    FFT模板(多项式乘法) 标签: FFT 扯淡 一晚上都用来捣鼓这个东西了...... 这里贴一位神犇的博客,我认为讲的比较清楚了.(刚好适合我这种复数都没学的) http://blog.csdn.n ...

  5. P1919 【模板】A*B Problem升级版 /// FFT模板

    题目大意: 给定l,输入两个位数为l的数A B 输出两者的乘积 FFT讲解 这个讲解蛮好的 就是讲解里面贴的模板是错误的 struct cpx { double x,y; cpx(double _x= ...

  6. fft模板 HDU 1402

    // fft模板 HDU 1402 #include <iostream> #include <cstdio> #include <cstdlib> #includ ...

  7. 51nod 1028 大数乘法 V2 【FFT模板题】

    题目链接 模板题.. #include<bits/stdc++.h> using namespace std; typedef int LL; typedef double db; nam ...

  8. 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 ...

  9. [hdu1402]A * B Problem Plus(FFT模板题)

    解题关键:快速傅里叶变换fft练习. 关于结果多项式长度的确定,首先将短多项式扩展为长多项式,然后扩展为两倍. #include<cstdio> #include<cstring&g ...

随机推荐

  1. 一文读懂非关系型数据库(NoSQL)

    为了更好的理解非关系型数据库,我又深入的度娘了下 原文地址:https://baijiahao.baidu.com/po/feed/share?wfr=spider&for=pc&co ...

  2. 网络监控之一:ss(Socket Statistics)

    ss是Socket Statistics的缩写. 顾名思义,ss命令可以用来获取socket统计信息,它可以显示和netstat类似的内容.但ss的优势在于它能够显示更多更详细的有关TCP和连接状态的 ...

  3. Java 的三个注释

    单行注释 // 这是名为 a 的类 class a{ } 多行注释 /* 这是多行注释 可以注释多行 */ class a{ } 文档注释 /** 这是文档注释 可以注释多行 */ class a{ ...

  4. 2016.8.17服务器端数据库用户导入导出方法 expdp和impdp

    EXP和IMP是客户端工具程序,它们既可以在客户端使用,也可以在服务端使用. EXPDP和IMPDP是服务端的工具程序,他们只能在ORACLE服务端使用,不能在客户端使用. IMP只适用于EXP导出的 ...

  5. Hash函数和消息摘要算法

    一.Hash函数 哈希函数就是能将任意长度的数据映射为固定长度的数据的函数.哈希函数返回的值被叫做哈希值.哈希码.散列,或者直接叫做哈希. 二.消息摘要   将长度不固定的消息(message)作为输 ...

  6. redis的特性

  7. 关于RAW 和 ASSEST文件夹的差异

    以下内容转自:http://www.cnblogs.com/leizhenzi/archive/2011/10/18/2216428.html *res/raw和assets的相同点: 1.两者目录下 ...

  8. android tween动画和Frame动画总结

    tween  动画有四种 //透明度动画 AlphaAnimation aa = (AlphaAnimation) AnimationUtils.loadAnimation(MainActivity. ...

  9. 阿里云、宝塔、wordpress建站

    1 阿里云 购买一个学生机就行啦 2 宝塔 2.1 更改阿里云的镜像 技巧01:先关掉阿里云之前的镜像 技巧02:到镜像市场中寻找宝塔的镜像资源 2.2 配置安全组 宝塔的控制面板需要开通端口 888 ...

  10. Arduino Serial库的使用

    1 Serial.begin() 2 Serial.end() 3 Serial.available() 4 Serial.read() 5 Serial.peek() 6 Serial.flush( ...