(FFT) A * B Problem Plus
题目链接:https://cn.vjudge.net/contest/280041#problem/F
题目大意:给你两个数,求这俩数相乘的结果。(长度最长5000)
具体思路:硬算肯定是不行的,比如说1024*1234 ,我们可以将1024转换成 (4*10^0 + 2*10^1 +0*10^2+1*10^3),然后1234转换成(4*10^0+3*10^1+2*10^2+1*10^3),然后我们就可以转换成多项式相乘来保证计算精度了。
AC代码:
#include<iostream>
#include<cmath>
#include<stdio.h>
#include<cstring>
#include<algorithm>
using namespace std;
# define ll long long
const int mod = 1e9+;
const double PI = acos(-1.0);
struct complex
{
double r,i;
complex(double _r = ,double _i = )
{
r = _r;
i = _i;
}
complex operator +(const complex &b)
{
return complex(r+b.r,i+b.i);
}
complex operator -(const complex &b)
{
return complex(r-b.r,i-b.i);
}
complex operator *(const complex &b)
{
return complex(r*b.r-i*b.i,r*b.i+i*b.r);
}
};
void change(complex y[],int len)
{
int i,j,k;
for(i = , j = len/; i < len-; i++)
{
if(i < j)
swap(y[i],y[j]);
k = len/;
while( j >= k)
{
j -= k;
k /= ;
}
if(j < k)
j += k;
}
}
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].r /= len;
}
const int maxn = 2e5+;//开数组的时候注意,长度的话至少是开两倍的,因为是两个数组相加的!!。
complex x1[maxn],x2[maxn];
char str1[maxn],str2[maxn];
int sum[maxn];
int main()
{
while(scanf("%s %s",str1,str2)==)
{
int len1=strlen(str1);
int len2=strlen(str2);
int len=;
while(len<len1*||len<len2*)
len<<=;
for(int i=; i<len1; i++)
{
x1[i]=complex(str1[len1--i]-'',);
}
for(int i=len1; i<len; i++)
{
x1[i]=complex(,);
}
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,);
fft(x2,len,);
for(int i=; i<len; i++)
{
x1[i]=x1[i]*x2[i];
}
fft(x1,len,-);
for(int i=; i<len; i++)
{
sum[i]=(int)(x1[i].r+0.5);
}
for(int i=; i<len; i++)
{
sum[i+]+=sum[i]/;
sum[i]%=;
}
len=len1+len2-;
while(sum[len]<=&&len>)
len--;
for(int i=len; i>=; i--)
{
printf("%c",sum[i]+'');
}
printf("\n");
}
return ;
}
(FFT) A * B Problem Plus的更多相关文章
- 【CF954I】Yet Another String Matching Problem(FFT)
[CF954I]Yet Another String Matching Problem(FFT) 题面 给定两个字符串\(S,T\) 求\(S\)所有长度为\(|T|\)的子串与\(T\)的距离 两个 ...
- A * B Problem Plus HDU - 1402 (FFT)
A * B Problem Plus HDU - 1402 (FFT) Calculate A * B. InputEach line will contain two integers A and ...
- 【数学】快速傅里叶变换(FFT)
快速傅里叶变换(FFT) FFT 是之前学的,现在过了比较久的时间,终于打算在回顾的时候系统地整理一篇笔记,有写错的部分请指出来啊 qwq. 卷积 卷积.旋积或褶积(英语:Convolution)是通 ...
- 快速傅里叶(FFT)的快速深度思考
关于按时间抽取快速傅里叶(FFT)的快速理论深度思考 对于FFT基本理论参考维基百科或百度百科. 首先谈谈FFT的快速何来?大家都知道FFT是对DFT的改进变换而来,那么它究竟怎样改进,它改进的思想在 ...
- 【BZOJ3527】力(FFT)
[BZOJ3527]力(FFT) 题面 Description 给出n个数qi,给出Fj的定义如下: \[Fj=\sum_{i<j}\frac{q_i q_j}{(i-j)^2 }-\sum_{ ...
- 【BZOJ4827】【HNOI2017】礼物(FFT)
[BZOJ4827][HNOI2017]礼物(FFT) 题面 Description 我的室友最近喜欢上了一个可爱的小女生.马上就要到她的生日了,他决定买一对情侣手 环,一个留给自己,一 个送给她.每 ...
- FFT/NTT总结+洛谷P3803 【模板】多项式乘法(FFT)(FFT/NTT)
前言 众所周知,这两个东西都是用来算多项式乘法的. 对于这种常人思维难以理解的东西,就少些理解,多背板子吧! 因此只总结一下思路和代码,什么概念和推式子就靠巨佬们吧 推荐自为风月马前卒巨佬的概念和定理 ...
- 【BZOJ4503】两个串(FFT)
[BZOJ4503]两个串(FFT) 题面 给定串\(S\),以及带通配符的串\(T\),询问\(T\)在\(S\)中出现了几次.并且输出对应的位置. \(|S|,|T|<=10^5\),字符集 ...
- 【BZOJ4259】残缺的字符串(FFT)
[BZOJ4259]残缺的字符串(FFT) 题面 给定两个字符串\(|S|,|T|\),两个字符串中都带有通配符. 回答\(T\)在\(S\)中出现的次数. \(|T|,|S|<=300000\ ...
随机推荐
- 互评Final版本——可以低头,但没必要——取件帮
基于NABCD评论作品,及改进建议 1.根据(不限于)NABCD评论作品的选题; (1)N(Need,需求) 当今大部分在校学生由于各种原因懒于亲自去取快递,而总麻烦身边人也不是长久之计.而有些同学需 ...
- 20135202闫佳歆--week6 进程的描述与创建--学习笔记
此为个人学习笔记存档! week 6 进程的描述与创建 一.进程的描述 1.进程控制块task_struct 以下内容来自视频课件,存档在此. 为了管理进程,内核必须对每个进程进行清晰的描述,进程描述 ...
- 《Linux内核分析》第二周学习小结 操作系统是如何工作的?
郝智宇 无转载 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.函数调用堆栈: 1.计算机是 ...
- Ubuntu安装eclipse,并创建桌面快捷方式
系统:Ubuntu 16.04 JDK版本:1.8.0_121 Ubuntu下安装JDK配置环境变量可见我的这篇文章 http://www.cnblogs.com/AloneZ/p/Ubuntu1 ...
- 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- [代码]--ORA-01745: 无效的主机/绑定变量名 ORA-00917: 缺失的逗号 oracle日期格式错误
今天在oracle中执行插入语句的时候报了一个奇怪的错误,在程序中报的错误是ORA-01745: 无效的主机/绑定变量名,网上一查说是缺失逗号,在查询分析器执行的时候报缺失的逗号,仔细看了一下也没有缺 ...
- Java重写toString和泛型的使用
一.在一个类中重写toString方法 public class Person { private String name; private int age; public Person() { } ...
- BZOJ2547 CTSC2002玩具兵(最短路径+二分答案+最大流)
先不考虑只有一个显得有些特殊的天兵. 可以发现超能力的作用实质上是使兵更换职业.每一个兵到达某个位置最少需要更换职业的次数是彼此独立的,因为如果需要某两人互换职业可以使他们各自以当前职业到达需要到的地 ...
- Tour HDU - 3488(最大权值匹配)
Tour In the kingdom of Henryy, there are N (2 <= N <= 200) cities, with M (M <= 30000) one- ...
- WEB下载显示下载名称乱码--java
将文件名编码转换为ISO8859-1即可,如下 downloadFileName = new String(fileName.getBytes("gbk"), "ISO8 ...