再写FFT模板
没什么好说的,今天又考了FFT(虽然不用FFT也能过)但是确实有忘了怎么写FFT了,于是乎只有重新写一遍FFT模板练一下手了。第一部分普通FFT,第二部分数论FFT,记一下模数2^23*7*17+1
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef double real;
namespace task1
{
const int maxn = ;
const real pi = 3.1415926535897932;
struct Complex
{
real x,y;
Complex(){}
Complex(real x,real y=):x(x),y(y){}
};
Complex operator + (Complex c1,Complex c2)
{
return Complex(c1.x+c2.x,c1.y+c2.y);
}
Complex operator - (Complex c1,Complex c2)
{
return Complex(c1.x-c2.x,c1.y-c2.y);
}
Complex operator * (Complex c1,Complex c2)
{
return Complex(c1.x*c2.x-c1.y*c2.y,c1.y*c2.x+c1.x*c2.y);
}
Complex operator / (Complex c1,real k)
{
return Complex(c1.x/k,c1.y/k);
}
char s1[maxn],s2[maxn];
Complex g1[maxn],g2[maxn],rr[maxn];
int lst[maxn];
void FFT(Complex g[],int l,int p)
{
int x;
for (int i=;i<l;i++)
{
x=;
for (int j=,k=(l>>);j<l;j<<=,k>>=)
if (i&j)x+=k;
if (x<i)
swap(g[x],g[i]);
}
Complex wt,w,tmp;
for (int i=;i<l;i<<=)
{
wt=Complex(cos(pi/i*p),sin(pi/i*p));
for (int j=;j<l;j+=(i<<))
{
Complex w=Complex(,);
for (int k=;k<i;k++)
{
tmp=g[j+k];
g[j+k]=g[j+k]+g[i+j+k]*w;
g[i+j+k]=tmp-g[i+j+k]*w;
w=w*wt;
}
}
}
}
void main()
{
int l1,l2;
scanf("%s",s1);
scanf("%s",s2);
l1=(int)strlen(s1);
l2=(int)strlen(s2);
for (int i=;i<l1;i++)
g1[(l1-i-)/]=g1[(l1-i-)/].x*+s1[i]-'';
for (int i=;i<l2;i++)
g2[(l2-i-)/]=g2[(l2-i-)/].x*+s2[i]-'';
int l=max(l1,l2)/+;
while (l!=(l&(-l)))l-=l&(-l);
l<<=;
FFT(g1,l,);
FFT(g2,l,);
for (int i=;i<l;i++)rr[i]=g1[i]*g2[i];
FFT(rr,l,-);
for (int i=;i<l;i++)
{
rr[i]=rr[i]/l;
lst[i]=(int)(rr[i].x+0.5);
}
for (int i=;i<l;i++)
{
lst[i+]+=lst[i]/;
lst[i]%=;
}
while (l> && !lst[l-])l--;
printf("%d",lst[l-]);
for (int i=l-;i>=;i--)
printf("%04d",lst[i]);
printf("\n");
}
}
namespace task2
{
typedef long long qword;
const int maxn = ;
const qword p = ;
char s1[maxn],s2[maxn];
qword g1[maxn],g2[maxn];;
qword rr[maxn];
qword pow_mod(qword x,qword y)
{
qword ret=;
while (y)
{
if (y&)ret=ret*x%p;
x=x*x%p;
y>>=;
}
return ret;
}
void FFT(qword g[],int l,int f)
{
int x=;
for (int i=;i<l;i++)
{
x=;
for (int j=,k=(l>>);j<l;j<<=,k>>=)
if (i&j)x+=k;
if (i<x)
swap(g[i],g[x]);
}
qword w,wt,tmp;
for (int i=,t=;i<l;i<<=,t++)
{
wt=pow_mod(,**(<<(-t)))%p;
if (f==-)
//wt=-wt;
wt=pow_mod(wt,p-);
for (int j=;j<l;j+=(i<<))
{
w=;
for (int k=;k<i;k++)
{
tmp=g[j+k];
g[j+k]=(g[j+k]+g[i+j+k]*w)%p;
g[i+j+k]=(tmp-g[i+j+k]*w)%p;
w=w*wt%p;
}
}
}
}
void main()
{
int l1,l2;
// for (int i=0;i<5;i++)
// printf("[1/%d*pi]:%d\n",(1<<i),7*17*(1<<(23-i)));
scanf("%s",s1);
scanf("%s",s2);
l1=(int)strlen(s1);
l2=(int)strlen(s2);
for (int i=;i<l1;i++)
g1[i]=s1[l1-i-]-'';
for (int i=;i<l2;i++)
g2[i]=s2[l2-i-]-'';
int l=max(l1,l2);
while (l!=(l&(-l)))l-=l&(-l);
l<<=;
FFT(g1,l,);
FFT(g2,l,);
for (int i=;i<l;i++)rr[i]=g1[i]*g2[i]%p;
FFT(rr,l,-);
qword invl=pow_mod(l,p-);
for (int i=;i<l;i++)rr[i]=(rr[i]*invl%p+p)%p;
for (int i=;i<l;i++)
{
rr[i+]+=rr[i]/;
rr[i]%=;
}
while (l> && ! rr[l-])l--;
for (int i=l-;i>=;i--)
printf("%d",(int)rr[i]);
}
} int main()
{
freopen("input.txt","r",stdin);
task1::main();
task2::main();
}
再写FFT模板的更多相关文章
- 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= ...
- HDU 1402 A * B Problem Plus (FFT模板题)
FFT模板题,求A*B. 用次FFT模板需要注意的是,N应为2的幂次,不然二进制平摊反转置换会出现死循环. 取出结果值时注意精度,要加上eps才能A. #include <cstdio> ...
- switch case :在JDK 7中,又加入了对String类型的支持,从此不用再写If-Else来判断字符串了
switch的case语句可以处理int,short,byte,char类型的值, 因为short,byte,char都会转换成int进行处理,这一点也可以从生成的字节码看出. char a = 'e ...
- 传递的值是this,在js里就不用再写$(this)
<input class="editinput" value="${detail.earlymoneyrmb}" name="earlymone ...
- 以前写SpringMVC的时候,如果需要访问一个页面,必须要写Controller类,然后再写一个方法跳转到页面,感觉好麻烦,其实重写WebMvcConfigurerAdapter中的addViewControllers方法即可达到效果了
以前写SpringMVC的时候,如果需要访问一个页面,必须要写Controller类,然后再写一个方法跳转到页面,感觉好麻烦,其实重写WebMvcConfigurerAdapter中的addViewC ...
- hdu1402(大数a*b&fft模板)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1402 题意: 给出两个长度1e5以内的大数a, b, 输出 a * b. 思路: fft模板 详情参 ...
- 再写一篇tps限流
再写一篇tps限流 各种限流算法的称呼 网上有很多文章介绍限流算法,但是对于这些算法的称呼与描述也是有点难以理解.不管那么多了.我先按我理解的维度梳理一下. 主要维度是:是正向计数还是反向计数.是定点 ...
- 缓存与数据库一致性之二:高并发下的key重建(先淘汰cache再写db)的问题
一.为什么数据会不一致 回顾一下上一篇文章<缓存与数据库一致性之一:缓存更新设计>中对缓存.数据库进行读写操作的流程. 写流程: (1)先淘汰cache (2)再写db 读流程: (1)先 ...
随机推荐
- IE6 BUG margin 两倍
触发条件: 父元素包含子元素 子元素设置了浮动(float) 子元素设置了外边距(margin) 浮动方向和边距方向一致 解决方案: 给该子元素添加 display:inline;
- latch和DFF的区别和联系
1.latch的缺点 ①没有时钟端,不受系统同步时钟的控制,无法实现同步操作:和当前我们尽可能采用时序电路的设计思路不符. ②对输入电平敏感,受布线延迟影响较大,很难保证输出没有毛刺产生: ③latc ...
- SQL语句打印四个方向的9 9 乘法表
declare @i int ,@j int ,@s nvarchar(max) set @i = 1 while @i <=9 begin set @s = ' ' set @j = 1 wh ...
- JavaScript高级程序设计(第三版)学习笔记13、14章
第13章,事件 事件冒泡 IE的事件叫做事件冒泡:由具体到不具体 <!DOCTYPE html> <html> <head> <title>E ...
- C++的转换构造函数、拷贝构造函数、赋值运算符重载
1 转换构造函数 C++的转换构造函数是只有一个参数的构造函数.当程序试图将一个其他类型的对象或基本类型值赋给该类的一个待初始化对象时(如Person p="Dean";) ...
- js调用本地 exe
js方法 function Run(strPath) //only for ie { try { var objShell = new ActiveXObject("wscript.sh ...
- SVN服务器从Windows迁移到Linux
gerui 2013.9.14 ge-rui@sohu.com 一.备份VisualSVN项目 1. 现在要使用Linux作为svn服务器,之前是在windows Server 2008上的,用的是V ...
- iOS崩溃日志分析
Incident Identifier: 55864905-937C-4172-B435-2ACA13D3070ECrashReporter Key: b85cab13431711060a5fab55 ...
- Dubbo在Spring和Spring Boot中的使用
一.在Spring中使用Dubbo 1.Maven依赖 <dependency> <groupId>com.alibaba</groupId> <artifa ...
- CUDA_矢量相加
#include<iostream> #define N 10 _ _global_ _ void add(*a,*b,*c) { int tid=blockIdx.x; if(tid&l ...