为什么我这么弱




其实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;
}

最新文章

  1. WinDbg调试.NET程序入门
  2. Java学习第三天160818 表单 框架 下拉列表等
  3. 部署步骤“回收 IIS 应用程序池”中出现错误: &lt;nativehr&gt;0x80070005&lt;/nativehr&gt;&lt;nativestack&gt;&lt;/nativestack&gt;拒绝访问。
  4. JS面向对象组件 -- 继承的其他方式(类式继承、原型继承)
  5. RAM区间最值
  6. iscc2016 mobile1-TurtleShell.apk解题过程
  7. 【Xamarin破解补丁找不到?】
  8. 创见WiFi SD卡破解之路
  9. python 备份压缩传输
  10. Sqlserver 锁(转)
  11. Lora开发
  12. org.hibernate.InvalidMappingException: Could not parse mapping document from resource com/domain/book.hbm.xml 联网跑一跑
  13. java泛型讲解
  14. 【JEECG技术文档】Jeecg高级查询器
  15. P4001 [BJOI2006]狼抓兔子(对偶图)
  16. 量身打造自己的MyEclipse(多图)
  17. UIViewController的初始化
  18. docker redis
  19. 考取RHCE认证的历程,总结的经验
  20. Redis学习篇(十二)之管道技术

热门文章

  1. linux系统下文件误删除该如何恢复?
  2. 关于python导入数据库excel数据时出现102, b&quot;Incorrect syntax near &#39;.15562&#39;.DB-Lib error message 20018, severity 1的问题总结
  3. Training loop Run Builder和namedtuple()函数
  4. GDOI 2021 普及组溺水记
  5. 技术分享 | app自动化测试(Android)--App 控件交互
  6. 论文解读(USIB)《Towards Explanation for Unsupervised Graph-Level Representation Learning》
  7. BUUCTF-佛系少年
  8. 关于Vue 移动端适配 (px2rem 插件将px转为rem)
  9. UiPath图片操作截图的介绍和使用
  10. Java ArrayList和LinkedList