没什么好说的,今天又考了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. 关于Java中return和finally谁先执行.

    例子一: public class A { public static void main(String[] args) { System.out.print(tt()); } public stat ...

  2. 【转】android自动化测试之MonkeyRunner使用实例(三)

    一.使用CMD命令打开模拟器 运行monkeyrunner之前必须先运行相应的模拟器或连上设备,不然monkeyrunner无法连接设备. 1.1  用Elipse打开Android模拟器或在CMD中 ...

  3. ant design 树形组件怎么使用

    getDefaultProps doesn't work with ES6 syntax; warning is not helpful 解决后: 参考地址:https://github.com/fa ...

  4. http请求之referer头与防盗链

    在网页中的占用大流量的信息可以写成这个信息在网络上的url位置,这样就会减少本网站的流量,但是其他网站也 不会随意让你使用人家的资源,因为这样的情对人家的网站没有好处,会增加人家网站的流量,所以要防止 ...

  5. 【JavaScript权威指南(第五版)】笔记之第二部分 客户端JavaScript 第13章~第23章

    第十三章 Web浏览器中的javascript ①   eg:下面两行代码实际上执行的是相同的功能 var answer = 42; window.answer = 42;   ③每个window对象 ...

  6. sql 更新重复数据只取一条记录

    select s.*  from (     select *, row_number() over (partition by PersonnelAccount order BY Personnel ...

  7. 40个Java集合面试问题和答案【中】【转载】

    接上文:http://www.cnblogs.com/xujianbo/p/5148075.html   16.UnsupportedOperationException是什么? Unsupporte ...

  8. iOS中 常用的mac终端指令

    1.使用caffeinate阻止Mac运行屏幕保护和睡眠 caffeinate能阻止Mac进入睡眠状态,而且屏幕保护也不会激活.我们最好使用-t为命令加入具体的时间.比如下面的命令可以使Mac一小时内 ...

  9. asp:时间的显示

    DateTime dt = DateTime.Now;//    Label1.Text = dt.ToString();//2005-11-5 13:21:25//    Label2.Text = ...

  10. [译]JavaScript insertAdjacentHTML

    原文地址:http://davidwalsh.name/insertadjacenthtml-beforeend 该死的DOM慢的很.随着我们的网站动态交互和Ajax操作越来越多,我们需要寻找一种高性 ...