FFT/NTT [51Nod 1028] 大数乘法 V2
题目链接:51Nod 传送门
没压位,效率会低一点
1.FFT

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAXN = 1<<18;
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)
{
change(arr, len);
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 wA1 = w * arr[k + i/2];
complex A0 = arr[k];
arr[k] = A0 + wA1;
arr[k + i/2] = A0 - wA1;
w = w * wn;
}
}
}
if(flg == -1)
for(int i = 0; i < len; ++i)
arr[i].r /= len;
}
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(10);
}
2.NTT

似乎并没有比FFT快
空间到是少了不少
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int MAXN = 1<<18;
const int mod = 998244353, G = 3;
char str1[MAXN], str2[MAXN];
int a1[MAXN], a2[MAXN];
inline void change(int arr[], const int& len)
{
for(register int i = 1, j = len/2; i < len-1; ++i)
{
if(i < j) swap(arr[i], arr[j]);
int k = len/2;
while(j >= k) j -= k, k>>=1;
j += k; //-.-写成了减样例半天没调过
}
}
inline int qmul(int a, int b)
{
int ret = 1;
while(b)
{
if(b & 1) ret = (LL)ret * a % mod;
a = (LL)a * a % mod; b>>=1;
}
return ret;
}
inline void NTT(int arr[], const int& len, const int& flg)
{
change(arr, len);
for(int i = 2, w, wn; i <= len; i<<=1)
{
if(~flg) wn = qmul(G, (mod-1)/i);
else wn = qmul(G, mod-1 - (mod-1)/i);
for(int j = 0; j < len; j += i)
{
w = 1;
for(int k = j; k < j + i/2; ++k)
{
int A0 = arr[k];
int wA1 = (LL)w * arr[k + i/2] % mod;
arr[k] = (A0 + wA1) % mod;
arr[k + i/2] = ((A0 - wA1) % mod + mod) % mod; //注意爆负
w = (LL)w * wn % mod;
}
}
}
if(flg == -1)
{
int inv = qmul(len, mod-2);
for(int i = 0; i < len; ++i)
arr[i] = (LL)arr[i] * inv % mod;
}
}
int main ()
{
scanf("%s%s", str1, str2);
int len1 = strlen(str1);
int len2 = strlen(str2);
int ml = len1 + len2, len = 1;
while(len < ml) len<<=1;
for(int i = 0; i < len1; ++i) a1[i] = str1[i] - '0';
for(int i = len1; i < len; ++i) a1[i] = 0;
for(int i = 0; i < len2; ++i) a2[i] = str2[i] - '0';
for(int i = len2; i < len; ++i) a2[i] = 0;
NTT(a1, len, 1), NTT(a2, len, 1);
for(int i = 0; i < len; ++i) a2[i] = (LL)a1[i] * a2[i] % mod;
NTT(a2, len, -1);
for(int i = len1+len2-2; i; --i)
a2[i-1] += a2[i] / 10, a2[i] %= 10;
int i = 0;
while(i < len1+len2-1 && !a2[i]) ++i;
if(i == len1+len2-1) puts("0");
else while(i < len1+len2-1) printf("%d", a2[i++]);
putchar(10);
}
FFT/NTT [51Nod 1028] 大数乘法 V2的更多相关文章
- 51Nod 1028 大数乘法 V2
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1028 分析: FFT/NTT板子题... 代码: NTT板子: #inc ...
- 51nod 1028 大数乘法 V2 【FFT模板题】
题目链接 模板题.. #include<bits/stdc++.h> using namespace std; typedef int LL; typedef double db; nam ...
- 1028 大数乘法 V2(FFT or py)
1028 大数乘法 V2 基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 给出2个大整数A,B,计算A*B的结果. Input 第1行:大数A 第2行:大数B ...
- 51 Nod 1028 大数乘法 V2【Java大数乱搞】
1028 大数乘法 V2 基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 给出2个大整数A,B,计算A*B的结果. Input 第1行:大数A 第2行:大数B (A ...
- 51nod 1027大数乘法
题目链接:51nod 1027大数乘法 直接模板了. #include<cstdio> #include<cstring> using namespace std; ; ; ; ...
- ACM学习历程—51NOD1028 大数乘法V2(FFT)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1028 题目大意就是求两个大数的乘法. 但是用普通的大数乘法,这 ...
- 51NOD 1027 大数乘法
1027 大数乘法 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 给出2个大整数A,B,计算A*B的结果. Input 第1行:大数A 第2行:大数B (A,B ...
- FFT版题 [51 Nod 1028] 大数乘法
题目链接:51 Nod 传送门 数的长度为10510^5105,乘起来后最大长度为2×1052\times10^52×105 由于FFT需要把长度开到222的次幂,所以不能只开到2×1052\time ...
- 【51NOD1028】大数乘法 V2
╰( ̄▽ ̄)╭ 给出2个大整数A,B,计算A*B的结果. (A,B的长度 <= 100000,A,B >= 0) (⊙ ▽ ⊙) 把大整数A看做一个次数界为lenA的多项式A(x),其中x ...
随机推荐
- JVM性能调优的6大步骤,及关键调优参数详解
JVM性能调优方法和步骤1.监控GC的状态2.生成堆的dump文件3.分析dump文件4.分析结果,判断是否需要优化5.调整GC类型和内存分配6.不断分析和调整JVM调优参数参考 对JVM内存的系统级 ...
- JSON学习(二)
首先,定义一个实体类Person: import com.fasterxml.jackson.annotation.JsonFormat; import java.util.Date; public ...
- Django-04-路由系统
1. 概述 URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL模式以及 2. path转换器 在django2.0 以上的版本中,默认使用的是path转换器,我们首先以此 ...
- Python2 和 Python3区别
字符串类型不同 py3: str bytes py2: unicode str 默认解释器编码 输入输出 int int long 除法 range和xrang 模块和包 字典 keys py2:列表 ...
- 山峰和山谷 Ridges and Valleys
题目描述 思路 一开始看这道题目,也不是很会,谁会把统计之类的问题和bfs联系在一起,没有开始的状态,没有结束的状态,题目中连一个最短之类的词也没有出现. 然后统计嘛,题目中说了方格高度都相同,就把周 ...
- Kubernetes之动态Jenkins slave
一.前提 本次实践前,需已完成以下过程: 1.搭建好一个Kubernetes集群(本实践为单节点集群),网上参考较多,不赘述. 2.选取kubernetes集群外的一台服务器安装 NFS服务端,并在集 ...
- sqoop与hbase导入导出数据
环境:sqoop1.4.6+hadoop2.6+hbase1.1+mysql5.7 说明: 1.文中的导入导出的表结构借鉴了网上的某篇博客 2.mysql导入hbase可以直接通过sqoop进行 3. ...
- ArcGIS Engine中C#开发不能引用ESRI.ArcGIS.AxControls问题
问题:ArcGIS Engine中C#开发不能引用ESRI.ArcGIS.AxControls问题 解决方法:将这里的特定版本改成“False”即可.
- C# vb .net图像合成-合成文字
在.net中,如何简单快捷地实现图像合成呢,比如合成文字,合成艺术字,多张图片叠加合成等等?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码 ...
- 易百教程人工智能python修正-人工智能无监督学习(聚类)
无监督机器学习算法没有任何监督者提供任何指导. 这就是为什么它们与真正的人工智能紧密结合的原因. 在无人监督的学习中,没有正确的答案,也没有监督者指导. 算法需要发现用于学习的有趣数据模式. 什么是聚 ...