再写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)先 ...
随机推荐
- 关于Eclipse的常用快捷键
最近用到了一些以前开发中没有用到的Eclipse中的快捷键,现在总结如下: Ctrl+Shift+G 全局 工作区中的引用 使用的图解: 在所选定的类上,按下Ctrl+Shift+G在Search的T ...
- C语言中 指针、引用和取值
指针是一个存储计算机内存地址的变量.从指针指向的内存读取数据称作指针的取值.指针可以指向某些具体类型的变量地址,例如int.long和double.指针也可以是void类型.NULL指针和未初始化指针 ...
- php json_encode转JSON 编码显示中文
对变量进行 JSON 编码显示中文 /**context":"/u2345/u43245/u2345 转成中文显示 * 对变量进行 JSON 编码[{"time" ...
- hdu 1096 A+B for Input-Output Practice (VIII)
A+B for Input-Output Practice (VIII) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3 ...
- gluster 安装配置基本指南
基于网络上的多篇文章,做了一些调整. gluster安装 ### Installing Gluster wget -P /etc/yum.repos.d http://download.gluste ...
- 使用 Spring 2.5 基于注解驱动的 Spring MVC
http://www.ibm.com/developerworks/cn/java/j-lo-spring25-mvc/ 概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Sp ...
- 之前做web性能优化的一些个人心得
一个web项目后期的维护主要在于性能方面.数据吞吐量一旦增大各种bug都出来了.那些通过硬件<数据库分表,数据库主从分离,读写分离>等的一些手段此处就不多说了.本文主要在编码方面做一个性能 ...
- Decorator设计模式浅谈
装饰类跟基础组件都实现了目标接口,是为了匹配正确的类型.Java中的IO设计就是典型的Decorator设计模式. 装饰模式产生的初衷是, 对默认实现类的行为进行扩展. 由于装饰类的构造器接受的参数是 ...
- smarty半小时快速上手入门教程
http://www.jb51.net/article/56754.htm http://www.yiibai.com/smarty/smarty_functions.html http://www. ...
- HTTP和HTTPS详解
http://blog.csdn.net/mingli198611/article/details/8055261/ 转自:http://www.cnblogs.com/ok-lanyan/archi ...