FFT版题 [51 Nod 1028] 大数乘法
题目链接: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] 大数乘法的更多相关文章
- 51 Nod 1028 大数乘法 V2【Java大数乱搞】
1028 大数乘法 V2 基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 给出2个大整数A,B,计算A*B的结果. Input 第1行:大数A 第2行:大数B (A ...
- 51 Nod 1027 大数乘法【Java大数乱搞】
1027 大数乘法 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 给出2个大整数A,B,计算A*B的结果. Input 第1行:大数A 第2行:大数B (A,B的长度 ...
- 1028 大数乘法 V2(FFT or py)
1028 大数乘法 V2 基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 给出2个大整数A,B,计算A*B的结果. Input 第1行:大数A 第2行:大数B ...
- 51nod 1028 大数乘法 V2 【FFT模板题】
题目链接 模板题.. #include<bits/stdc++.h> using namespace std; typedef int LL; typedef double db; nam ...
- FFT/NTT [51Nod 1028] 大数乘法 V2
题目链接:51Nod 传送门 没压位,效率会低一点 1.FFT #include <cstdio> #include <cstring> #include <algori ...
- 51Nod 1028 大数乘法 V2
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1028 分析: FFT/NTT板子题... 代码: NTT板子: #inc ...
- 51 Nod 1005 大数加法【Java大数乱搞,python大数乱搞】
1005 大数加法 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 给出2个大整数A,B,计算A+B的结果. Input 第1行:大数A 第2行:大数B (A,B的长度 ...
- 51 Nod 1029 大数除法【Java大数乱搞】
1029 大数除法 基准时间限制:4 秒 空间限制:131072 KB 分值: 160 难度:6级算法题 给出2个大整数A,B,计算A / B和A Mod B的结果. Input 第1行:大数A ...
- 蓝桥杯 第三届C/C++预赛真题(6) 大数乘法(数学题)
对于32位字长的机器,大约超过20亿,用int类型就无法表示了,我们可以选择int64类型,但无论怎样扩展,固定的整数类型总是有表达的极限!如果对超级大整数进行精确运算呢?一个简单的办法是:仅仅使用现 ...
随机推荐
- 解决 niceScroll 自适应DOM 高度变化
利用dataTable展示数据列表时,当选择每页显示数量时,滚动条还是按照页面初始化时显示的,导致无法滚动查看下面的数据, 在stackoverflower 找到一个可用的方法,但不知道为什么仅写 ...
- 【题解】Luogu P5341 [TJOI2019]甲苯先生和大中锋的字符串
原题传送门 实际按照题意模拟就行 我们先求出字符串的sa 因为要在字符串中出现k次,所以我们枚举\(l,r(r-l+1=k)\)看一共有多少种合法的方案 合法方案的长度下界\(lb\)为\(Max(h ...
- 【简解】SP7556 Stock Charts
题目大意 给出一个折线图,有N条线段,你想要把这些线段分成几个集合,使得每个集合中任意两条线段不相交. 求最少集合数. 分析 喵帕斯:以下提及的所有折线均指横坐标在\([1,k]\)里的折线段. 思考 ...
- git 学习笔记 --创建与合并分支
在版本回退里,你已经知道,每次提交,Git都把它们串成一条时间线,这条时间线就是一个分支.截止到目前,只有一条时间线,在Git里,这个分支叫主分支,即master分支.HEAD严格来说不是指向提交,而 ...
- Spring Security的RBAC数据模型嵌入
1.简介 基于角色的权限访问控制(Role-Based Access Control)作为传统访问控制(自主访问,强制访问)的有前景的代替受到广泛的关注.在RBAC中,权限与角色相关联,用户通过成 ...
- 20、Outer Apply 和 Cross Apply
1.場合 select...caseが複雑の時 2.運用方法 SELECT * FROM stu CROSS APPLY ( --like inner join * FROM score WHERE ...
- Spring Boot 优雅的配置拦截器方式
https://my.oschina.net/bianxin/blog/2876640 https://cs.xieyonghui.com/java/55.html 其实spring boot拦截器的 ...
- java之spring之整合ssh
这篇主要讲解spring + struts2 + hibernate : 目录结构如下: t_role t_user 1.新建 web项目 :spring_ssh 2.在 WebRoot/WEB-IN ...
- shell编程必须要掌握的命令-xargs
一,说xargs命令前,说一下什么是shell编程 什么是shell编程呢,说白了就是按一定的规则把各种命令组织起来,完成一定的事情.纯属个人理解,哈哈.不管是交互式的shell,还是非交互的shel ...
- Spring AOP无法拦截Controller的原因
因为Spring的Bean扫描和Spring-MVC的Bean扫描是分开的, 两者的Bean位于两个不同的Application, 而且Spring-MVC的Bean扫描要早于Spring的Bean扫 ...