hdu1402(大数a*b&fft模板)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1402
题意: 给出两个长度1e5以内的大数a, b, 输出 a * b.
思路: fft模板
详情参见: m.blog.csdn.net/f_zyj/article/details/76037583
http://blog.csdn.net/sdj222555/article/details/9786527
https://wenku.baidu.com/view/8bfb0bd476a20029bd642d85.html
可以将 a, b 看成两个多项式, 每个数位为一项, 每一位上的数字即为所在项的系数.所以直接fft即可.
代码:
#include <iostream>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <stdio.h>
using namespace std; const double PI = acos(-1.0); struct Complex{//复数结构体
double x, y;//实部,虚部
Complex(double _x = 0.0, double _y = 0.0){
x = _x;
y = _y;
}
Complex operator -(const Complex &b) const{
return Complex(x - b.x, y - b.y);
}
Complex operator +(const Complex &b) const{
return Complex(x + b.x, y + b.y);
}
Complex operator *(const Complex &b) const{
return Complex(x * b.x - y * b.y, x * b.y + y * b.x);
}
}; //进行FFT和IFFT反转变化
//位置i和(i二进制反转后位置)互换
void change(Complex y[], int len){//len必须为2的幂
for(int i = , j = len / ; i < len - ; i++){
if(i < j) swap(y[i], y[j]); //交换互为下标反转的元素,i<j保证只交换一次
int k = len >> ;
while(j >= k){
j -= k;
k /= ;
}
if(j < k) j += k;
}
} //做FFT,len必须为2的幂,on=1是DFT,on=-1是IDTF
void fft(Complex y[], int len, int on){
change(y, len);//调用反转置换
for(int h = ; h <= len; h <<= ){
Complex wn(cos(-on * * PI / h), sin(-on * * PI / h));
for(int j = ; j < len; j += h){
Complex w(, );//初始化螺旋因子
for(int k = j; k < j + h / ; k++){//配对
Complex u = y[k];
Complex t = w * y[k + h / ];
y[k] = u + t;
y[k + h / ] = u - t;
w = w * wn;//更新螺旋因子
}
}
}
if(on == -){
for(int i = ; i < len; i++){
y[i].x /= len;//IDTF
}
}
} const int MAXN = 2e5 + ;
Complex x1[MAXN], x2[MAXN];
char str1[MAXN >> ], str2[MAXN >> ];
int sum[MAXN]; int main(void){
while(~scanf("%s%s", str1, str2)){
int len1 = strlen(str1);
int len2 = strlen(str2);
int len = ;
while(len < len1 * || len < len2 * ) len <<= ;
//将str1,str2构造成两个多项式
for(int i = ; i < len1; i++){//倒存
x1[i] = Complex(str1[len1 - i - ] - '', );
}
for(int i = len1; i < len; i++){
x1[i] = Complex(, );//不够的补0
}
for(int i = ; i < len2; i++){
x2[i] = Complex(str2[len2 - i - ] - '', );
}
for(int i = len2; i < len; i++){
x2[i] = Complex(, );
}
fft(x1, len, ); //DFT(str1)
fft(x2, len, ); //DFT(str2);
for(int i = ; i < len; i++){
x1[i] = x1[i] * x2[i];//点乘结果存入x1
}
fft(x1, len, -);//IDFT(a*b)
for(int i = ; i < len; i++){
sum[i] = (int)(x1[i].x + 0.5);//四舍五入
}
for(int i = ; i < len; i++){
sum[i + ] += sum[i] / ;
sum[i] %= ;
}
len = len1 + len2 - ;//长度为len1和len2的两个数的乘积长度最大不超过len1+len2+1
while(sum[len] <= && len > ) len--;//去前导0
for(int i = len; i >= ; i--){
printf("%d", sum[i]);
}
puts("");
}
return ;
}
hdu1402(大数a*b&fft模板)的更多相关文章
- [hdu1402]大数乘法(FFT模板)
题意:大数乘法 思路:FFT模板 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ...
- 再写FFT模板
没什么好说的,今天又考了FFT(虽然不用FFT也能过)但是确实有忘了怎么写FFT了,于是乎只有重新写一遍FFT模板练一下手了.第一部分普通FFT,第二部分数论FFT,记一下模数2^23*7*17+1 ...
- HDU 1402 A * B Problem Plus (FFT模板题)
FFT模板题,求A*B. 用次FFT模板需要注意的是,N应为2的幂次,不然二进制平摊反转置换会出现死循环. 取出结果值时注意精度,要加上eps才能A. #include <cstdio> ...
- FFT模板(多项式乘法)
FFT模板(多项式乘法) 标签: FFT 扯淡 一晚上都用来捣鼓这个东西了...... 这里贴一位神犇的博客,我认为讲的比较清楚了.(刚好适合我这种复数都没学的) http://blog.csdn.n ...
- P1919 【模板】A*B Problem升级版 /// FFT模板
题目大意: 给定l,输入两个位数为l的数A B 输出两者的乘积 FFT讲解 这个讲解蛮好的 就是讲解里面贴的模板是错误的 struct cpx { double x,y; cpx(double _x= ...
- fft模板 HDU 1402
// fft模板 HDU 1402 #include <iostream> #include <cstdio> #include <cstdlib> #includ ...
- 51nod 1028 大数乘法 V2 【FFT模板题】
题目链接 模板题.. #include<bits/stdc++.h> using namespace std; typedef int LL; typedef double db; nam ...
- hdu----(1402)A * B Problem Plus(FFT模板)
A * B Problem Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- [hdu1402]A * B Problem Plus(FFT模板题)
解题关键:快速傅里叶变换fft练习. 关于结果多项式长度的确定,首先将短多项式扩展为长多项式,然后扩展为两倍. #include<cstdio> #include<cstring&g ...
随机推荐
- SQL简单嵌套查询与非嵌套查询的比较(MSSQL2005)
某天的工作是修复某个项目的bug,接着就发现,其sql极其混乱,有非常多的left join和in操作,还有嵌套查询(只有一个表的嵌套查询).不知道看到过哪里的资料说,嵌套查询速度慢,于是我把全部嵌套 ...
- ORACLE增加用户
create user 账号 identified by "密码"; grant connect to 账号; grant resource to 账号; --把dba 权限给in ...
- 在U盘分区安装Kali并引导live CD 教程以及常见的注意事项
Kali Linux作为强大的全能渗透系统,把它制成Live CD基本算是必备技能了,但是官方提供的文档虽然简单,但是整个U盘都会被占用,确实是有点可惜,结合网上提供的一些思路加上自己的经验,向大家讲 ...
- js原型及原型链
一. 普通对象与函数对象 JavaScript 中,万物皆对象!但对象也是有区别的.分为普通对象和函数对象,Object ,Function 是JS自带的函数对象.下面举例说明 function f ...
- bluebird的安装配置
安装 下载bluebird 3.5.0(开发) 意味着在开发中使用的未分类源文件.警告和长堆栈跟踪被启用,这会影响性能. <script src="//cdn.jsdelivr.net ...
- ubuntu16配置Mask-RCNN
一.安装Anaconda3 1.下载 下载地址:https://www.continuum.io/downloads 2.安装 在文件目录下执行:bash Anaconda3-4.2.0-Linux- ...
- 基于IFC的施工过程模拟程序(4D BIM)
- 线程池的原理以及实现线程池的类ExecutorService中方法的使用
1.线程池:线程池就是就像一个容器,而这个容器就是用来存放线程的,且有固定的容量. 如果没有线程池,当需要一个线程来执行任务时就需要创建一个线程,我们设创建线程的时间为t1,执行线程的时间为t2,销毁 ...
- JDBC方式从数据库中查询数据并显示
1.创建数据库表myuser DROP TABLE IF EXISTS `myuser`; CREATE TABLE `myuser` ( `) NOT NULL COMMENT '姓名', `id` ...
- ListBox 光标如何定位在最后一行 显示
richTextBox_show.SelectionStart = richTextBox_show.Text.Length - 1; richTextBox_show.Focus();