P3803 FFT求多项式系数
P3803 FFT求多项式系数
传送门:https://www.luogu.org/problemnew/show/P3803
题意:
这是一道FFT模板题,求多项式系数
题解:
对a和b的系数求一个fft,转换为点乘式后
O(n)扫一遍直接算系数即可
对于多项式相加
\(\begin{array}{l}{A(x)=\left(x_{0}, y_{0}\right),\left(x_{1}, y_{1}\right) \ldots\left(x_{n}, y_{n}\right)} \\ {B(x)=\left(x_{0}, y_{0}^{\prime}\right),\left(x_{1}, y_{1}^{\prime}\right) \ldots .\left(x_{n}, y_{n}\right)}\end{array}\)
\(A(x)+B(x)=\left(x_{0}, y_{0}+y_{0}^{\prime}\right),\left(x_{1}, y_{1}+y_{1}^{\prime}\right)\left(x_{n}, y_{n}+y_{n}^{\prime}\right)\)
对于多项式相乘,我们需要补上一些项使得最后乘的的系数个数为2n+1
\(\begin{array}{l}{A(x)=\left(x_{0}, y_{0}\right),\left(x_{1}, y_{1}\right) \ldots\left(x_{2 n}, y_{2 n}\right)} \\ {B(x)=\left(x_{0}, y_{0}^{\prime}\right),\left(x_{1}, y_{1}^{\prime}\right) \ldots .\left(x_{2 n}, y_{2 n}\right)}\end{array}\)
\(A(x) B(x)=\left(x_{0}, y_{0} y_{0}^{\prime}\right),\left(x_{1}, y_{1} y_{1}^{\prime}\right)\left(x_{2 n}, y_{2 n} y_{2 n}^{\prime}\right)\)
代码:
#include <set>
#include <map>
#include <cmath>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
const int maxn = 1e7 + 5;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double Pi = acos(-1.0);
LL quick_pow(LL x, LL y) {
LL ans = 1;
while(y) {
if(y & 1) {
ans = ans * x % mod;
} x = x * x % mod;
y >>= 1;
} return ans;
}
struct complex {
double x, y;
complex(double xx = 0, double yy = 0) {
x = xx, y = yy;
}
} a[maxn], b[maxn];
complex operator + (complex a, complex b) {
return complex(a.x + b.x, a.y + b.y);
}
complex operator - (complex a, complex b) {
return complex(a.x - b.x, a.y - b.y);
}
complex operator * (complex a, complex b) {
return complex(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);
}
int n, m;
int l, r[maxn];
int limit = 1;
void fft(complex *A, int type) {
for(int i = 0; i < limit; i++) {
if(i < r[i]) swap(A[i], A[r[i]]);
}
for(int mid = 1; mid < limit; mid <<= 1) {
complex Wn(cos(Pi / mid), type * sin(Pi / mid));
for(int R = mid << 1, j = 0; j < limit; j += R) {
complex w(1, 0);
for(int k = 0; k < mid; k++, w = w * Wn) {
complex x = A[j + k], y = w * A[j + mid + k];
A[j + k] = x + y;
A[j + mid + k] = x - y;
}
}
}
}
int main() {
#ifndef ONLINE_JUDGE
FIN
#endif
scanf("%d%d", &n, &m);
for(int i = 0; i <= n; i++) scanf("%lf", &a[i].x);
for(int i = 0; i <= m; i++) scanf("%lf", &b[i].x);
while(limit <= n + m) limit <<= 1, l++;
for(int i = 0; i < limit; i++) {
r[i] = (r[i >> 1] >> 1) | ((i & 1) << (l - 1));
}
fft(a, 1);
fft(b, 1);
for(int i = 0; i <= limit; i++) {
a[i] = a[i] * b[i];
}
fft(a, -1);
for(int i = 0; i <= n + m; i++) {
printf("%d ", (int)(a[i].x / limit + 0.5));
}
printf("\n");
return 0;
}
P3803 FFT求多项式系数的更多相关文章
- 洛谷p3803 FFT入门
洛谷p3803 FFT入门 ps:花了我一天的时间弄懂fft的原理,感觉fft的折半很神奇! 大致谈一谈FFT的基本原理: 对于两个多项式的卷积,可以O(n^2)求出来(妥妥的暴力) 显然一个多项式可 ...
- [笔记]ACM笔记 - 利用FFT求卷积(求多项式乘法)
卷积 给定向量:, 向量和: 数量积(内积.点积): 卷积:,其中 例如: 卷积的最典型的应用就是多项式乘法(多项式乘法就是求卷积).以下就用多项式乘法来描述.举例卷积与DFT. 关于多项式 对于多项 ...
- CodeForces - 528D Fuzzy Search (FFT求子串匹配)
题意:求母串中可以匹配模式串的子串的个数,但是每一位i的字符可以左右偏移k个位置. 分析:类似于 UVALive -4671. 用FFT求出每个字符成功匹配的个数.因为字符可以偏移k个单位,先用尺取法 ...
- FFT求卷积(多项式乘法)
FFT求卷积(多项式乘法) 卷积 如果有两个无限序列a和b,那么它们卷积的结果是:\(y_n=\sum_{i=-\infty}^\infty a_ib_{n-i}\).如果a和b是有限序列,a最低的项 ...
- BZOJ3527 推出卷积公式FFT求值
BZOJ3527 推出卷积公式FFT求值 传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=3527 题意: \(F_{j}=\sum_{i&l ...
- HDU 1402 A * B Problem Plus (FFT求高精度乘法)
A * B Problem Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Gym - 101667H - Rock Paper Scissors FFT 求区间相同个数
Gym - 101667H:https://vjudge.net/problem/Gym-101667H 参考:https://blog.csdn.net/weixin_37517391/articl ...
- MATLAB使用fft求取给定音频信号的频率
一段10s立体声音频,采样率位8000Hz,已知频率为1000Hz clc; clear; [data, Fs] = audioread('1khz_stereo_8000.wav'); fs=Fs; ...
- 【FFT求卷积】Problem D. Duel
[AC] #include <stdio.h> #include <iostream> #include <string.h> #include <algor ...
随机推荐
- hdu5289 RMQ+二分
RMQ预处理最大值,最小值,然后对于每一点,二分可能满足的区间长度,长度-1就是该店开始的区间满足的个数. #include<stdio.h> #include<string.h&g ...
- iPhone 7 Plus 维修记 (一)(2019-08-07)
iPhone 7 Plus 维修记 问题 电池鼓包,屏幕已经被撑起,偶尔死机突然关机. 分析 初步分析是电池损坏. 维修 由于电池没有双易拉条需要将后壳加热后再取出电池. 更换电池后测试,发现电量一会 ...
- js原生复习2.0
// 1.闭包的作用// 实现共有变量,函数累加器的实现// 可以做缓存以及储存结构// 可以实现封装,实现属性私有化// 模块开发,防止全局污染// var name = 123;// var in ...
- 高可用Kubernetes集群原理介绍
■ 文/ 天云软件 云平台开发工程师 张伟 1. 背景 Kubernetes作为容器应用的管理中心,对集群内部所有容器的生命周期进行管理,结合自身的健康检查及错误恢复机制,实现了集群内部应用层的高可用 ...
- html的select标签清空option!~~~~
最好的方法:document.getElementById("selectId").length = 1; 也可以document.getElementById("sel ...
- Java练习 SDUT-1294_选票统计
选票统计 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 某校学生会主席由全校学生投票选举产生,共有m名候选人报名参选, ...
- 17-3 cookie和session
一 . Cookie 1.cookie 是什么? 保存在浏览器端的键值对! 服务端在返回响应的时候,告诉浏览器保存的键值对!浏览器可以拒绝保存Cookie. 2. 为什么要有cookie? HTTP请 ...
- ModuleNotFoundError: No module named 'tools.nnwrap' pytorch 安装
https://pytorch.org/get-started/locally/ pytorch 主页选择后安装
- CNN输出维度的计算
在 CNN 的一层中的 patch 中共享权重 w ,无论猫在图片的哪个位置都可以找到. 当我们试图识别一个猫的图片的时候,我们并不在意猫出现在哪个位置.无论是左上角,右下角,它在你眼里都是一只猫 ...
- OracleSpatial函数
Oracle_spatial的函数 一sdo_Geom包的函数: 用于表示两个几何对象的关系(结果为True/False)的函数:RELATE,WITHIN_DISTANCE 验证的函数:VALIDA ...