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类型,但无论怎样扩展,固定的整数类型总是有表达的极限!如果对超级大整数进行精确运算呢?一个简单的办法是:仅仅使用现 ...
随机推荐
- [Docker] Windows 宿主环境下,共享或上传文件到容器的方法
需求如题. 解决方案1 - 挂载目录(适用于创建新的容器) 格式-v 容器目录 或 -v 本地目录:容器目录 范例Linux宿主环境下:使用镜像 nginx:latest,以后台模式启动一个容器,将容 ...
- [Docker] - 不同容器之间相互访问的实现方式(例如:Client 访问 DB)
部署了两个独立的容器: Container #1 - Web ClientContainer #2 - SQL Server 不同容器间如何互访? 无法从 Container #1 访问到 Conta ...
- nginx+upsync+consul 构建动态nginx配置系统
参考: http://www.php230.com/weixin1456193048.html [upsync模块说明.性能评测] https://www.jianshu.com/p/76352ef ...
- C语言 hello
#include <stdio.h> int main() { /* 我的第一个 C 程序 */ printf("Hello, World! \n"); ; } 实例解 ...
- python优先级问题
- ElasticSearch 429 Too Many Requests circuit_breaking_exception
错误提示 { "statusCode": 429, "error": "Too Many Requests", "message& ...
- Ambari深入学习(I)-系统架构
Ambari是hadoop分布式集群配置管理工具,是由hortonworks主导的开源项目.它已经成为apache基金会的孵化器项目,已经成为hadoop运维系统中的得力助手,引起了业界和学术界的关注 ...
- jQuery中的几个案例:隔行变色、复选框全选和全不选
1 表格隔行变色 1 技术分析: 1 )基本过滤选择器: odd: even: 2 )jq添加和移除样式: addClass(); removeClass(); 2 代码实现 <script s ...
- idea使用git进行项目管理
第一部分: 安装 1. 下载地址: https://git-scm.com/download/win; 如果速度慢, 使用 迅雷下载; 2. 点击安装, 然后下一步, 直到下面这个页面: 建议: 按 ...
- 练习bloc , 动画
有点意思, import 'package:flutter/material.dart'; import 'package:rxdart/rxdart.dart'; main()=>runApp ...