题目链接:51 Nod 传送门

数的长度为10510^5105,乘起来后最大长度为2×1052\times10^52×105

由于FFT需要把长度开到222的次幂,所以不能只开到2×1052\times10^52×105,会TLE(卡了好久,还以为是要压位)

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAXN = 400005;//!!!
const double Pi = acos(-1.0); struct complex
{
double r, i;
complex(double _r=0, double _i=0):r(_r), i(_i){}
complex operator +(const complex &t)const
{
return complex(r + t.r, i + t.i);
}
complex operator -(const complex &t)const
{
return complex(r - t.r, i - t.i);
}
complex operator *(const complex &t)const
{
return complex(r*t.r - i*t.i, r*t.i + t.r*i);
}
}a1[MAXN], a2[MAXN], w, wn;
char s1[MAXN], s2[MAXN];
int n, len1, len2, ans[MAXN]; inline void change(complex arr[], int len)
{
for(int i = 1, j = len/2, k; i < len-1; ++i)
{
if(i < j) swap(arr[i], arr[j]);
for(k = len/2; k <= j; j-=k, k>>=1);
j += k;
}
} inline void fft(complex arr[], int len, int flg)
{
for(int i = 2; i <= len; i<<=1)
{
wn = complex(cos(Pi*flg*2/i), sin(Pi*flg*2/i));
for(int j = 0; j < len; j+=i)
{
w = complex(1, 0);
for(int k = j; k < j + i/2; ++k)
{
complex u = w * arr[k + i/2];
complex v = arr[k];
arr[k] = v + u;
arr[k + i/2] = v - u;
w = w * wn;
}
}
}
if(flg == -1)
for(int i = 0; i < len; ++i)
arr[i].r /= len;
} inline void FFT(complex arr[], int len, int flg)
{
change(arr, len);
fft(arr, len, flg);
} int main()
{
scanf("%s", s1), len1 = strlen(s1);
scanf("%s", s2), len2 = strlen(s2);
int len = len1 + len2;
for(n = 1; n < len; n<<=1); for(int i = 0; i < len1; ++i) a1[i] = complex((double)(s1[i] - '0'), 0);
for(int i = len1; i < n; ++i) a1[i] = complex();
FFT(a1, n, 1); for(int i = 0; i < len2; ++i) a2[i] = complex((double)(s2[i] - '0'), 0);
for(int i = len2; i < n; ++i) a2[i] = complex();
FFT(a2, n, 1); for(int i = 0; i < n; ++i) a2[i] = a1[i] * a2[i];
FFT(a2, n, -1); for(int i = 0; i < len1+len2-1; ++i)
ans[i] = (int)(a2[i].r + 0.5); for(int i = len1+len2-2; i; --i)
{
ans[i-1] += ans[i]/10;
ans[i] %= 10;
}
int i;
for(i = 0; !ans[i] && i < len1+len2-1; ++i);
if(i == len1+len2-1) putchar('0');
else while(i < len1+len2-1) printf("%d",ans[i++]); //此处不能用putchar
//因为我ans[0]没有向前进位,所以要用%d输出
putchar(10);
}

FFT版题 [51 Nod 1028] 大数乘法的更多相关文章

  1. 51 Nod 1028 大数乘法 V2【Java大数乱搞】

    1028 大数乘法 V2 基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 给出2个大整数A,B,计算A*B的结果. Input 第1行:大数A 第2行:大数B (A ...

  2. 51 Nod 1027 大数乘法【Java大数乱搞】

    1027 大数乘法 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 给出2个大整数A,B,计算A*B的结果. Input 第1行:大数A 第2行:大数B (A,B的长度  ...

  3. 1028 大数乘法 V2(FFT or py)

    1028 大数乘法 V2 基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 给出2个大整数A,B,计算A*B的结果.   Input 第1行:大数A 第2行:大数B ...

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

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

  5. FFT/NTT [51Nod 1028] 大数乘法 V2

    题目链接:51Nod 传送门 没压位,效率会低一点 1.FFT #include <cstdio> #include <cstring> #include <algori ...

  6. 51Nod 1028 大数乘法 V2

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1028 分析: FFT/NTT板子题... 代码: NTT板子: #inc ...

  7. 51 Nod 1005 大数加法【Java大数乱搞,python大数乱搞】

    1005 大数加法 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 给出2个大整数A,B,计算A+B的结果. Input 第1行:大数A 第2行:大数B (A,B的长度  ...

  8. 51 Nod 1029 大数除法【Java大数乱搞】

    1029 大数除法 基准时间限制:4 秒 空间限制:131072 KB 分值: 160 难度:6级算法题 给出2个大整数A,B,计算A / B和A Mod B的结果.   Input 第1行:大数A ...

  9. 蓝桥杯 第三届C/C++预赛真题(6) 大数乘法(数学题)

    对于32位字长的机器,大约超过20亿,用int类型就无法表示了,我们可以选择int64类型,但无论怎样扩展,固定的整数类型总是有表达的极限!如果对超级大整数进行精确运算呢?一个简单的办法是:仅仅使用现 ...

随机推荐

  1. [转帖](区块链补习班)ERC20很多人都听过,但ERC是什么你真的了解吗?

    (区块链补习班)ERC20很多人都听过,但ERC是什么你真的了解吗? http://baijiahao.baidu.com/s?id=1600948969290990883&wfr=spide ...

  2. git 学习笔记---操作标签

    如果标签打错了,也可以删除: $ git tag -d v0.1 Deleted tag 'v0.1' (was f15b0dd) 因为创建的标签都只存储在本地,不会自动推送到远程.所以,打错的标签可 ...

  3. 创建包含CRUD操作的Web API接口4:实现Put方法

    本节教程是前三节的延续,在前面我们创建了Web API和必要的基础设施,也实现了Get和Post方法.接下来,我们将在Web API中实现Put方法. RESTful架构中,HTTP PUT方法用于在 ...

  4. ubuntu中安装python3和pip

    python3: 在ubuntu的包中,python的二代和三代版本的命名:二代:python,三代:python3 安装python3: sudo apt install python3 同理:pi ...

  5. 用C#做一个 拉流播放器

    做拉流播放器第一个想到就是,.,..FFmpeg没错 我也是用强大的他它来做的.但是我用的不是  cmd 调用 而是用的强大的FFmpeg.AutoGen FFmpeg.AutoGen 这个是C# 一 ...

  6. ArcGIS Engine开发鹰眼图的功能(基础篇)

    鹰眼是用于调节全视域范围内主地图显示范围情况的副地图.它体现了地图整体与详细局部的关系. 用户可以通过鼠标单击或者画框等动作实现鹰眼与主地图的交互情况. 鹰眼功能的原理是通过主地图窗口的地图控件和鹰眼 ...

  7. Java异常体系结构学习笔记

    异常类的继承层次       1.Throwable是所有异常类的父类,他也继承自Object.所以Throwable是一个类,而不是接口. 2.Error这个分支的异常是由于Java虚拟机内部错误导 ...

  8. 2019 斗鱼java面试笔试题 (含面试题解析)

      本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.斗鱼等公司offer,岗位是Java后端开发,因为发展原因最终选择去了斗鱼,入职一年时间了,之前面试了很多家公 ...

  9. django.db.utils.InternalError: (1060, "Duplicate column name 'user_id'")迁移报错解决方法

    django.db.utils.InternalError: (1060, "Duplicate column name 'user_id'")迁移报错解决方法 django.db ...

  10. js中cssText批量修改元素样式

    平常编写代码,更改一个元素样式的时候,自己都是用 obj.style.width = "200px"; obj.style.position = "absolute&qu ...