为什么我这么弱




其实FFT也挺水的,一点数学基础加上细心即可。细节·技巧挺多。

递归

在TLE的边缘苦苦挣扎

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long //#define ON_DEBUG #ifdef ON_DEBUG #define D_e_Line printf("\n\n----------\n\n")
#define D_e(x) cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin); #else #define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ; #endif struct ios{
template<typename ATP>ios& operator >> (ATP &x){
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x*= f;
return *this;
}
}io;
using namespace std; const int N = 4000007; // how much should I open QAQ ?
const double pi = acos(-1.0); struct Complex{
double x, y;
Complex (double xx = 0, double yy = 0) {x = xx, y = yy;} Complex operator + (Complex b){ return Complex(x + b.x, y + b.y); }
Complex operator - (Complex b){ return Complex(x - b.x, y - b.y); }
Complex operator * (Complex b){ return Complex(x * b.x - y * b.y, x * b.y + y * b.x); }
}a[N], b[N]; inline void FFT(int limit, Complex *a, int opt){
if(limit == 1) return;
Complex a1[(limit >> 1) + 3], a2[(limit >> 1) + 3];
for(register int i = 0; i <= limit; i += 2){
a1[i >> 1] = a[i];
a2[i >> 1] = a[i + 1];
}
FFT(limit >> 1, a1, opt);
FFT(limit >> 1, a2, opt);
Complex Wn = Complex( cos(2.0 * pi / limit), opt * sin(2.0 * pi / limit));
Complex w = Complex( 1, 0);
R(i,0,(limit >> 1) - 1){ // be careful, do not write 'R(i,0,(limit >> 1))'
a[i] = a1[i] + w * a2[i];
a[i + (limit >> 1)] = a1[i] - w * a2[i];
w = w * Wn;
}
} int main(){
int n, m;
io >> n >> m;
R(i,0,n) io >> a[i].x;
R(i,0,m) io >> b[i].x; int limit;
for(limit = 1; limit <= n + m; limit <<= 1); FFT(limit, a, 1);
FFT(limit, b, 1); // coefficient changes to point value R(i,0,limit){
a[i] = a[i] * b[i];
} FFT(limit, a, -1); // point value changes to coefficient R(i,0,n + m){
printf("%d ", (int)(a[i].x / limit + 0.5)); // ans should divide limit
} return 0;
}

迭代

快得飞起\ *^* /

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long //#define ON_DEBUG #ifdef ON_DEBUG #define D_e_Line printf("\n\n----------\n\n")
#define D_e(x) cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin); #else #define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ; #endif struct ios{
template<typename ATP>ios& operator >> (ATP &x){
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x*= f;
return *this;
}
}io;
using namespace std; const int N = 4000007; // how much should I open QAQ ? // Oh, I undersand ! It's influenced by 'limit'
const double pi = acos(-1.0); struct Complex{
double x, y;
Complex (double xx = 0, double yy = 0) {x = xx, y = yy;} Complex operator + (Complex b){ return Complex(x + b.x, y + b.y); }
Complex operator - (Complex b){ return Complex(x - b.x, y - b.y); }
Complex operator * (Complex b){ return Complex(x * b.x - y * b.y, x * b.y + y * b.x); }
}a[N], b[N]; int r[N]; inline void FFT(int limit, Complex *a, int opt){
R(i,0,limit - 1)
if(i < r[i])
swap(a[i], a[r[i]]);
for(register int mid = 1; mid < limit; mid <<= 1){
Complex Wn( cos(pi / mid), opt * sin(pi / mid));
int len = mid << 1;
for(register int j = 0; j < limit; j += len){
Complex w( 1, 0);
R(k,0,mid - 1){
Complex x = a[j + k], y = w * a[j + mid + k];
a[j + k] = x + y;
a[j + mid + k] = x - y;
w = w * Wn;
}
}
}
} int main(){
FileOpen();
int n, m;
io >> n >> m;
R(i,0,n) io >> a[i].x;
R(i,0,m) io >> b[i].x; int limit = 1, len = 0;
while(limit <= n + m){
limit <<= 1;
++len;
} R(i,0,limit - 1){
r[i] = (r[i >> 1] >> 1) | ((i & 1) << (len - 1));
} FFT(limit, a, 1);
FFT(limit, b, 1); // coefficient changes to point value R(i,0,limit){
a[i] = a[i] * b[i];
} FFT(limit, a, -1); // point value changes to coefficient R(i,0,n + m){
printf("%d ", (int)(a[i].x / limit + 0.5)); // ans should divide limit
} return 0;
}

Luogu3803 【模板】多项式乘法(FFT)的更多相关文章

  1. 洛谷.3803.[模板]多项式乘法(FFT)

    题目链接:洛谷.LOJ. FFT相关:快速傅里叶变换(FFT)详解.FFT总结.从多项式乘法到快速傅里叶变换. 5.4 又看了一遍,这个也不错. 2019.3.7 叕看了一遍,推荐这个. #inclu ...

  2. 【Luogu3803】多项式乘法FFT(FFT)

    题目戳我 一道模板题 自己尝试证明了大部分... 剩下的还是没太证出来... 所以就是一个模板放在这里 以后再来补东西吧.... #include<iostream> #include&l ...

  3. P3803 [模板] 多项式乘法 (FFT)

    Rt 注意len要为2的幂 #include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); inli ...

  4. 多项式乘法(FFT)学习笔记

    ------------------------------------------本文只探讨多项式乘法(FFT)在信息学中的应用如有错误或不明欢迎指出或提问,在此不胜感激 多项式 1.系数表示法  ...

  5. @总结 - 1@ 多项式乘法 —— FFT

    目录 @0 - 参考资料@ @1 - 一些概念@ @2 - 傅里叶正变换@ @3 - 傅里叶逆变换@ @4 - 迭代实现 FFT@ @5 - 参考代码实现@ @6 - 快速数论变换 NTT@ @7 - ...

  6. 【learning】多项式乘法&fft

    [吐槽] 以前一直觉得这个东西十分高端完全不会qwq 但是向lyy.yxq.yww.dtz等dalao们学习之后发现这个东西的代码实现其实极其简洁 于是趁着还没有忘记赶紧来写一篇博 (说起来这篇东西的 ...

  7. [uoj#34] [洛谷P3803] 多项式乘法(FFT)

    新技能--FFT. 可在 \(O(nlogn)\) 时间内完成多项式在系数表达与点值表达之间的转换. 其中最关键的一点便为单位复数根,有神奇的折半性质. 多项式乘法(即为卷积)的常见形式: \[ C_ ...

  8. UOJ 34 多项式乘法 FFT 模板

    这是一道模板题. 给你两个多项式,请输出乘起来后的多项式. 输入格式 第一行两个整数 nn 和 mm,分别表示两个多项式的次数. 第二行 n+1n+1 个整数,表示第一个多项式的 00 到 nn 次项 ...

  9. [模板] 多项式: 乘法/求逆/分治fft/微积分/ln/exp/幂

    多项式 代码 const int nsz=(int)4e5+50; const ll nmod=998244353,g=3,ginv=332748118ll; //basic math ll qp(l ...

  10. 【模板】多项式乘法(FFT)

    题目描述 给定一个n次多项式F(x),和一个m次多项式G(x). 请求出F(x)和G(x)的卷积. 输入输出格式 输入格式: 第一行2个正整数n,m. 接下来一行n+1个数字,从低到高表示F(x)的系 ...

随机推荐

  1. 即时通讯IM,是时代进步的逆流?看看JNPF怎么说

    JNPF快速开发平台所包含的第四个重要的开发框架是即时通讯沟通工具.即时沟通工具的目的是让各大企事业单位在各种业务工作流程环境下实现实时无缝协同办公,打破信息数据孤岛,形成高效的层级流转审批和各流程环 ...

  2. vue项目经常遇到的Error: Loading chunk * failed

    vue项目随着代码量.业务组件.路由页面等的丰富,出于性能要求考虑不得不使用代码分割技术实现路由和组件的懒加载,这看似没什么问题 当每次通过npm run build构建生产包并部署到服务器后,操作页 ...

  3. ExtJS 布局-Accordion布局(Accordion layout)

    更新记录: 2022年6月2日 开始. 2022年6月3日 发布. 1.说明 accordion(手风琴)布局一次仅显示一个子组件,内置支持 折叠 和 展开.当需要堆叠多个子组件,并每次只显示一次时, ...

  4. php公立转农历

    <?php function nongli($riqi) { //优化修改 20160807 FXL $nian=date('Y',strtotime($riqi)); $yue=date('m ...

  5. JAVA设计模式总结—建造者模式

    建造者模式 模式动机与定义 ​ 首先建造者模式的动机是为了创建复杂对象,简化传统的创建方法,提高创建的效率和可读性. ​ 像图中的这个例子,用户的需求是驾驶一辆汽车,但是对于用户来说是不需要了解汽车装 ...

  6. js与java encodeURI 进行编码与解码

    JS escape()使用转义序列替换某些字符来对字符串进行编码  JavaScript 中国 编码后 JavaScript %u4E2D%u56FD unescape()对使用   encodeUR ...

  7. 『现学现忘』Docker基础 — 41、将本地镜像推送到阿里云

    目录 1.准备工作 2.阿里云容器镜像仓库的使用 (1)创建命名空间 (2)创建容器镜像 (3)查看阿里云镜像仓库的信息 3.将本地Docker镜像推送到阿里云 (1)登陆阿里云 (2)给镜像生成版本 ...

  8. 015(Power string)(哈希表)

    题目:http://ybt.ssoier.cn:8088/problem_show.php?pid=1457 题目思路: 思路就是预设子串的长度,从1开始,而后一段一段试 试到一个对不上的就打回 如果 ...

  9. CesiumJS 2022^ 源码解读[0] - 文章目录与源码工程结构

    很高兴你能在浮躁的年代里还有兴趣阅读源代码,CesiumJS 至今已有十年以上,代码量也积累了三十多万行(未压缩状态). 我也很荣幸自己的文章能被读者看到,如果对你有帮助.有启发,点个赞就是对我最大的 ...

  10. Codeforces Round #783 (Div. 2)

    A. Direction Change 题意 从(1,1)点出发到(n,m),每次可以向上下左右四个方向移动,但是不能与上次移动方向相同 最少要移动多少不,如果不能到达输出 -1 思路 假设n< ...