没什么好说的,今天又考了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模板的更多相关文章

  1. FFT模板(多项式乘法)

    FFT模板(多项式乘法) 标签: FFT 扯淡 一晚上都用来捣鼓这个东西了...... 这里贴一位神犇的博客,我认为讲的比较清楚了.(刚好适合我这种复数都没学的) http://blog.csdn.n ...

  2. P1919 【模板】A*B Problem升级版 /// FFT模板

    题目大意: 给定l,输入两个位数为l的数A B 输出两者的乘积 FFT讲解 这个讲解蛮好的 就是讲解里面贴的模板是错误的 struct cpx { double x,y; cpx(double _x= ...

  3. HDU 1402 A * B Problem Plus (FFT模板题)

    FFT模板题,求A*B. 用次FFT模板需要注意的是,N应为2的幂次,不然二进制平摊反转置换会出现死循环. 取出结果值时注意精度,要加上eps才能A. #include <cstdio> ...

  4. switch case :在JDK 7中,又加入了对String类型的支持,从此不用再写If-Else来判断字符串了

    switch的case语句可以处理int,short,byte,char类型的值, 因为short,byte,char都会转换成int进行处理,这一点也可以从生成的字节码看出. char a = 'e ...

  5. 传递的值是this,在js里就不用再写$(this)

    <input class="editinput" value="${detail.earlymoneyrmb}" name="earlymone ...

  6. 以前写SpringMVC的时候,如果需要访问一个页面,必须要写Controller类,然后再写一个方法跳转到页面,感觉好麻烦,其实重写WebMvcConfigurerAdapter中的addViewControllers方法即可达到效果了

    以前写SpringMVC的时候,如果需要访问一个页面,必须要写Controller类,然后再写一个方法跳转到页面,感觉好麻烦,其实重写WebMvcConfigurerAdapter中的addViewC ...

  7. hdu1402(大数a*b&fft模板)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1402 题意: 给出两个长度1e5以内的大数a, b, 输出 a * b. 思路: fft模板 详情参 ...

  8. 再写一篇tps限流

    再写一篇tps限流 各种限流算法的称呼 网上有很多文章介绍限流算法,但是对于这些算法的称呼与描述也是有点难以理解.不管那么多了.我先按我理解的维度梳理一下. 主要维度是:是正向计数还是反向计数.是定点 ...

  9. 缓存与数据库一致性之二:高并发下的key重建(先淘汰cache再写db)的问题

    一.为什么数据会不一致 回顾一下上一篇文章<缓存与数据库一致性之一:缓存更新设计>中对缓存.数据库进行读写操作的流程. 写流程: (1)先淘汰cache (2)再写db 读流程: (1)先 ...

随机推荐

  1. 问题:loadrunner录制event为0

     loadrunner录制问题问题1:录制时出现event为0的状况 解决办法: 1.如果是IE浏览器,把启用第三方浏览器扩展*钩给去掉    2使用火狐浏览器,这个就比较好,在lr启动的时候就去勾选 ...

  2. jemter接口测试之---接口测试的一些约定

      一.接口规范 1.前端请求接口 请求数据格式:appType =1&args ={json}&session =xxx&timestamp =now&sign =x ...

  3. CentOS 7 gedit编辑器中文乱码解决方法

    无需root登陆 打开终端输入如下命令: gsettings set org.gnome.gedit.preferences.encodings auto-detected "['GB180 ...

  4. Maven(3.0.5) 环境的安装配置

    Maven 安装步骤 JDK 安装:在使用 Maven 之前,要确定已经安装了 JDK. huey@huey-K42JE:~$ java -version java version "1.7 ...

  5. ASP大数据量使用GetRows()提升速度

    抽取10万条数据,Access数据库,GetRows() 现有10W条数据,Access数据库保存 通过正常提取: <% Set conn= Server.CreateObject(" ...

  6. Azure Redis Cache作为ASP.NET Session状态提供程序

    从上一篇博客<使用Azure Redis Cache>我们已经可以创建并使用Redis Cache为我们服务了. 作为Web开发者,我们都知道Session状态默认是保存在内存中的,它的优 ...

  7. Microsoft SQL Server Product Samples:Database

    从SQL Server 2005 之后示例数据都为AdventureWorks,需要的通过codeplex网站下载.这样设计的目的应该在于是生产库行不必要的用户以及权限分配. 从以下网址访问http: ...

  8. Ext.Net学习笔记15:Ext.Net GridPanel 汇总(Summary)用法

    Ext.Net学习笔记15:Ext.Net GridPanel 汇总(Summary)用法 Summary的用法和Group一样简单,分为两步: 启用Summary功能 在Feature标签内,添加如 ...

  9. C#变量初始化问题:字段初始值无法引用非静态字段、方法或属性

    http://www.cnblogs.com/bluestorm/p/3432190.html 问题:字段初始值设定项无法引用非静态字段.方法或属性的问题 下面代码出错的原因,在类中定义的字段为什么不 ...

  10. Dubbo在Spring和Spring Boot中的使用

    一.在Spring中使用Dubbo 1.Maven依赖 <dependency> <groupId>com.alibaba</groupId> <artifa ...