HDU 1402 A * B Problem Plus FFT入门题
来源:互联网 发布:铭牌设计软件 编辑:程序博客网 时间:2024/06/05 09:38
FFT的理解
大牛的博客
大牛的博客
简单的理解
就是对两个多项式乘法求系数
代码
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;const int MAXN = 2e5 + 5;const double PI = acos(-1.0);struct complex { double r, i; complex(double _r = 0.0, double _i = 0.0) { r = _r; i = _i; } complex operator +(const complex &b) { return complex(r + b.r, i + b.i); } complex operator -(const complex &b) { return complex(r - b.r, i - b.i); } complex operator *(const complex &b) { return complex(r * b.r - i * b.i, r * b.i + i * b.r); }};void change(complex y[], int len) { int i, j, k; for(i = 1, j = len / 2; i < len - 1; i++) { if(i < j)swap(y[i], y[j]); k = len / 2; while( j >= k) { j -= k; k /= 2; } if(j < k) j += k; }}void fft(complex y[], int len, int on) { change(y, len); for(int h = 2; h <= len; h <<= 1) { complex wn(cos(-on * 2 * PI / h), sin(-on * 2 * PI / h)); for(int j = 0; j < len; j += h) { complex w(1, 0); for(int k = j; k < j + h / 2; k++) { complex u = y[k]; complex t = w * y[k + h / 2]; y[k] = u + t; y[k + h / 2] = u - t; w = w * wn; } } } if(on == -1) for(int i = 0; i < len; i++) y[i].r /= len;}char A[MAXN], B[MAXN];int ans[MAXN];complex x[MAXN], y[MAXN];int main() { while(~scanf("%s%s", A, B)) { int lena = 0, len1 = strlen(A); int lenb = 0, len2 = strlen(B); while(1 << lena < len1) lena ++; while(1 << lenb < len2) lenb ++; int len = 1 << max(lena, lenb) + 1; for(int i = 0; i < len; i ++) { if(i < len1) x[i] = complex(A[len1 - i - 1] - '0', 0); else x[i] = complex(0, 0); if(i < len2) y[i] = complex(B[len2 - i - 1] - '0', 0); else y[i] = complex(0, 0); } fft(x, len, 1); fft(y, len, 1); for(int i = 0; i < len; i ++) { x[i] = x[i] * y[i]; } fft(x, len, -1); for(int i = 0; i < len; i ++) { ans[i] = (int)(x[i].r + 0.5); } for(int i = 0; i < len; i ++) { ans[i + 1] += ans[i] / 10; ans[i] %= 10; } int flag = 0; for(int i = len - 1; i >= 0; i --) { if(ans[i] > 0) { flag = 1; printf("%d", ans[i]); continue; } if(flag || i == 0) printf("0"); } printf("\n"); } return 0;}
1 0
- HDU 1402 A * B Problem Plus FFT入门题
- [HDU 1402] A * B Problem Plus (FFT入门)
- HDU 1402 A * B Problem Plus (FFT入门,高精度乘法)
- hdu 1402 A * B Problem Plus FFT
- HDU 1402 A * B Problem Plus FFT
- [HDU 1402]A * B Problem Plus(FFT)
- HDU 1402 A * B Problem Plus FFT
- 【HDU】1402 A * B Problem Plus 【FFT】
- 【HDU】1402 A * B Problem Plus 【FFT】
- HDU 1402 A * B Problem Plus FFT
- hdu 1402 A * B Problem Plus[【FFT】
- HDU 1402 A * B Problem Plus FFT
- hdu 1402 A * B Problem Plus(FFT)
- [HDU]1402 A * B Problem Plus [FFT]
- hdu 1402 A * B Problem Plus(FFT)
- HDU 1402 A * B Problem Plus (FFT模板题)
- HDU 1402 A * B Problem Plus (FFT模板题)
- HDU 1402 A * B Problem Plus(FFT模版题)
- nyoj 大数阶乘
- 提高篇编程项目 第十九讲 扑克牌 发奖金
- linux命令详解-useradd,groupadd
- 最新版 Xcode8 macOS Sierra 10.12 安装CocoaPods
- 基于oracle的分页功能(jdbc实现)
- HDU 1402 A * B Problem Plus FFT入门题
- c语言中的内存问题
- android graphic(7)—gralloc分配图形缓冲区
- Gstreamer中一些gst-launch常用命令
- navicat 多用户远程连接 mysql 数据库
- hdu 4502 (吉哥系列故事——临时工计划)(贪心+背包)
- iOS 堆栈符号解析最佳实践
- 《程序员》Part4 数据库
- 依赖注入的解析