HDU1402 A * B Problem Plus FFT
分析:网上别家的代码都分析的很好,我只是给我自己贴个代码,我是kuangbin的搬运工
一点想法:其实FFT就是快速求卷积罢了,当小数据的时候我们完全可以用母函数来做,比如那种硬币问题
FFT只是用来解决数据规模较大时的办法,可以达到nlogn的效率,大体原理就是运用了n次单位复根的折半引理
具体可以看算法导论
高度仰慕kuangbin大神的模板,实在是不想自己写
对于这个题,我们10^k的系数看成多项式系数,然后求卷积,进位就好了
- #include <stdio.h>
- #include <iostream>
- #include <algorithm>
- #include <string.h>
- #include <vector>
- #include <math.h>
- #include <map>
- using namespace std;
- typedef long long LL;
- const int INF=0x3f3f3f3f;
- const int N=2e5+;
- const double pi = acos(-1.0);
- struct complex{
- double r,i;
- complex(double R=,double I=){
- r=R;i=I;
- }
- complex operator+(const complex &a)const{
- return complex(r+a.r,i+a.i);
- }
- complex operator-(const complex &a)const{
- return complex(r-a.r,i-a.i);
- }
- complex operator*(const complex &a)const{
- return complex(r*a.r-i*a.i,r*a.i+i*a.r);
- }
- };
- void change(complex x[],int len){
- int i,j,k;
- for(i=,j=len/;i<len-;++i){
- if(i<j)swap(x[i],x[j]);
- k=len/;
- while(j>=k){j-=k;k>>=;}
- if(j<k)j+=k;
- }
- }
- void fft(complex x[],int len,int on){
- change(x,len);
- for(int i=;i<=len;i<<=){
- complex wn(cos(-on**pi/i),sin(-on**pi/i));
- for(int j=;j<len;j+=i){
- complex w(,);
- for(int k=j;k<j+i/;++k){
- complex u = x[k];
- complex t = w*x[k+i/];
- x[k]=u+t;
- x[k+i/]=u-t;
- w=w*wn;
- }
- }
- }
- if(on==-)for(int i=;i<len;++i)x[i].r/=len;
- }
- complex x1[N],x2[N];
- char str1[N/],str2[N/];
- int sum[N];
- int main(){
- while(~scanf("%s%s",str1,str2)){
- int len1 = strlen(str1);
- int len2 = strlen(str2),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("%d",sum[i]);
- printf("\n");
- }
- return ;
- }
HDU1402 A * B Problem Plus FFT的更多相关文章
- HDU-1402 A * B Problem Plus FFT(快速傅立叶变化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1402 一般的的大数乘法都是直接模拟乘法演算过程,复杂度O(n^2),对于这题来说会超时.乘法的过程基本 ...
- [hdu1402]A * B Problem Plus(FFT模板题)
解题关键:快速傅里叶变换fft练习. 关于结果多项式长度的确定,首先将短多项式扩展为长多项式,然后扩展为两倍. #include<cstdio> #include<cstring&g ...
- hdu 1402 A * B Problem Plus FFT
/* hdu 1402 A * B Problem Plus FFT 这是我的第二道FFT的题 第一题是完全照着别人的代码敲出来的,也不明白是什么意思 这个代码是在前一题的基础上改的 做完这个题,我才 ...
- 【CF954I】Yet Another String Matching Problem(FFT)
[CF954I]Yet Another String Matching Problem(FFT) 题面 给定两个字符串\(S,T\) 求\(S\)所有长度为\(|T|\)的子串与\(T\)的距离 两个 ...
- FFT/NTT模板 既 HDU1402 A * B Problem Plus
@(学习笔记)[FFT, NTT] Problem Description Calculate A * B. Input Each line will contain two integers A a ...
- 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)
http://acm.hdu.edu.cn/showproblem.php?pid=1402 初学FFT. http://www.cnblogs.com/WABoss/p/FFT_Note.html ...
- 【FFT】hdu1402 A * B Problem Plus
FFT板子. 将大整数看作多项式,它们的乘积即多项式的乘积在x=10处的取值. #include<cstdio> #include<cmath> #include<cst ...
- A * B Problem Plus(fft)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1402 hdu_1402:A * B Problem Plus Time Limit: 2000/100 ...
随机推荐
- hdu2011
http://acm.hdu.edu.cn/showproblem.php?pid=2011 #include<iostream> #include<math.h> #incl ...
- lintcode 中等题:Evaluate Reverse Polish notation逆波兰表达式求值
题目 逆波兰表达式求值 在逆波兰表达法中,其有效的运算符号包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰计数表达. 样例 ["2", "1&q ...
- lintcode:合并区间
题目: 合并区间 给出若干闭合区间,合并所有重叠的部分. 样例 给出的区间列表 => 合并后的区间列表: [ [ [1, 3], [1, 6], [2, 6], => [8, 10], [ ...
- 7、单向一对多的关联关系(1的一方有n的一方的集合属性,n的一方却没有1的一方的引用)
单向一对多的关联关系 具体体现:1的一方有n的一方的集合的引用,n的一方却没有1的一方的引用 举个例子:顾客Customer对订单Order是一个单向一对多的关联关系.Customer一方有对Orde ...
- Failed to allocate memory: 8
Failed to allocate memory: 8This application has requested the Runtime to terminate it in an unusual ...
- Django admin的一些有用定制
Model实例,myapp/models.py: from django.db import models class Blog(models.Model): name = models.CharFi ...
- android的helloworld工程目录学习
android的helloworld工程目录学习 Android工程的主要目录有src.gen.Android X.X.bin.res等文件夹. 1. Src文件夹 Src文件夹包含java源 ...
- NuGet在2015中的使用
NuGet Package Restore https://docs.nuget.org/Consume/Package-Restore 以https://github.com/andburn/hd ...
- error LNK2005 new,delete 等已经在LIBCMT.lib(delete.obj) 中定义 错误修正
http://blog.csdn.net/funnyskyf/article/details/5938597 1>uafxcw.lib(afxmem.obj) : error LNK2005: ...
- Android布局详解之一:FrameLayout
原创文章,如有转载,请注明出处:http://blog.csdn.net/yihui823/article/details/6702273 FrameLayout是最简单的布局了.所有放在布局里的 ...