Code[VS] 3123 高精度练习之超大整数乘法
FFT 做 高精度乘法
#include <bits/stdc++.h> const double pi = acos(-); struct complex
{
double a, b; inline complex(
double _a = ,
double _b = )
{
a = _a;
b = _b;
} inline friend complex operator +
(const complex &a, const complex &b)
{
return complex(a.a + b.a, a.b + b.b);
} inline friend complex operator -
(const complex &a, const complex &b)
{
return complex(a.a - b.a, a.b - b.b);
} inline friend complex operator *
(const complex &a, const complex &b)
{
return complex(a.a*b.a - a.b*b.b, a.a*b.b + a.b*b.a);
} inline friend complex & operator +=
(complex &a, const complex &b)
{
return a = a+b;
} inline friend complex & operator -=
(complex &a, const complex &b)
{
return a = a-b;
} inline friend complex & operator *=
(complex &a, const complex &b)
{
return a = a*b;
}
}; inline complex alpha(double a)
{
return complex(cos(a), sin(a));
} typedef std::vector<complex> vec; vec FFT(const vec &a)
{
int n = a.size(); if (n == )return a; complex w_k(, );
complex w_n = alpha(pi*/n); vec ar[], yr[], y(n); for (int i = ; i < n; ++i)
ar[i & ].push_back(a[i]); for (int i = ; i < ; ++i)
yr[i] = FFT(ar[i]); for (int i = ; i < n/; ++i, w_k *= w_n)
{
y[i] = yr[][i] + w_k * yr[][i];
y[i + n/] = yr[][i] - w_k * yr[][i];
} return y;
} vec IFFT(const vec &a)
{
int n = a.size(); if (n == )return a; complex w_k(, );
complex w_n = alpha(-pi*/n); vec ar[], yr[], y(n); for (int i = ; i < n; ++i)
ar[i & ].push_back(a[i]); for (int i = ; i < ; ++i)
yr[i] = IFFT(ar[i]); for (int i = ; i < n/; ++i, w_k *= w_n)
{
y[i] = yr[][i] + w_k * yr[][i];
y[i + n/] = yr[][i] - w_k * yr[][i];
} return y;
} char s1[]; int len1;
char s2[]; int len2; vec v1, v2, p1, p2, mul, ans; signed main(void)
{
scanf("%s", s1); len1 = strlen(s1);
scanf("%s", s2); len2 = strlen(s2); int len = len1 + len2; while (len != (len&-len))++len; for (int i = len1 - ; ~i; --i)v1.push_back(complex(s1[i] - '', ));
for (int i = len2 - ; ~i; --i)v2.push_back(complex(s2[i] - '', )); while ((int)v1.size() < len)v1.push_back(complex());
while ((int)v2.size() < len)v2.push_back(complex()); p1 = FFT(v1);
p2 = FFT(v2); for (int i = ; i < len; ++i)
mul.push_back(p1[i] * p2[i]); ans = IFFT(mul); std::vector<int> ret; for (int i = ; i < len; ++i)
ret.push_back((int)round(ans[i].a / len)); for (int i = ; i < len; ++i)
if (ret[i] >= )
{
ret[i + ] += ret[i] / ;
ret[i] %= ;
} while (ret.size() != && !ret[ret.size() - ])
ret.pop_back(); for (int i = ret.size() - ; i >= ; --i)
putchar('' + ret[i]);
putchar('\n');
}
@Author: YouSiki
Code[VS] 3123 高精度练习之超大整数乘法的更多相关文章
- codevs 3123 高精度练习之超大整数乘法
fft. #include<iostream> #include<cstdio> #include<cstring> #include<complex> ...
- 3123 高精度练习之超大整数乘法 - Wikioi
题目描述 Description 给出两个正整数A和B,计算A*B的值.保证A和B的位数不超过100000位. 输入描述 Input Description 读入两个用空格隔开的正整数 输出描述 Ou ...
- 【CodeVS 3123】高精度练习之超大整数乘法 &【BZOJ 2197】FFT快速傅立叶
第一次写法法塔,,,感到威力无穷啊 看了一上午算导就当我看懂了?PS:要是机房里能有个清净的看书环境就好了 FFT主要是用了巧妙的复数单位根,复数单位根在复平面上的对称性使得快速傅立叶变换的时间复杂度 ...
- c++ 超大整数除法 高精度除法
c++ 超大整数除法 高精度除法 解题思路 计算a/b,其中a为大整数,b为普通整数,商为c,余数为r. 根据手算除法的规则,上一步的余数记为r,则本次计算的被除数为t=r*10+被除数的本位数值a[ ...
- POJ 1001 解题报告 高精度大整数乘法模版
题目是POJ1001 Exponentiation 虽然是小数的幂 最终还是转化为大整数的乘法 这道题要考虑的边界情况比较多 做这道题的时候,我分析了 网上的两个解题报告,发现都有错误,说明OJ对于 ...
- codevs 3119 高精度练习之大整数开根 (各种高精+压位)
/* codevs 3119 高精度练习之大整数开根 (各种高精+压位) 二分答案 然后高精判重 打了一个多小时..... 最后还超时了...压位就好了 测试点#1.in 结果:AC 内存使用量: 2 ...
- JavaScript超大整数加法
原文:JavaScript超大整数加法 什么是「超大整数」? JavaScript 采用 IEEE754标准 中的浮点数算法来表示数字 Number. 我也没花时间去详细了解 IEEE754标准 ,但 ...
- c++ 超长整数乘法 高精度乘法
c++ 超长整数乘法 高精度乘法 解题思路 参考加法和减法解题思路 乘法不是一位一位的按照手算的方式进行计算,而是用循环用一个数的某一位去乘另外一个数 打卡代码 #include<bits/st ...
- poj2389-Bull Math(大整数乘法)
一,题意: 大整数乘法模板题二,思路: 1,模拟乘法(注意"逢十进一") 2,倒序输出(注意首位0不输出) 三,步骤: 如:555 x 35 = 19425 5 5 5 5 5 ...
随机推荐
- SqlServer--模糊查询-通配符
查询所有姓张的同学Select * from student where left(sName,1)='张' 看上去很美,如果改成查询名字中带亮的学生怎么做?换一种做法 like Select ...
- oracle in VS or效率
select * from test where status in ('01', '02', '03', '111'); select * from test where status = '01' ...
- 在服务器上发布MVC5的应用
如果在Windows server 2012R2上发布MVC应用,步骤稍微简单一些: 安装Win Server2012R2 增加角色IIS和asp.net4.5, IIS里增加asp.net4.5支持 ...
- linux(64位的系统)下nasm进行汇编链接时出现的问题
出现问题: $nasm -f elf hello.asm -o hello.o $ld -s hello.o -o hello ld: i386 architecture of input file ...
- Android Studio failed to open by giving error “Files Locked” 解决方案
windows 7 下的解决方案 导航至 android-studio 安装目录. (默认为C:\Program Files (x86)\Android\android-studio). 往上一层文件 ...
- MongoDb 创建、更新以及删除文档常用命令
mongodb由C++写就,其名字来自humongous这个单词的中间部分,从名字可见其野心所在就是海量数据的处理.关于它的一个最简洁描述为:scalable, high-performance, o ...
- echo命令详解
echo: echo [-neE] [arg ...] echo会将输入的字符串送往标准输出.输出的字符串间以空白字符隔开, 并在最后加上换行号. Options: -n 不在最后自动换行 -e 使用 ...
- 【CSS】使用盒模型
盒子是CSS中的基础概念,我们需要使用它来配置元素的外观以及文档的整体布局. 1. 为元素应用内边距 应用内边距会在元素内容和边距之间添加空白.我们可以为内容盒的每个边界单独设置内边距,或者使用 pa ...
- HTML总结
几个知识点: HTML 指的是超文本标记语言 (Hyper Text Markup Language) HTML框架结构: <!DOCTYPE html> <html> < ...
- Android UI组件----AppWidget控件入门详解
Widget引入 我们可以把Widget理解成放置在桌面上的小组件(挂件),有了Widget,我们可以很方便地直接在桌面上进行各种操作,例如播放音乐. 当我们长按桌面时,可以看到Widget选项,如下 ...